Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker COPY Host ${HOME}/.cache to container

Tags:

docker

I would like to copy my winetricks cache over to the docker container:

HOST:

~/.cache/winetricks

to CONTAINER

/home/myUser/.cache/winetricks.

My current approach is to create a copy of the cache in the docker root and use COPY to move the cache over to the container. This will make the cache available at build time.

I am using the approach to save build time. The Docker COPY command is commented out in production.

So here is my question: Why do I have to make a duplicate of my ~/.cache directory? Why can I not copy a directory from outside of the docker root to the container?

like image 886
user8162 Avatar asked Feb 23 '18 17:02

user8162


People also ask

How do I copy a docker image to a config file?

You should use the ADD instruction in your Dockerfile to copy the config file into the container. Note: generally, it is better to use COPY instead of ADD unless you specifically need how ADD handles tarballs. See this answer and Dockerfile best practices.

Where is docker build cache stored?

The cache uses the same storage driver as used for image layers. Metadata is stored in databases at /var/lib/docker/buildkit . When using overlay2 driver the layer itself is in /var/lib/docker/overlay2/<ID>/diff/ . For <ID> , see below. /var/lib/docker can vary depending on data-root in your dockerd configuration.


2 Answers

So here is my question: Why do I have to make a duplicate of my ~/.cache directory? Why can I not copy a directory from outside of the docker root to the container?

The first step of a docker build command is to send the build context to the docker engine performing the build. This engine may be on a remote server. This build context is typically a . at the end of the command line indicating to send the current directory as your context.

This context is used for every COPY and ADD command, and any file not included in the context is unavailable for the COPY and ADD. Changing the behavior to allow all files on the host to be accessible would break the client/server design of docker builds and introduce a security vulnerability where someone could send a malicious Dockerfile to the build server and use that to extract secret data from the server into the image.

You can change the build context to be your home directory, instead of your project sub-directory. To do this, you'd also need to update all the COPY and ADD commands with the path relative to $HOME. You would also see a significantly longer build time as your entire home directory gets sent to the server.

For your specific issue, there's a new feature that just entered into experimental called BuildKit. One of the first features being implemented is mounting a directory during a RUN command for the purposes of a packaging cache you only want to pull once.

like image 145
BMitch Avatar answered Oct 22 '22 00:10

BMitch


https://docs.docker.com/storage/volumes/#choose-the--v-or-mount-flag

@Charles Duffy is right.

dockerfile below

VOLUME ["(change-to-full-path)/.cache/winetrick"]

lanch value below

-v (change-to-full-path)/.cache/winetricks:/home/myUser/.cache/winetricks

This will allow you to set a volume, and then path it into the container

like image 44
ajankuv Avatar answered Oct 22 '22 00:10

ajankuv