How to Push a Docker Image to Docker Hub from Azure DevOps Pipelines ππ³
In the world of DevOps, automation is everything! One key task is building and pushing Docker images to a container registry like Docker Hub. In this blog, I'll guide you step-by-step on how to build and push a Docker image from Azure DevOps Pipelines to Docker Hub. By the end, you'll have a CI/CD pipeline that automates your Docker image deployments. Let's dive in! π»
Step 1: Configure Azure DevOps Project π§
Before anything else, you need to set up a project in Azure DevOps where your pipeline will run.
-
Create a Project: Start by creating a new project in Azure DevOps.
-
Link Repositories: Make sure your project repository contains a
Dockerfileand all the source code needed to build the Docker image.
Step 2: Set Up a Docker Hub Service Connection π³π
For the pipeline to push the Docker image to Docker Hub, you need to authenticate Azure DevOps with Docker Hub:
-
Go to Project Settings > Service Connections.
-
Add a new service connection for Docker Hub.
-
Enter your Docker Hub credentials (username and password/access token).
-
Save the connection and name it
DockerHubfor easy reference.
Step 3: Write the Azure DevOps Pipeline YAML π
Now, letβs create the Azure DevOps pipeline YAML file. This file will build and push the Docker image to Docker Hub.trigger: - main resources: - repo: self variables: system.debug: true # Enable debug for troubleshooting tag: '$(Build.BuildId)' # Unique tag for each build imageName: 'pexabo/thanoslistener' # Docker image with Docker Hub username stages: - stage: Build displayName: Build and Push Docker Image jobs: - job: Build displayName: Build Docker Image pool: vmImage: ubuntu-latest # Use an Ubuntu agent steps: # Login to Docker Hub - task: Docker@2 displayName: 'Login to Docker Hub' inputs: command: login containerRegistry: 'DockerHub' # Reference to Docker Hub service connection # Build the Docker image - task: Docker@2 displayName: 'Build Docker Image' inputs: command: build dockerfile: '$(Build.SourcesDirectory)/Dockerfile' # Path to the Dockerfile repository: $(imageName) # Define the repository name (image) tags: | $(imageName):$(Build.BuildId) # Tag image with Build ID $(imageName):latest # Tag image as latest # Push the Docker image to Docker Hub - task: Docker@2 displayName: 'Push Docker Image' inputs: command: push repository: $(imageName) # Push the built image to Docker Hub tags: | $(imageName):$(Build.BuildId) $(imageName):latest
Step 4: Trigger the Pipeline πββοΈ
Once your YAML file is ready, commit it to your repository and push it to the main branch (or any branch that triggers the pipeline). Azure DevOps will start the pipeline:
-
Login to Docker Hub: The pipeline will authenticate using the service connection.
-
Build the Docker Image: Docker will build the image from your
Dockerfile. -
Push the Image: The image will be tagged with the build ID and pushed to Docker Hub.
Your image will now be available on Docker Hub, ready for use! π
Step 5: Verify the Docker Image on Docker Hub π
Once the pipeline completes successfully, head over to Docker Hub and verify that your image is available under your repository.
-
Search for your image using the image name (
pexabo/thanoslistenerin our case). -
Check that the image is tagged correctly with the build ID and
latest.
Bonus Tips π‘
-
Use Secrets for Security: Donβt hard-code your Docker Hub password in the YAML file! Use Azure DevOps pipeline secrets to store sensitive information.
-
Automate Further: Once the Docker image is pushed, you can add additional stages in your pipeline to deploy the image to environments like Kubernetes or App Service.
Conclusion π
By following these steps, you've now set up a fully automated pipeline that builds and pushes Docker images to Docker Hub using Azure DevOps Pipelines. This setup will save you tons of time by removing the need to manually build, tag, and push Docker images. π οΈ
Happy Dockerizing! π³β¨
Let me know if you have any questions or need further assistance! π
Imported from rifaterdemsahin.com Β· 2025