Different file owner inside Docker container and in host machine

0 votes

I am having a trouble with Docker users. I run my container as gitlab-runner using the following command:

docker run --name testes_cashlink --hostname testes_cashlink --user gitlab-runner --privileged -t -v $DIR_TESTES:/cashlink_host -v $DIR_BATS:/bats -v $DIR_PROJETO:/cashlink_war docker-cashlink-ci /bats/run.sh

The files created inside the docker container show owner gitlab-runner, however, the same files show in my host machine as owner roggerfernandes. gitlab-runner must be the owner of files created inside Docker container as well as host machine.

Can anyone help me with this.

Jun 27, 2018 in Docker by shubham
• 7,340 points
1,934 views

1 answer to this question.

0 votes

Here is what you can try. Since file systems, at least in Unix- and Linux-like systems (including macOS), file owners are a number, not a name. Various tools such as ls will translate the number into a name for convenience, but it is still just a number. Your user gitlab-runner in the container, and the user roggerfernandes on the host system, have the same UID. You can find the numeric ID by running the id command.

Here it is on my laptop (reformatted a bit for readability):

$ id
uid=501(dan) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),
  79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),501(access_bpf),
  33(_appstore),100(_lpoperator),204(_developer),395(com.apple.access_ftp),
  398(com.apple.access_screensharing),399(com.apple.access_ssh)

Here you see at the beginning my UID is 501.

You can also run this command with a username, e.g. id gitlab-runner inside the container.

docker exec testes_cashlink id gitlab-runner

So when the user in the container owns a file, it is stored as a numeric ID (quite likely 1000, a common default). When you look on your host system, the mechanism that translates the number into a username just has a different username in its result than you would see inside the container.

If you need a specific user ID inside the container, you need to modify your Dockerfile so that when creating the user, you specify its uid. For example:

RUN useradd -u 1005 <other options> gitlab-runner
I hope the above information would be helpful for you.
answered Jun 27, 2018 by Atul
• 10,240 points

Related Questions In Docker

0 votes
1 answer

Different file owner inside Docker container and in host machine Ask

Filesystems, at least in Unix- and Linux-like ...READ MORE

answered Jun 25, 2018 in Docker by Damon Salvatore
• 5,980 points
943 views
+1 vote
2 answers

How do I copy a file from docker container to host?

Here is a way which you can ...READ MORE

answered Aug 28, 2018 in Docker by Damon Salvatore
• 5,980 points
28,575 views
0 votes
1 answer

How to create a container and run images in docker?

Look for what all images you have ...READ MORE

answered Feb 23, 2019 in Docker by Kalgi
• 52,350 points
2,216 views
0 votes
1 answer

How does save and export command work in docker and how are they different from one another?

Export : Export is used to persist a ...READ MORE

answered Jul 2, 2019 in Docker by Sirajul
• 59,230 points

edited Jul 2, 2019 by Sirajul 6,963 views
+2 votes
1 answer
0 votes
2 answers
+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,220 points

edited Oct 16, 2018 by lina 34,132 views
+1 vote
4 answers

COPY is not working in Docker

I had the exact same issue. I ...READ MORE

answered May 6, 2019 in Docker by Emilia
22,348 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP