How to Cache Portion of Docker Build

I’ve got a Circle implementation which resembles the following:

# Customize the test machine
machine:
    # Use the docker service
    services:
      - docker
    # Set environment variables
    environment:
      FLASK_CONFIG: test

# Customize dependencies
dependencies:
  override:
    - docker info
    - docker build -t my_image .

# Customize test commands
test:
  override:
    - >-
      docker run --network=host
      -e FLASK_CONFIG=$FLASK_CONFIG
      --entrypoint python my_image:latest -m unittest discover -v -s test

And my Dockerfile resembles:

FROM ubuntu:16.04

# Update the default application repository sources list
RUN apt-get update && apt-get install -y \
  python2.7 \
  python-pip \
  python-dev \
  build-essential \
  libpq-dev \
  libsasl2-dev

# Upgrade pip
RUN pip install --upgrade pip

# Copy application source code to container
COPY . /app
WORKDIR /app

# Install Python deps
RUN pip install -r requirements.txt

# Instruct container to listen on port 500
EXPOSE 5000

# Set the container entrypoint
ENTRYPOINT ["gunicorn", "--config", "/app/config/gunicorn.py", "--access-logfile", "-", "--error-logfile", "-", "app:app"]

Each time I do a build, these two steps take a long time to run:

  • RUN apt-get update && apt-get install dep1, dep2, etc
  • RUN pip install -r requirements.txt

Is it possible to cache these portions only to speed up the total build-time of the container?

I think you can greatly improve the overall performance of the build if you look into 2.0 executorType: docker platform. This would require you, though, to build images on your side and push them, say, to dockerhub.