Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make docker COPY ignore file permissions?

I'm pulling a docker image from registry and I want to use it as cache, running locally docker build --cache-from MY_IMAGE .. It uses case up to the layer COPY requirements.txt ${SOME_DIR}. I suspect the problem is that docker looks at the file permissions, not just the file content. Is there any way to make it ignore the file permissions?

Thanks in advance! :)

PS I could probably use RUN, HEREDOC and pipes to avoid using COPY, but it would look really awful.

Regards

like image 663
pjboro Avatar asked Jul 02 '20 19:07

pjboro


People also ask

Does Docker copy overwrite existing file?

It seems that docker build won't overwrite a file it has previously copied. I have a dockerfile with several copy instructions, and files touched in earlier COPY directives don't get overwritten by later ones. After building this, $BASE/config/thatfile. yml contains the contents of file1.

Can we copy file to stopped container?

You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container. If - is specified for either the SRC_PATH or DEST_PATH , you can also stream a tar archive from STDIN or to STDOUT . The CONTAINER can be a running or stopped container.

What is .dockerignore used for?

The . dockerignore file is very similar to the . gitignore file in that it allows you to specify a list of files or directories that Docker is to ignore during the build process. This can come in really handy in certain instances.

How to ignore certain files and folders from your Docker images?

Here's how to ignore certain files and folders from your Docker images. This lets you copy in everything you want, while ignoring the cruft. In most cases, you’ll be copying in the source code of your application into a Docker image. Typically you would do that by adding COPY src/ dest/ or similar to your Dockerfile.

What is the default file permission for copying files in dockerfile?

Bookmark this question. Show activity on this post. Seems like a basic issue but couldnt find any answers so far .. When using ADD / COPY in Dockerfile and running the image on linux, the default file permission of the file copied in the image is 644.

How to reduce the size of a docker image?

Similar to a.gitignore file, a.Dockerignore files allows you to mention a list of files and/or directories which you might want to ignore while building the image. This would definitely reduce the size of the image and also help to speed up the docker build process.

How do I deploy a file to a docker container?

Reminder: Most files are deployed to a container through: (a) a COPY directive in dockerfile , (during the image build process) (b) through a docker cp command, (usually after a docker create command that creates but doesn’t start yet the container)


1 Answers

After reading the question again, I guess the cache invalidation is the problem here - COPY command is invalidating cache when something changes, and permissions are part of the meta information, which are included for this calculation.

Permissions are noted, because hashing is used to detect changes. There is related question for this: Why does my Docker cache get invalidated by this COPY command?

There is no way around this - just use COPY command at the last point you could if your file permissions are different based on upstream registry image.

Permissions during the copy operation itself are not derived from the source, instead by default "All new files and directories are created with a UID and GID of 0", as based on the official docs.

However, you can change owner while using the COPY command.

You can use --chown parameter, with the user:group syntax, or by using UID:GID values.

Note, that this works only with Linux based containers. On Windows machines different concepts with permissions have been used, and usage of /etc/groups or /etc/passwd is not possible.

like image 153
Niklas Avatar answered Sep 20 '22 11:09

Niklas