dockerdocker-composedockerfiletmpfs

Faster Dockerfile build using ramdisk


We run a build process that compiles multiple artifacts from a large codebase.

On a decent spec i7 with SSDs this takes ~20 minutes, however when I shift to using tmpfs time drops to ~3 minutes.

We are packaging the build process using docker-compose to ensure all developers get an identical build environment, but also want them to be able to run the project using artifacts in the Docker cache if there are no changes. The perceived build time is an issue, but we're also concerned about wearing out the disk drives because the process write > 1Gb on every build.

Is there a way to mount tmpfs in Dockerfile 'RUN' commands (NOT the run container phase)?

I have tried to use mount (below) but get a permission denied:

RUN mkdir -p /core_src && mount -t tmpfs /dev/foo /core_src

Solution

  • The experimental frontend to buildkit allows tmpfs mounts during a run step.

    Your Dockerfile would look like:

    # syntax=docker/dockerfile:experimental
    FROM your_base_image
    RUN --mount=type=tmpfs,target=/core_src compile_command_here
    

    Then to enable buildkit with compose, you can set two environment variables:

    export DOCKER_BUILDKIT=1 # or configure in daemon.json
    export COMPOSE_DOCKER_CLI_BUILD=1
    

    Enabling buildkit in /etc/docker/daemon.json looks like:

    { "features": { "buildkit": true } }
    

    And then dockerd needs to be reloaded to use this (systemctl reload docker).