Github actions: run multiple jobs in the same docker

I’m learning to deploy github actions to run multiple jobs with docker, and this is what I have so far:

github actions yml file is shown as follow. There are 2 jobs: job0 builds docker with Dockerfile0 and job1 builds docker with Dockerfile1.

# .github/workflows/main.yml
name: docker CI

on: push

jobs:
  job0:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Build and Run
      run: docker build . --file Dockerfile0 --tag job0

  job1:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Build and Run
      run: docker build . --file Dockerfile1 --tag job1

Dockerfile0 and Dockerfile1 share basically the same content, except for the argument in the last line:

FROM ubuntu:20.04

ADD . /docker_ci
RUN apt-get update -y
RUN apt-get install -y ... ...

WORKDIR /docker_ci
RUN python3 script.py <arg>

I wonder, can I build a docker for the 1st job, and then invoke multiple jobs execute command within the docker built from the 1st job? Thus I don’t have to keep multiple Dockerfile and save some docker building time.

It would be better to build my docker locally from Dockerfile so I hope to avoid using container from docker hub.

runs-for-docker-actions looks relevant but I have trouble finding example deploying action locally (without publishing).

I’m not exactly sure what you’re trying to do, do you want to use a container built in one job as the build environment (job container, even?) for later jobs?

One thing I can definitely say is that jobs in GitHub hosted runners each get a fresh VM, so if you build a container in one job and want to use it in another you have to push it to a registry, or store the image somewhere else.

1 Like

jobs in GitHub hosted runners each get a fresh VM

This is certainly something I didn’t know. Thanks, problem solved.

1 Like