Bird
Raised Fist0
Dockerdevops~20 mins

GitHub Actions with Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker GitHub Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Docker build in GitHub Actions
You have this GitHub Actions step to build a Docker image:
      - name: Build Docker Image
        run: docker build -t myapp:latest .

What will be the output if the Dockerfile is missing in the repository root?
AError: Cannot locate Dockerfile: No such file or directory
BSuccessfully builds the image with default settings
CBuild completes but image is empty
DWarning: Dockerfile not found, using default base image
Attempts:
2 left
💡 Hint
Docker build requires a Dockerfile in the specified context directory.
🔀 Workflow
intermediate
2:00remaining
Correct GitHub Actions step to push Docker image
Which GitHub Actions step correctly logs in to Docker Hub and pushes an image named 'myapp:latest'?
A
- name: Push Docker Image
  run: |
    docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
    docker push myapp:latest
B
- name: Push Docker Image
  run: |
    docker push myapp:latest
    docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
C
- name: Push Docker Image
  run: |
    docker login ${{ secrets.DOCKER_USER }} ${{ secrets.DOCKER_PASS }}
    docker push myapp:latest
D
- name: Push Docker Image
  run: |
    docker push myapp:latest
Attempts:
2 left
💡 Hint
Docker login must happen before pushing the image.
❓ Configuration
advanced
2:00remaining
GitHub Actions Docker cache usage
You want to speed up Docker builds in GitHub Actions by caching layers. Which configuration snippet correctly uses the official Docker cache action?
A
- name: Cache Docker layers
  uses: docker/build-push-action@v4
  with:
    cache-from: type=local,src=/tmp/.buildx-cache
    cache-to: type=local,dest=/tmp/.buildx-cache
B
- name: Cache Docker layers
  uses: actions/cache@v3
  with:
    path: /tmp/.buildx-cache
    key: ${{ runner.os }}-buildx-${{ hashFiles('**/Dockerfile') }}
C
- name: Cache Docker layers
  uses: actions/cache@v3
  with:
    path: /var/lib/docker
    key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile') }}
D
- name: Cache Docker layers
  uses: actions/cache@v3
  with:
    path: /tmp/.buildx-cache
    key: ${{ runner.os }}-buildx-${{ github.sha }}
Attempts:
2 left
💡 Hint
Cache key should depend on Dockerfile content hash for cache invalidation.
❓ Troubleshoot
advanced
2:00remaining
Diagnosing Docker build failure in GitHub Actions
A GitHub Actions workflow fails with this error during docker build:
failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /github/workspace/Dockerfile: no such file or directory

What is the most likely cause?
AThe docker build command is missing the -t tag option
BDocker daemon is not running on the GitHub runner
CThe Dockerfile is named incorrectly or missing in the repository root
DThe GitHub Actions runner lacks permissions to run docker commands
Attempts:
2 left
💡 Hint
The error says it cannot open the Dockerfile file.
✅ Best Practice
expert
3:00remaining
Optimal multi-stage Docker build in GitHub Actions
You want to build a multi-stage Docker image in GitHub Actions to reduce final image size. Which Dockerfile snippet correctly implements a two-stage build for a Go application?
A
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY myapp /usr/local/bin/myapp
CMD ["myapp"]
B
FROM alpine:latest AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM golang:1.20
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]
C
FROM golang:1.20
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY /app/myapp /usr/local/bin/myapp
CMD ["myapp"]
D
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]
Attempts:
2 left
💡 Hint
The final stage should copy the binary from the builder stage using --from.

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

  1. Step 1: Understand GitHub Actions role

    GitHub Actions automates tasks like building and testing when code changes happen.
  2. Step 2: Connect automation with Docker

    Using GitHub Actions with Docker means automating Docker image builds and tests on events like pushes.
  3. Final Answer:

    To automate building and testing Docker images on code changes -> Option A
  4. 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

  1. Step 1: Recall Docker build command

    The command to build an image is 'docker build' with '-t' to tag the image.
  2. 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.
  3. Final Answer:

    run: docker build -t myimage . -> Option D
  4. Quick Check:

    Build image command = C [OK]
Hint: Use 'docker build -t name .' to build images [OK]
Common Mistakes:
  • Using 'docker create' instead of 'docker build'
  • Confusing 'docker start' with building images
  • Using 'docker push' to build images
3. Given this GitHub Actions workflow step:
run: |
  docker build -t test-image .
  docker run --rm test-image echo Hello
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

  1. 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'.
  2. Step 2: Identify the output of the run command

    The 'docker run' command runs 'echo Hello' inside the container, so the output is 'Hello'.
  3. Final Answer:

    Hello -> Option B
  4. 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

  1. Step 1: Understand docker build requirement

    The 'docker build .' command requires a Dockerfile in the current directory (repo root after checkout).
  2. Step 2: Identify the error cause

    If the Dockerfile is missing from the repository, the build step fails with 'Dockerfile not found' error.
  3. Final Answer:

    The Dockerfile is missing in the repository -> Option A
  4. 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
C. on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: docker build -t myuser/myapp:latest . - run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin - run: docker push myuser/myapp:latest
D. on: push: branches: - develop jobs: build: runs-on: ubuntu-latest steps: - run: docker build -t myuser/myapp . - run: docker push myuser/myapp

Solution

  1. Step 1: Check trigger event and branch

    on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: docker build -t myuser/myapp:latest . - run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin - run: docker push myuser/myapp:latest triggers on push to 'main' branch, which matches the requirement.
  2. Step 2: Verify build and push steps

    on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: docker build -t myuser/myapp:latest . - run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin - run: docker push myuser/myapp:latest includes checkout, build with proper tagging, login using secrets, and push commands correctly.
  3. Final Answer:

    The snippet that triggers on push to main, checks out code, logs in with secrets, builds tagged image, and pushes -> Option C
  4. Quick Check:

    Push on main + login + build + push = A [OK]
Hint: Use 'on: push: branches: - main' and login before push [OK]
Common Mistakes:
  • Using pull_request instead of push event
  • Triggering on tags instead of branches
  • Pushing without Docker login step
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard