To run a Docker container from a user in Unix/Linux

The container will always run as root, but it so happens that in the Dockerfile prescribed user to work inside of the container. And then when you attempt to use another user, give a password, which I did not know, for example:

$ docker exec-ti jenkins /bin/bash
[email protected]:/$ su -
Password:

As you can see from the above command, I ran Jenkins. The container has a user (Jenkins too), but I don’t know the password. The solution is to use a parameter which will set the desired user at the start of the container:

$ docker exec -it-u your_container_user your_conatainer container_command

Or:

$ docker exec -it --user your_container_user your_conatainer container_command 

Here is an example from Jenkins:

$ docker exec -it-u root jenkins /bin/bash

With this command, I connect to the container as root user.

Yes, but if there is a need to launch the container from another user, you should use:

$ sudo -u your_user "docker run-d --name jenkins --hostname of jenkins.local-p 8080:8080 -p 50000:50000 --restart always-v /usr/local/jenkins/data_2:/var/jenkins_home --dns=10.17.0.3 --dns=10.17.0.4 --dns=1.1.1.1 dns --=74.82.42.42 --add-host=gitlab_local_docker:172.168.0.66 jenkins/jenkins:latest"

Or, first, switch to user and run it from Docker:

$ su YOUR_USER 

And here it is:

docker run-d 
--name jenkins
--hostname of jenkins.local
-p 8080:8080 -p 50000:50000
--restart always
-v /usr/local/jenkins/data_2:/var/jenkins_home
--dns=10.17.0.3
--dns=10.17.0.4
--dns=1.1.1.1
--dns=74.82.42.42
--add-host=gitlab_local_docker:172.168.0.66
jenkins/jenkins:latest

That’s all, “Run a Docker container from a user in Unix/Linux” is completed.

Source: linux-notes.org

(Visited 41 times, 1 visits today)