Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker replicate UID/GID in container from host

When creating Docker containers I keep running into the issue of the UID/GID not being reflected in the container (I realize this is by design). What I am looking for is a way to keep host permissions reasonable and / or to replicate the UID/GID from the host user / group accounts in my Docker container. For instance:

host -

woot4moo:x:504:504:woot4moo:/home/woot4moo:/bin/bash

I would like this same behavior in the Docker container. That being said, is this even the right way to do this type of thing? My belief is I could simply run:

useradd -u 504 -g 504 woot4moo

as part of my Dockerfile, but I am not sure if that is valid.

like image 445
Woot4Moo Avatar asked Sep 04 '15 11:09

Woot4Moo


People also ask

How do I copy a Docker container ID?

To summarize, follow these steps to copy a file from a Docker container to a host machine: Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container.

How do I run a container with a specific user?

To run a command as a different user inside your container, add the --user flag: docker exec --user guest container-name whoami.

How do I clone in Docker?

To 'clone' a container, you'll have to make an image of that container first, you can do so by "committing" the container. Docker will (by default) pause all processes running in the container during commit to preserve data-consistency. Commit my_container as an image called my_container_snapshot , and tag it yymmdd .


1 Answers

You wouldn't want to run that as part of the image build process (in your Dockerfile), because the host on which someone is running a container is often not the host on which you are building the image.

One way of solving this is passing in UID/GID information via environment variables:

docker run -e APP_UID=100 -e APP_GID=100 ...

And then have an ENTRYPOINT script that includes something like the following before running the CMD:

useradd -c 'container user' -u $APP_UID -g $APP_GID appuser
chown -R $APP_UID:$APP_GID /app/data
like image 126
larsks Avatar answered Sep 24 '22 15:09

larsks