Skip to main content
In this tutorial, you’ll learn how to configure a GitHub Actions workflow that runs a code coverage job entirely inside a Docker container and connects to a MongoDB service container. This strategy isolates your test environment and streamlines networking between the job and its service.

Table of Contents

  1. Job Container Overview
  2. Adding a MongoDB Service Container
  3. Defining the Code Coverage Job
  4. Workflow Execution and Logs
  5. Links and References

1. Job Container Overview

Specifying a container at the job level ensures that all subsequent steps execute inside your chosen Docker image. You no longer need to manually install language runtimes or tools on the runner—the container already includes them. Basic syntax:
Define additional container properties under the container key:

2. Adding a MongoDB Service Container

Service containers run alongside your job container on a shared network. Each service is reachable by its service name without specifying ports. Example with PostgreSQL:
For MongoDB, you’ll configure the service similarly:
Service containers automatically join a user-defined network. You can ping them by service name (e.g., mongo) from within the job container without extra port mappings.

3. Defining the Code Coverage Job

Below is a complete code-coverage job that:
  • Runs inside the node:18 container
  • Spins up a MongoDB service container
  • Caches and installs dependencies
  • Executes the coverage script
  • Archives the test reports

4. Workflow Execution and Logs

When this workflow runs, GitHub Actions will:
  1. Pull and start the node:18 job container.
  2. Pull and start the MongoDB service container (siddharth67/mongo-db:non-prod).
  3. Automatically join both containers on a shared bridge network.
In the Run coverage step, your script can connect to MongoDB at mongodb://mongo:27017/superData using the provided credentials.
The image shows a GitHub Actions workflow interface with jobs for unit testing and code coverage, indicating successful completion of tasks like initializing containers and starting a MongoDB service container.
Behind the scenes, the containers use their service label (mongo) as the hostname, eliminating explicit port mappings.

Watch Video