Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set docker image username at container creation time?

I have an OpenSuse 42.3 docker image that I've configured to run a code. The image has a single user(other than root) called "myuser" that I create during the initial Image generation via the Dockerfile. I have three script files that generate a container from the image based on what operating system a user is on.

Question: Can the username "myuser" in the container be set to the username of the user that executes the container generation script?

My goal is to let a user pop into the container interactively and be able to run the code from within the container. The code is just a single binary that executes and has some IO, so I want the user's directory to be accessible from within the container so that they can navigate to a folder on their machine and run the code to generate output in their filesystem.

Below is what I have constructed so far. I tried setting the USER environment variable during the linux script's call to docker run, but that didn't change the user from "myuser" to say "bob" (the username on the host machine that started the container). The mounting of the directories seems to work fine. I'm not sure if it is even possible to achieve my goal.

Linux Container script:

username="$USER"
userID="$(id -u)"
groupID="$(id -g)"
home="${1:-$HOME}"

imageName="myImage:ImageTag"
containerName="version1Image"

docker run  -it -d --name ${containerName}  -u $userID:$groupID     \
            -e USER=${username} --workdir="/home/myuser"            \
            --volume="${home}:/home/myuser" ${imageName} /bin/bash  \

Mac Container script:

username="$USER"
userID="$(id -u)"
groupID="$(id -g)"
home="${1:-$HOME}"

imageName="myImage:ImageTag"
containerName="version1Image"

docker run  -it -d --name ${containerName}                          \
            --workdir="/home/myuser"            \
            --v="${home}:/home/myuser" ${imageName} /bin/bash  \

Windows Container script:

ECHO OFF
SET imageName="myImage:ImageTag"
SET containerName="version1Image"

docker run -it -d --name %containerName% --workdir="/home/myuser" -v="%USERPROFILE%:/home/myuser" %imageName% /bin/bash


echo "Container %containerName% was created."
echo "Run the ./startWindowsLociStream script to launch container"
like image 429
wandadars Avatar asked Aug 15 '17 15:08

wandadars


People also ask

How do I set the hostname for a container?

Basic idea is to use docker inspect to obtain the pid of the container, then enter the uts namespace of the container via nsenter . Running hostname inside that namespace will change the hostname for the docker instance that shares that namespace.

How do I run a docker container as 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.

Can we assign name to your container in docker?

When a container is created using Docker, Docker provides a way to name the container using the the --name <container_name> flag. However, if no name is provided by the user while creating/running a Docker container, Docker automatically assigns the container a name.

How do I pass a username and password in docker run?

Provide a password using STDIN To run the docker login command non-interactively, you can set the --password-stdin flag to provide a password through STDIN . Using STDIN prevents the password from ending up in the shell's history, or log-files.


1 Answers

Usernames are not important. What is important are the uid and gid values.

User myuser inside your container will have a uid of 1000 (first non-root user id). Thus when you start your container and look at the container process from the host machine, you will see that the container is owned by whatever user having a uid of 1000 on the host machine.

You can override this by specifying the user once you run your container using:

docker run --user 1001 ...

Therefore if you want the user inside the container, to be able to access files on the host machine owned by a user having a uid of 1005 say, just run the container using --user 1005.

To better understand how users map between the container and host take a look at this wonderful article. https://medium.com/@mccode/understanding-how-uid-and-gid-work-in-docker-containers-c37a01d01cf

like image 131
yamenk Avatar answered Sep 18 '22 13:09

yamenk