In part 2, we added code analysis and tests tasks to the build pipeline, now it´s time to build the application and publish it to docker hub.
To build an Docker image, you need to create a Docker file and write the steps to mount your image. In the example, the Docker file is responsible for download the net core sdk image to build the image and download a net core runtime sdk to run the application within.
You can use your own Docker file or use the file from this repository as template.
Back to pipeline, add a new task, by clicking the + button on agent job. Search for Docker, select the Docker task and add to your pipeline. The default action will build the image and push to the container registry.
Once the task is placed, select a container registry connection, this is the place you will publish your image. The container registry can be a Docker Hub, Azure Container Registry or others. In this post we are using Docker Hub as container registry. For first time you need to add a connection to Docker Hub account Add a new connection and fill the properties of connection:
On container repository property, write the name of image you want to create. The user account on Docker Hub must preeced the image name, otherwise, a access denied error will be thrown.
When the pipeline runs, the Docker task will build the image based on the Dockerfile file and will publish the image to your Docker hub account. One important thing is the image tag, this property will set the image version of your Docker image. Change the default value $(Build.BuildId) to $(Build.BuildNumber) pipeline variable. This variable represents the build id number and is unique for each build.
Now your pipeline is ready to build your application and publish to the docker hub registry. In the next and final post we will add a final task, set the pipeline trigger and test it.