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

2020-04-05 06:45发布

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?

标签: docker
2条回答
一夜七次
2楼-- · 2020-04-05 07:17

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.

查看更多
甜甜的少女心
3楼-- · 2020-04-05 07:33

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

查看更多
登录 后发表回答