Search code examples
c#dockerazure-devopsdockerfile

Get project on the same level for deploy


I have a project in .NET 8 with a structure like this:

MySolution
|-- MySolution.MyApi
|   -- Dockerfile
|   -- nuget.config
|   -- OtherFile API
|-- MySolution.Commun
|   -- DB (entities, migration files, etc.)

We have an other team that manage the deployment files for CI/CD in Azure, so I cannot update them.

The deployment files call the docker build and all.

My issue is that in my Dockerfiles I have to COPY the MySolution.Commun so it can build the API.

Because of the constrain I have to run the docker cmd into the MySolution.MyApi.

(Running the docker build from MySolution folder and specifying the path to the MySolution.MyApi for the build with -f and the dockerfile, works but I cannot modify the deployment files of the other team and add the -f, so that's a no go.)

I try using something like COPY ../MySolution.Commun ./src (and other variation) but every time I run the docker build it say it

does not find the "../MySolution.Commun/MySolution.Commun.csproj"

I'm sure there is something missing but I cannot see it...


Solution

  • The root cause of the problem is with the scope of the build context which needs to be set on the 'docker build' command.

    When running the 'docker build' command under the "MySolution.MyApi" folder (set the working directory as the path of "MySolution.MyApi" folder), if the build context is set as '.' on the command:

    docker build -f Dockerfile -t mathcalc:$(Build.BuildId) .
    

    The scope of build context will be limited under the "MySolution.MyApi" folder. the COPY instruction in the Dockerfile cannot access files outside of this folder.

    When copying source files from the build context, their paths are interpreted as relative to the root of the build context. If you specify a relative path leading outside of the build context, the parent directory paths are stripped out automatically. So, the effective source path of "COPY ../MySolution.Commun ./src" will become "COPY MySolution.Commun ./src" (equivalent to "COPY ./MySolution.Commun ./src"). Since the folder "MySolution.Commun" is not under "MySolution.MyApi", it will be not found. See the documentation of "COPY" for more details.

    enter image description here

    For your case, any changes in the Dockerfile cannot affect the scope of build context. You must make changes on the 'docker build' command itself to let the scope of build context to be the root of the solution which is the parent directory of folders "MySolution.Commun" and "MySolution.MyApi".

    You can contact the team which manage the deployment files for CI/CD, and ask them to changes the pipeline task which runs the 'docker build' command to be with the configuration like as one of below:

    1. Use the default working directory which should be the root of the solution, and set the build context as '.' on the command. The '.' point to the working directory as the root of build context.

      steps:
      - script: |
          docker build -f MySolution.MyApi/Dockerfile -t image-name:image-tag .
        displayName: 'Docker Build'
      
    2. If the working directory is set as the path of the "MySolution.MyApi" folder, you can set the build context as '..' on the command. The '..' point to the parent directory of working directory as the root of build context.

      steps:
      - script: |
          docker build -f Dockerfile -t image-name:image-tag ..
        displayName: 'Docker Build'
        workingDirectory: '$(System.DefaultWorkingDirectory)/MySolution.MyApi'
      

    With above configurations, in the Dockerfile, both "COPY ../MySolution.Commun ./src" and "COPY ./MySolution.Commun ./src" can work fine to copy the files from "MySolution.Commun" folder.