Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker ADD --chown bug or feature?

I am having a problem adding a file to an image and setting ownership via --chown flag. Specifically, here is a dockerfile adding a simple text file:

FROM fedora:24

ARG user_name=slave
ARG user_uid=1000
ARG user_home=/home/$user_name/

RUN useradd -l -u ${user_uid} -ms /bin/bash $user_name

WORKDIR ${user_home}
USER ${user_name}
ADD --chown=1397765041:1397765041 test.txt ./
CMD ls -l

This results in expected ownership of text.txt as can be seen:

$ docker run --rm -it bm/tmp:latest
total 4
-rw-r--r-- 1 some_user 1397765041 6 Oct 21 20:00 test.txt

Cool. Now if I change test.txt to a tar file (for example boost_1_57_0.tar.bz2), and rebuild, this is what I get:

$ docker run --rm -it bm/tmp:latest
total 4
drwx------ 8 501 root 4096 Oct 31  2014 boost_1_57_0

Here is how I am building (probably doesn't matter tho):

docker build -t bm/tmp --build-arg user_name=some_user --build-arg user_uid=1397765041 .

As we can see, ownership is NOT as expected in this case. It seems the behavior of --chown differs from the two cases shown above. I know that ADD automatically extracts tars. I don't know how the ownership is being set in the case where the file is a tar file. Anyone?

like image 898
Perplexabot Avatar asked Oct 21 '19 20:10

Perplexabot


People also ask

What is difference between ADD and copy in Dockerfile?

COPY is a docker file command that copies files from a local source location to a destination in the Docker container. ADD command is used to copy files/directories into a Docker image. It only has only one assigned function. It can also copy files from a URL.

What is chown command in Docker?

The chown command adds another layer to the image without deleting the previous layers. That means, that the final container image contains both layers.

What is Docker Workdir?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

Where do you put Dockerignore?

Docker CLI will only look for . dockerignore file in the root directory of the context, if you have a monorepo of multiple packages, make sure . dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.


Video Answer


1 Answers

Unfortunately, ADD --chown only works for regular files. ADD with a tarball uses the ownership and permissions listed inside in tarball.

Workarounds:

  • Run tar yourself with --owner/--owner-map/--group/--group-map.
  • chown -R after the ADD.
like image 164
John Kugelman Avatar answered Nov 15 '22 04:11

John Kugelman