Skip to main content
In this guide, we’ll walk through a GitHub Actions workflow that builds a Docker image, validates it with runtime tests, and prepares it for publishing to Docker Hub.

1. Authenticate with Docker Hub

Before building your container, log in to Docker Hub securely using the official docker/login-action.
Store your Docker Hub credentials as GitHub Secrets or repository variables to avoid exposing sensitive data in your workflow YAML.

2. Build the Image for Testing

Use the docker/build-push-action to build your image locally without pushing it immediately. This allows you to run integration or health checks before publishing.
The image shows a webpage from GitHub Marketplace detailing sections on usage, examples, customizing, troubleshooting, and contributing for building and pushing Docker images.
For more customization options, refer to the official documentation: docker/build-push-action.

3. Define Your Dockerfile

Place a Dockerfile at the root of your repository to specify the container environment:
Key steps:
  • Start from an official Node.js Alpine base image.
  • Install dependencies before copying the rest of the source code.
  • Expose port 3000 and set npm start as the default command.

4. Run Container Tests

After building the image, verify that the application starts and responds to its health endpoint. Replace placeholders with your actual GitHub Secrets or repository variables.
Ensure that your application exposes health endpoints like /live or /ready to prevent false positives during automated testing.

5. Implement Health Endpoints in Your App

Add health-check routes in your Express application so the workflow can validate container readiness:

6. Monitor the Workflow in GitHub Actions

Once you push your changes, go to the Actions tab in your repository to review the workflow run.
The image shows a GitHub Actions page for a project called "Solar System Workflow," displaying a list of workflow runs with their statuses and timestamps.
Select the Containerization job to inspect its steps:
The image shows a GitHub Actions workflow interface for a project named "solar-system," displaying a "docker build and test" process in progress with unit testing and code coverage jobs.
Watch each step transition to success:
The image shows a GitHub Actions workflow interface with a successful "docker build and test" job, including steps like unit testing, code coverage, and containerization.
You can also inspect logs to confirm build arguments, container startup, and endpoint testing:
The image shows a GitHub Actions interface displaying a successful containerization job with details of Docker image testing, including repository names, tags, image IDs, creation times, and sizes.

With your image successfully built and validated, you’re now ready to push it to Docker Hub. Continue to the next article to configure the push step in your workflow.

Watch Video