📖 Scenario: You work in a team that builds Docker images for your app. You want to automate pushing these images to Docker Hub using a Continuous Integration (CI) pipeline.This project guides you to write a simple Dockerfile, set up image tagging, and push the image to Docker Hub from a CI script.
🎯 Goal: Build a Docker image from a Dockerfile, tag it with a version, and push it to Docker Hub using a CI script.
📋 What You'll Learn
Create a Dockerfile with a simple base image
Set a variable for the image tag
Write a command to build and tag the Docker image
Write a command to push the tagged image to Docker Hub
💡 Why This Matters
🌍 Real World
Automating Docker image builds and pushes saves time and avoids manual errors in software delivery.
💼 Career
CI pipelines that build and push Docker images are common tasks for DevOps engineers and developers working with containerized apps.
Progress0 / 4 steps
1
Create a simple Dockerfile
Create a file named Dockerfile with these exact contents: FROM alpine:3.18 CMD ["echo", "Hello from Docker"]
Docker
Hint
This Dockerfile uses Alpine Linux as the base and prints a message when run.
2
Set the image tag variable
In a file named ci_script.sh, create a variable called IMAGE_TAG and set it to "myapp:1.0".
Docker
Hint
Use IMAGE_TAG="myapp:1.0" exactly to set the tag.
3
Build and tag the Docker image
In ci_script.sh, add a command to build the Docker image from the current directory and tag it with $IMAGE_TAG.
Docker
Hint
Use docker build -t $IMAGE_TAG . to build and tag the image.
4
Push the Docker image to Docker Hub
In ci_script.sh, add a command to push the Docker image tagged with $IMAGE_TAG to Docker Hub.
Docker
Hint
Use docker push $IMAGE_TAG to push the image.
Practice
(1/5)
1. What is the main purpose of pushing Docker images from a CI pipeline?
easy
A. To delete old images from the local Docker cache
B. To run containers automatically on the local machine
C. To upload built images to a remote registry for sharing and deployment
D. To build images only on the developer's computer
Solution
Step 1: Understand CI pipeline role
A CI pipeline automates building and testing code changes, including Docker images.
Step 2: Purpose of pushing images
Pushing images uploads them to a remote registry so others can use or deploy them.
Final Answer:
To upload built images to a remote registry for sharing and deployment -> Option C
Quick Check:
Pushing images = Upload to registry [OK]
Hint: Pushing means sending images to a remote place [OK]
Common Mistakes:
Confusing pushing with running containers
Thinking pushing deletes images
Assuming pushing only builds images locally
2. Which command correctly pushes a Docker image named myapp:latest to Docker Hub in a CI script?
easy
A. docker publish myapp:latest
B. docker upload myapp:latest
C. docker send myapp:latest
D. docker push myapp:latest
Solution
Step 1: Identify Docker push syntax
The correct command to upload images to a registry is docker push <image>.
Step 2: Verify other options
Commands like upload, send, and publish are not valid Docker commands.
Final Answer:
docker push myapp:latest -> Option D
Quick Check:
Push = docker push [OK]
Hint: Use 'docker push' to send images to registry [OK]