0
0
Dockerdevops~30 mins

GitHub Actions with Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
GitHub Actions with Docker
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Add a step with run: echo "Docker image built successfully" to show success.