Skip to main content
In this guide, you’ll learn how to build a Docker image and run container tests in a GitHub Actions workflow before publishing it to Docker Hub. This end-to-end CI setup ensures your application image is validated automatically on each commit.

Prerequisites

Ensure you have the following jobs configured in your workflow:
  • unit-testing
  • code-coverage
You’ll also need a step to authenticate with Docker Hub:

Using docker/build-push-action for Build & Test

We’ll leverage Docker’s official build-push-action to compile and test the image locally (push: false). This action supports multiple platforms and advanced build features via Buildx.
GitHub Marketplace page for docker/build-push-action showing usage, examples, customizing, troubleshooting, and contributing sections.
By setting push: false you prevent the image from being sent to a registry, enabling quick feedback on build and tests.

Workflow Snippet

Build & Test Action Summary

Dockerfile

Place this Dockerfile in your repository root to define the container:
This configuration:
  • Starts from the lightweight Node.js 18 Alpine image
  • Sets /usr/app as the working directory
  • Installs dependencies from package.json
  • Copies the rest of your application code
  • Defines default environment variables for MongoDB
  • Exposes port 3000 and launches the app using npm start

Application Health Endpoints

Ensure your Express app exposes a simple /live endpoint for the workflow test. Example in app.js:
Be sure your placeholder environment variables in the Dockerfile match those used in your test commands to avoid runtime errors.

Workflow Run Results

Once the workflow is committed, GitHub Actions will execute the build-and-test job. You’ll see output like this:
GitHub Actions page showing the "Solar System Workflow" runs with statuses and timestamps.
GitHub Actions interface for the "docker build and test" job running under the "solar-system" workflow, showing progress and success of each step.
Example build logs:
And container test output:
Since all steps pass, your Docker image is successfully built and tested locally. Next up: pushing it to Docker Hub and deploying!

Watch Video