Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker : execute commands as a non-root user

Earlier I used to run with the following command :

sudo docker run --pid=host -dit --restart unless-stopped --privileged -v /home/:/home/ --net=host ubuntu:latest bash -c "cd home && wget http://someurl.com/a.js -O /home/a.js && node a.js && tail -F anything"

This command would launch the container having a root user by default. So the wget http://someurl.com/a.js -O /home/a.js command used to work without any issues.

Now I want to get sound from the container. To make sure that the container and the host plays audio simultaneously I used pulseaudio socket otherwise I used to get device busy error as alsa captures the sound card.

Here is the new command I used to accomplish my requirement:

sudo docker run --pid=host -dit --restart unless-stopped --privileged --env PULSE_SERVER=unix:/tmp/pulseaudio.socket --env PULSE_COOKIE=/home/$USER/pulseaudio.cookie --volume /tmp/pulseaudio.socket:/tmp/pulseaudio.socket --volume /home/$USER/pulseaudio.client.conf:/etc/pulse/client.conf --user $(id -u):$(id -g) -v /home/:/home/ --net=host ubuntu:latest bash -c "cd home && wget http://someurl.com/a.js -O /home/a.js && node a.js && tail -F anything"

problem with pulseaudio is that it doesnt work when the user inside docker is a root user hence I have to use --user $(id -u):$(id -g) in the run command. Now since the user is not root, the wget http://someurl.com/a.js -O /home/a.js command gives permission denied error.

I want this wget command to execute whenever I start my container. I want to run the container as non-root as well as be able to execute the wget command also.

Is there any workaround for this?

like image 966
node_man Avatar asked Aug 27 '26 17:08

node_man


1 Answers

1- Execute docker command with non-root user

If this is your case and don't want to run docker command with root user, follow this link . create a docker group and add your current user to it.

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

2- Execute commands inside docker! with non-root user

If I'm right you want to use a non-root user inside docker not the root!

The uid given to your user in the docker is related to the root docker images you are using, for example alphine or ubuntu:xenial as mentioned in this article

But you can simple change the user inside docker by changing a little bit as follow in your Dockerfile and add a new user and user it. like this:

 RUN adduser -D myuser
 USER myuser
 ENTRYPOINT [“sleep”]
 CMD [“1000”]

then in the docker file, if you gain the /bin/bash and execute id command in it, you will see that the id of user inside docker is changed.

Update:

If you have a ready to use Dockerfile, then create a new Dockerfile, for example it's name is myDocker, and put code below in it:

 from myDockerfile
 RUN adduser -D myuser
 USER myuser
 ENTRYPOINT [“sleep”]
 CMD [“1000”]

then save this file,and build it:

$ docker build -t myNewDocker .
$ docker run myNewDocker <with your options>
like image 126
Reza Torkaman Ahmadi Avatar answered Aug 30 '26 12:08

Reza Torkaman Ahmadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!