0
0
Dockerdevops~5 mins

GitHub Actions with Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building and testing Docker containers automatically can be slow and error-prone if done manually. GitHub Actions lets you automate these steps so your Docker images are built and tested every time you update your code.
When you want to build a Docker image automatically after pushing code to GitHub.
When you want to run tests inside a Docker container on every code change.
When you want to push your Docker image to a registry like Docker Hub after a successful build.
When you want to ensure your Docker container works before deploying it.
When you want to save time by automating repetitive Docker build and test steps.
Config File - docker-image.yml
docker-image.yml
name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build Docker image
        run: docker build -t dockerhub-user/my-app:latest .

      - name: Push Docker image
        run: docker push dockerhub-user/my-app:latest

      - name: Run container to test
        run: |
          docker run --rm dockerhub-user/my-app:latest echo "Container runs successfully"

This GitHub Actions workflow file automates building and pushing a Docker image.

  • on.push.branches: Runs the workflow when code is pushed to the main branch.
  • jobs.build.runs-on: Uses an Ubuntu machine to run the steps.
  • actions/checkout@v3: Gets your code from GitHub.
  • docker/login-action@v2: Logs into Docker Hub using stored secrets.
  • docker build: Builds the Docker image with a tag.
  • docker push: Uploads the image to Docker Hub.
  • docker run: Runs the image to check it works.
Commands
Builds a Docker image from the current folder and tags it as dockerhub-user/my-app:latest.
Terminal
docker build -t dockerhub-user/my-app:latest .
Expected OutputExpected
Sending build context to Docker daemon 8.192kB Step 1/5 : FROM alpine:3.18 ---> a24bb4013296 Step 2/5 : COPY . /app ---> Using cache ---> 3c1a2f4b5d6e Step 3/5 : WORKDIR /app ---> Using cache ---> 7b9e8f1a2c3d Step 4/5 : RUN echo "Hello from Docker" ---> Running in 1a2b3c4d5e6f Hello from Docker Removing intermediate container 1a2b3c4d5e6f ---> 9f8e7d6c5b4a Step 5/5 : CMD ["echo", "Container runs successfully"] ---> Running in 7f6e5d4c3b2a Removing intermediate container 7f6e5d4c3b2a ---> 2a3b4c5d6e7f Successfully built 2a3b4c5d6e7f Successfully tagged dockerhub-user/my-app:latest
-t - Tags the image with a name and version
Uploads the built Docker image to Docker Hub so others can download it.
Terminal
docker push dockerhub-user/my-app:latest
Expected OutputExpected
The push refers to repository [docker.io/dockerhub-user/my-app] 123456789abc: Pushed latest: digest: sha256:abcdef1234567890 size: 1234
Runs the Docker image to check it works and prints a success message, then removes the container.
Terminal
docker run --rm dockerhub-user/my-app:latest echo "Container runs successfully"
Expected OutputExpected
Container runs successfully
--rm - Removes the container after it finishes running
Key Concept

If you remember nothing else from this pattern, remember: GitHub Actions can automatically build, test, and push your Docker images every time you update your code.

Common Mistakes
Not setting Docker Hub credentials in GitHub secrets and trying to push the image.
Without credentials, Docker cannot log in and push the image, causing the workflow to fail.
Store your Docker Hub username and access token in GitHub secrets and reference them in the workflow.
Forgetting to tag the Docker image before pushing.
Docker push requires a tagged image; without a tag, it won't know what to upload.
Use the -t flag with docker build to tag the image properly.
Running docker run without --rm flag during tests.
Containers remain after running, cluttering the system with stopped containers.
Add --rm to remove the container automatically after the test run.
Summary
Create a GitHub Actions workflow file to automate Docker image build and push.
Use docker build with a tag to create the image from your code.
Push the tagged image to Docker Hub using docker push.
Run the image with docker run to verify it works before deployment.