📖 Scenario: You work in a small team that wants to automate building a Docker image for your app whenever code is pushed to GitHub. You will create a GitHub Actions workflow file that builds a Docker image using a simple Dockerfile.
🎯 Goal: Build a GitHub Actions workflow file named docker-build.yml that triggers on pushes to the main branch, builds a Docker image from the provided Dockerfile, and tags it with latest.
📋 What You'll Learn
Create a basic Dockerfile with a simple command
Create a GitHub Actions workflow file named docker-build.yml
Configure the workflow to trigger on pushes to main
Add a job that builds the Docker image using the Dockerfile
Tag the Docker image as latest
💡 Why This Matters
🌍 Real World
Automating Docker image builds on code changes helps teams deliver software faster and with fewer errors.
💼 Career
Knowing how to use GitHub Actions with Docker is a key skill for DevOps engineers and developers working with containerized applications.
Progress0 / 4 steps
1
Create a simple Dockerfile
Create a file named Dockerfile with these exact contents: FROM alpine:latest CMD ["echo", "Hello from Docker"]
Docker
Hint
Use FROM alpine:latest to start from a small Linux image. Use CMD ["echo", "Hello from Docker"] to print a message when the container runs.
2
Create GitHub Actions workflow file
Create a file named .github/workflows/docker-build.yml and add the following header lines: name: Docker Build on: push: branches: [main]
Docker
Hint
Use name: Docker Build to name the workflow. Use on: push: branches: [main] to trigger on pushes to main branch.
3
Add job to build Docker image
In .github/workflows/docker-build.yml, add a job named build that runs on ubuntu-latest and uses these steps: 1. actions/checkout@v3 2. docker build -t myapp:latest . run in a run step
Docker
Hint
Use jobs: to define jobs. Use runs-on: ubuntu-latest to specify the runner. Use actions/checkout@v3 to get the code. Use docker build -t myapp:latest . to build the image.
4
Print confirmation message
Add a final step in the build job that prints Docker image built successfully using a run step.
Docker
Hint
Add a step with run: echo "Docker image built successfully" to show success.
Practice
(1/5)
1. What is the main purpose of using GitHub Actions with Docker in a project?
easy
A. To automate building and testing Docker images on code changes
B. To manually run Docker commands on your local machine
C. To replace Docker with a different container tool
D. To write Dockerfiles without using any commands
Solution
Step 1: Understand GitHub Actions role
GitHub Actions automates tasks like building and testing when code changes happen.
Step 2: Connect automation with Docker
Using GitHub Actions with Docker means automating Docker image builds and tests on events like pushes.
Final Answer:
To automate building and testing Docker images on code changes -> Option A
Quick Check:
Automation with Docker = D [OK]
Hint: GitHub Actions automate tasks triggered by code events [OK]
Common Mistakes:
Thinking GitHub Actions replace Docker itself
Confusing manual commands with automation
Assuming Dockerfiles are written by GitHub Actions
2. Which of the following is the correct syntax to build a Docker image inside a GitHub Actions workflow step?
easy
A. run: docker start -t myimage .
B. run: docker create myimage .
C. run: docker push -t myimage .
D. run: docker build -t myimage .
Solution
Step 1: Recall Docker build command
The command to build an image is 'docker build' with '-t' to tag the image.
Step 2: Match with GitHub Actions step syntax
In GitHub Actions, 'run:' is used to run shell commands, so 'run: docker build -t myimage .' is correct.
Final Answer:
run: docker build -t myimage . -> Option D
Quick Check:
Build image command = C [OK]
Hint: Use 'docker build -t name .' to build images [OK]
What will be the output when this step runs successfully?
medium
A. test-image
B. Hello
C. docker build completed
D. Error: image not found
Solution
Step 1: Understand the commands run
The first command builds an image tagged 'test-image'. The second runs a container from it that prints 'Hello'.
Step 2: Identify the output of the run command
The 'docker run' command runs 'echo Hello' inside the container, so the output is 'Hello'.
Final Answer:
Hello -> Option B
Quick Check:
docker run echo output = Hello [OK]
Hint: Run command output is what container prints [OK]
Common Mistakes:
Thinking build command outputs 'test-image'
Expecting build success message as output
Assuming image not found error without build
4. You have this GitHub Actions step:
run: docker build -t myapp .
run: docker run myapp
The workflow fails with 'Error: Dockerfile not found'. What is the likely cause?
medium
A. The Dockerfile is missing in the repository
B. The two 'run' commands are separate steps and do not share state
C. The image name 'myapp' is invalid
D. The 'docker run' command syntax is incorrect
Solution
Step 1: Understand docker build requirement
The 'docker build .' command requires a Dockerfile in the current directory (repo root after checkout).
Step 2: Identify the error cause
If the Dockerfile is missing from the repository, the build step fails with 'Dockerfile not found' error.
Final Answer:
The Dockerfile is missing in the repository -> Option A
Quick Check:
docker build fails = no Dockerfile [OK]
Hint: Ensure Dockerfile exists in repository root [OK]
Common Mistakes:
Thinking separate steps prevent image sharing
Assuming image name 'myapp' is invalid
Believing 'docker run' syntax causes the error
5. You want to create a GitHub Actions workflow that builds a Docker image only when a push happens to the 'main' branch and then pushes the image to Docker Hub. Which of these YAML snippets correctly sets the trigger and steps?
hard
A. on:
push:
tags:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: docker build -t myuser/myapp .
- run: docker push myuser/myapp
B. on:
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: docker build -t myapp .
- run: docker push myapp