Service containers are Docker containers that provide a portable way to host supporting services—like databases or caches—for your GitHub Actions workflows. They enable your jobs to spin up dependencies on demand, ensuring consistency across environments.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Table of Contents
- Basic Workflow Without Services
- How to Add a Service Container
- Running Jobs Inside a Container with Services
- Service Containers Overview
- Summary
- Links and References
Basic Workflow Without Services
This example workflow checks out code, installs Node.js, pulls dependencies, and runs tests without any external services.How to Add a Service Container
To include a service container—such as MongoDB—you define it under theservices key. Map the container port to a runner port, then connect to it via localhost.
- The GitHub runner uses Docker to start a MongoDB container named
mongodb-service. - Port
27017inside the container is exposed on port12345of the runner. - Tests connect to
localhost:12345to reach MongoDB.
Always make sure the port you map on the runner (e.g.,
12345) is not in use by other services.Running Jobs Inside a Container with Services
You can run the entire job in a container and still attach service containers alongside it. Below, a Node.js container runs the job, and MongoDB is available via hostname.- The job runs inside
ghcr.io/node-and-packages:20. mongodb-servicestarts on the same Docker network.- Hostname
mongodb-serviceresolves to the MongoDB container on port27017.
Service containers share a user-defined bridge network. Do not rely on
localhost inside the primary container; use the defined service hostname.Service Containers Overview
| Feature | Description | Example |
|---|---|---|
| Port Mapping | Expose container ports to the runner or other jobs. | ports: [ 12345:27017 ] |
| Hostname Resolution | Services are reachable by their service key. | MONGODB_HOST: mongodb-service |
| Custom Images | Use specific versions or private registry images. | image: myregistry/mongo:5.0 |
| Network Isolation | Runs in a user-defined bridge for security and speed. | Containers cannot access runner network unless mapped. |
Summary
Service containers in GitHub Actions allow you to:- Spin up databases, caches, or message queues alongside your workflows.
- Connect to services via mapped ports or hostnames.
- Run jobs directly on a runner or inside a dedicated container while still leveraging supporting services.