Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharable cache/host directory during both build and runtime contexts #1723

Open
JParkinson1991 opened this issue Oct 6, 2020 · 2 comments
Open

Comments

@JParkinson1991
Copy link

JParkinson1991 commented Oct 6, 2020

Aim

The ability to define and share a known location within the host during both build and runtime.

The rationale for this being to improve development experience. Many of us bind mount into containers during development so to be able to share package management caches etc during image builds and development workflows without the need to keep rebuilding etc images could improve outputs.

A diagram to explain, (albeit quite crude)

Screenshot 2020-10-06 at 15 14 40

Possible Solutions

Using this Dockerfile as an example

Important to note: Due to multi-stage and moving compiled assets into an alpine image, all caches created by composer are lost. This is fine, we want clean/slim builds in production, but during development those caches could speed things up.

# syntax = docker/dockerfile:1.0-experimental
ARG COMPOSER_VERSION=latest
FROM composer:${COMPOSER_VERSION} as composer

# Set the work dir to something reflective of the image
WORKDIR /code

# Add the composer manifests
COPY composer.json composer.json
COPY composer.lock composer.lock

# Build the source
# Using the cache mount.. here we define a hostPath, a known directory within the build context
RUN composer install -v

# Move final assets into streamlined alpine image
# Moving to scratch causes too many headaches..
FROM alpine:latest
WORKDIR /code
COPY --from=composer /code /code

Option 1 - Defining host path on cache mount

One option to achieve this could be to allow the definition of the hostPath when mounting cache.

Updating the above Dockerfiles RUN command

RUN --mount=type=cache,target=$COMPOSER_HOME/cache,hostPath=./dir-in-context \
    composer install -v;

Option 2 - Allowing bind mount to write to host

Another option could be allow bind mounts to write back to the host during image build. As far as im aware, currently you can files into an image during build with bind mounts, but images cannot push back onto the host.

Add a persist flag to bind mounts to allow 2-way writes, ie from host to image, from image to host

Updating the above Dockerfiles RUN command

RUN --mount=type=bind,source=./path-in-context,target=$COMPOSER_HOME/cache,readwrite,persist \
    composer install -v;

Usage at runtime

Having a known cache location on the host, in both options above ./dir-in-context, that same directory can be mounted into containers on runtime.

$ docker run --rm --volume=./dir-in-context:/tmp/cache composer:latest 

Runtime mounts can be 2-way so as the command updates the mounted directory, those new files can be pushed back into image builds and the cycle continues.

@JParkinson1991
Copy link
Author

Feature request only.

Apologies but i do not have the required skills in the go language to provide any implementation suggestions.

@tonistiigi
Copy link
Member

#1512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants