How to configure your Docker build to run on both Visual Studio and on Azure pipelines
When working with Docker in Visual Studio, the default behavior of the IDE is to place the Dockerfile at the project level. However, if your solution has multiple projects and you want to achieve easier integration of the Dockerfile into your Azure pipelines, then you might want to move the file to the solution level. Let us see how to achieve this task.
The steps you have to do in your solution files
-
Physically move the Dockerfile: Move the Dockerfile from the project directory to the solution directory.
-
Modify the .csproj file: Update the
.csproj
of the project which used to contain the Dockerfile to point to the new location of the Dockerfile. TheDockerfileFile
property tells Visual Studio to look for the Dockerfile in the solution directory. Add also theDockerfileContext
property to update the default context used when building the Docker image:
<PropertyGroup>
<DockerfileFile>..\Dockerfile</DockerfileFile>
<DockerfileContext>..\TheFolderContainingYourSolution</DockerfileContext>
</PropertyGroup>
-
Keep Dockerfile contents unchanged: The contents of the Dockerfile can be left as they were when you initially added the file via the option in Visual Studio
-
Build and Run: Press on F5. Your Dockerfile should be picked from its new location, a new image should be created in the Container Registry and a new tab on your browser should open.
The steps you have to do in your Azure pipeline
With the previous steps we managed to move the Dockerfile one folder on top of the Visual Studio solution, however, we now also have to adapt our Azure pipeline to be able to build the Docker image and not get the failed to compute cache key: "/XXX/XXX.csproj" not found
error which is thrown because docker cannot locate the Dockerfile.
We have to adapt our pipeline job like in the following example. Pay attention to the following points:
- The
$(...)
contain variables defined in the Azure pipeline and you should use the correct values for them for your pipeline. - The
buildContext
property is set to the root directory which contains the files needed for the Docker build. By doing this way, we do not have to change any paths in our Dockerfile. Alternatively you could also use the variable$(Build.SourcesDirectory)
as value of the property.
variables:
imageRepository: 'THE_REPOSITORY_NAME_INSIDE_THE_DOCKER_CONTAINER'
containerRegistry: 'THE_DOCKER_CONTAINER_NAME'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
buildContext: "."
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
I hope this article will help you manage building your application in Docker using both Visual Studio or your Azure pipeline.