GitHub Actions (S2E01) - Job / Service containers

Jeason JosephJeason Joseph
2 min read

Job container

GitHub action can be used for CI jobs, which involves setting up environment, downloading dependencies libraries etc. Instead suitable container images can be used to run the job and those steps can be skipped.

name: CI
on:
  push:
    branches: [ main ]
jobs:
  container-test-job:
    runs-on: ubuntu-latest
    container:
      image: node:18
      credentials:
        username: ${{ github.actor }}          # only for private registry
        password: ${{ secrets.github_token }}  # only for private registry
      env:                                    # Using environment variables with a container 
        NODE_ENV: development         
      ports:                                  # Exposing network ports on a container
        - 80
      volumes:                                # Mounting volumes in a container
        - my_docker_volume:/volume_mount      # <source on host>:<destination in container>
      options: --cpus 1
    steps:
      - name: Check for dockerenv file
        run: (ls /.dockerenv && echo Found dockerenv) || (echo No dockerenv)

Service container

A service container is a Docker container that provides supporting services for a job within a workflow, such as databases or caching system. Host services that your workflow needs, like databases (e.g., PostgreSQL, Redis), message brokers, or other supporting applications.

jobs:
  build:
    container:
      image: node:18
    services:
      redis-svc:
        # Docker Hub image
        image: redis
        ports:
          - 6379:6380    #Maps TCP port 6379 in the container to port 6380 on the Docker host.
#The above step is needed if the job is running on the runner & not in container.
      db:
        # Private registry image
        image: ghcr.io/octocat/testdb:latest
        credentials:
          username: ${{ github.repository_owner }}
          password: ${{ secrets.ghcr_password }}
  1. When running jobs directly on the runner machine & not as a container-job, you can access service containers using localhost:<port> or 127.0.0.1:<port> by means of port mapping.

  2. If your job runs in a Docker container(container-job), no need to map ports on the host or the service container. The service can be accessed using the configured label. redis-svc:<port>

1
Subscribe to my newsletter

Read articles from Jeason Joseph directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jeason Joseph
Jeason Joseph

I am an DevOps Engineer working since 2018. Had the opportunity to provide consulting services to many IT service companies. Passionate about learning new technology.