0
0
Dockerdevops~10 mins

GitHub Actions with Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - GitHub Actions with Docker
Push code to GitHub
GitHub Actions triggered
Run workflow steps
Build Docker image
Run tests inside Docker container
Push Docker image to registry (optional)
Complete workflow
This flow shows how pushing code triggers GitHub Actions to build and test a Docker image, then optionally push it to a registry.
Execution Sample
Docker
name: Docker CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Docker image
        run: docker build -t myapp:latest .
      - name: Run tests
        run: docker run myapp:latest pytest
This GitHub Actions workflow builds a Docker image and runs tests inside the container on every push.
Process Table
StepActionCommand RunResultNext Step
1Trigger workflowPush code to GitHubWorkflow startsCheckout code
2Checkout codeactions/checkout@v3Code available on runnerBuild Docker image
3Build Docker imagedocker build -t myapp:latest .Docker image 'myapp:latest' createdRun tests in container
4Run testsdocker run myapp:latest pytestTests executed, results shownWorkflow complete
5Complete workflowN/AWorkflow ends successfullyEnd
💡 Workflow ends after tests run successfully or fails if any step errors
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
CodeNot checked outChecked out on runnerChecked out on runnerChecked out on runnerChecked out on runner
Docker ImageNot builtNot builtBuilt as 'myapp:latest'Built as 'myapp:latest'Built as 'myapp:latest'
Test StatusNot runNot runNot runTests passed or failedTests passed or failed
Key Moments - 3 Insights
Why do we need to checkout the code before building the Docker image?
The checkout step downloads the code to the runner so the docker build command can access the Dockerfile and source files. Without it, the build would fail (see execution_table step 2).
What happens if the tests inside the Docker container fail?
If tests fail during the 'docker run' step (step 4), the workflow stops and marks the run as failed. This prevents pushing broken code.
Can the Docker image be pushed to a registry in this workflow?
Yes, but this example does not include that step. You can add a step after tests to push the image if tests pass.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 3?
ACode is checked out on the runner
BDocker image 'myapp:latest' is created
CTests have been executed
DWorkflow ends successfully
💡 Hint
Check the 'Result' column for step 3 in the execution_table
At which step does the workflow run tests inside the Docker container?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the 'Run tests' action in the execution_table
If the checkout step is removed, what will happen during the Docker build?
ADocker build fails due to missing files
BDocker image builds successfully
CTests run without errors
DWorkflow completes without building
💡 Hint
Refer to key_moments about why checkout is needed before build
Concept Snapshot
GitHub Actions with Docker:
- Trigger on push event
- Checkout code to runner
- Build Docker image with 'docker build'
- Run tests inside container with 'docker run'
- Optionally push image to registry
- Workflow stops if any step fails
Full Transcript
This visual execution shows how GitHub Actions automates Docker workflows. When code is pushed, the workflow starts by checking out the code to the runner. Then it builds a Docker image using the Dockerfile in the repo. After building, it runs tests inside a container created from that image. If tests pass, the workflow completes successfully. If any step fails, the workflow stops and reports failure. This process helps automate testing and deployment using Docker containers in GitHub Actions.