somecoolname
somecoolname

Reputation: 337

chown not working when coping a file in a dockerfile

I'm running docker engine on windows and am trying to add my own file to the image. Problem is that when I copy the file its ownership is always root:root but it needs to be heartbeat:heartbeat (exisitng user on image). Mounting a single file with the -v parameter und docker run doesn't seam to be possible on windows atm. Thats why I tried to create my own image with a docker file:

FROM docker.elastic.co/beats/heartbeat:7.16.3
USER root
COPY --chown=heartbeat:heartbeat yml/heartbeat.yml /usr/share/heartbeat/heartbeat.yml
RUN chown -R heartbeat:heartbeat /usr/share/heartbeat

The --chown parameter behind the coping does nothing. It is still root when I check and the RUN chown command results in a error. Here the output:

docker image build ./ -t custom/heartbeat:7.16.3
Sending build context to Docker daemon  10.75kB
Step 1/4 : FROM docker.elastic.co/beats/heartbeat:7.16.3
 ---> b64ad4b42006
Step 2/4 : USER root
 ---> Using cache
 ---> 922a9121e51b
Step 3/4 : COPY --chown=heartbeat:heartbeat yml/heartbeat.yml /usr/share/heartbeat/heartbeat.yml
 ---> Using cache
 ---> f30eb4934dca
Step 4/4 : RUN chown -R heartbeat:heartbeat /usr/share/heartbeat
 ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
 ---> Running in 2ae3bfdd5422
The command '/bin/sh -c chown -R heartbeat:heartbeat /usr/share/heartbeat' returned a non-zero code: 4294967295: failed to shutdown container: container 2ae3bfdd5422e81461a14896db0908e4cd67af1a6f99c629abff1e588f62fc32 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110): subsequent terminate failed container 2ae3bfdd5422e81461a14896db0908e4cd67af1a6f99c629abff1e588f62fc32 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110)

All help is welcome...

Running with --platform:

PS C:\SynteticMonitoring> docker image build ./ -t custom/heartbeat:7.16.3
Sending build context to Docker daemon  9.728kB
Step 1/4 : FROM --platform=linux/amd64 docker.elastic.co/beats/heartbeat:7.16.3
 ---> b64ad4b42006
Step 2/4 : USER root
 ---> Using cache
 ---> 922a9121e51b
Step 3/4 : COPY --chown=heartbeat:heartbeat yml/heartbeat.yml /usr/share/heartbeat/heartbeat.yml
 ---> Using cache
 ---> f30eb4934dca
Step 4/4 : RUN chmod +r /usr/share/heartbeat/heartbeat.yml
 ---> Using cache
 ---> e9a075d2ab53
Successfully built e9a075d2ab53
Successfully tagged custom/heartbeat:7.16.3
PS C:\SynteticMonitoring> docker run --interactive --tty --entrypoint /bin/sh custom/heartbeat:7.16.3
sh-4.2# ls -l
total 106916
-rw-r--r-- 1 root root     13675 Jan  7 00:47 LICENSE.txt
-rw-r--r-- 1 root root   1964303 Jan  7 00:47 NOTICE.txt
-rw-r--r-- 1 root root       851 Jan  7 00:47 README.md
drwxrwxr-x 2 root root      4096 Jan  7 00:48 data
-rw-r--r-- 1 root root    374197 Jan  7 00:47 fields.yml
-rwxr-xr-x 1 root root 107027952 Jan  7 00:47 heartbeat
-rw-r--r-- 1 root root     69196 Jan  7 00:47 heartbeat.reference.yml
-rw-rw-rw- 1 root root      1631 Jan 26 06:49 heartbeat.yml
drwxr-xr-x 2 root root      4096 Jan  7 00:47 kibana
drwxrwxr-x 2 root root      4096 Jan  7 00:48 logs
drwxr-xr-x 2 root root      4096 Jan  7 00:47 monitors.d
sh-4.2# pwd
/usr/share/heartbeat

Upvotes: 1

Views: 8544

Answers (2)

theUndying
theUndying

Reputation: 1744

You can't chown of a file to a user that does not exist. It seems that the heartbeat user and group do not exist in your base image.

That's why the COPY --chown does nothing and you get files owned by root.

You can fix this by creating the user before COPYing. To do this, add a line before your COPY statement, such as:

RUN addgroup heartbeat && adduser -S -H heartbeat -G heartbeat

If you don't have addgroup and adduser in your base image, try alternative:

RUN useradd -rUM -s /usr/sbin/nologin heartbeat

This will create the group and user heartbeat and then chown will be able to successfully change the ownership.

Upvotes: 3

zoot
zoot

Reputation: 226

According to Dockerfile documentation:

The optional --platform flag can be used to specify the platform of the image in case FROM references a multi-platform image. For example, linux/amd64, linux/arm64, or windows/amd64. By default, the target platform of the build request is used.

I suggest try something like:

FROM [--platform=<platform>] <image> [AS <name>]
FROM --platform=linux/amd64 docker.elastic.co/beats/heartbeat:7.16.3

Upvotes: 0

Related Questions