GitHub Actions with Docker - Time & Space Complexity
When using GitHub Actions with Docker, it's important to understand how the time to run your workflows grows as you add more steps or build larger images.
We want to see how the execution time changes when the input, like the number of Docker build steps, increases.
Analyze the time complexity of the following GitHub Actions workflow snippet that builds a Docker image.
name: Build Docker Image
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: docker build -t my-app .
This workflow checks out the code and builds a Docker image from the Dockerfile in the repository.
Look for repeated actions or loops that affect time.
- Primary operation: Docker build processes each instruction in the Dockerfile sequentially.
- How many times: Once per build, but the number of instructions in the Dockerfile determines how many steps Docker runs.
The time to build grows roughly with the number of instructions in the Dockerfile.
| Input Size (n = Dockerfile instructions) | Approx. Operations (build steps) |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The build time increases linearly as you add more instructions.
Time Complexity: O(n)
This means the build time grows directly in proportion to the number of Dockerfile instructions.
[X] Wrong: "Adding more Dockerfile instructions won't affect build time much because Docker caches layers."
[OK] Correct: While caching helps, the first build or changes to instructions still require processing each step, so more instructions mean more work.
Understanding how build time scales with Dockerfile size shows you can reason about workflow efficiency and resource use, a useful skill in real projects.
What if we split the Docker build into multiple smaller images and build them separately? How would the time complexity change?
Practice
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 AQuick Check:
Automation with Docker = D [OK]
- Thinking GitHub Actions replace Docker itself
- Confusing manual commands with automation
- Assuming Dockerfiles are written by GitHub Actions
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 DQuick Check:
Build image command = C [OK]
- Using 'docker create' instead of 'docker build'
- Confusing 'docker start' with building images
- Using 'docker push' to build images
run: | docker build -t test-image . docker run --rm test-image echo HelloWhat will be the output when this step runs successfully?
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 BQuick Check:
docker run echo output = Hello [OK]
- Thinking build command outputs 'test-image'
- Expecting build success message as output
- Assuming image not found error without build
run: docker build -t myapp . run: docker run myappThe workflow fails with 'Error: Dockerfile not found'. What is the likely cause?
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 AQuick Check:
docker build fails = no Dockerfile [OK]
- Thinking separate steps prevent image sharing
- Assuming image name 'myapp' is invalid
- Believing 'docker run' syntax causes the error
Solution
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.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.Final Answer:
The snippet that triggers on push to main, checks out code, logs in with secrets, builds tagged image, and pushes -> Option CQuick Check:
Push on main + login + build + push = A [OK]
- Using pull_request instead of push event
- Triggering on tags instead of branches
- Pushing without Docker login step
Ready to prove it?
Pass the test and this pattern moves unknown
