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 -
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
$ 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=18.104.22.168 dns --=22.214.171.124 --add-host=gitlab_local_docker:126.96.36.199 jenkins/jenkins:latest"
Or, first, switch to user and run it from Docker:
$ su YOUR_USER
And here it is:
--hostname of jenkins.local
-p 8080:8080 -p 50000:50000
That’s all, “Run a Docker container from a user in Unix/Linux” is completed.