Docker, Linux: Disable Docker commands for a user but allow him running a Docker container
A tutorial of how to create an environment with Docker commands disabled for a particular user, but with a possibility for this user to run a Docker container on a Linux (CentOS8) machine.
The solution is based on usage of a restricted shell with modifications disabling some of well-known possibilities to break out from it.
- Create a user with restrictions:
sudo adduser ruser
- Set a password for ruser(type the password twice):
sudo passwd ruser
- Create a restricted shell:
sudo cp /bin/bash /bin/rbash
- Modify rusersettings. Force him to use restricted shell:
sudo usermod -s /bin/rbash ruser
- Verify correctness of rusersettings in /etc/passwd:
sudo cat /etc/passwd
There should be a line containing:
ruser:...:/home/ruser:/bin/rbash
- Create a directory which will contain commands available for ruser:
mkdir /home/ruser/commands
- Modify PATHenvironment variable inruser’s.bash_profile:
vi /home/ruser/.bash_profile
Add the following line to the bottom of the file:
export PATH=$HOME/commands
- Log in as ruser(or execute asu):
su - ruser
- Verify that the majority of commands are not available for ruser:
cd
as the shell is restricted, outputs:
-rbash: cd: restricted
ls
as ls is absent in /home/ruser/commands, outputs:
-rbash: ls: command not found
docker
just like for ls, due to the fact that docker is absent from /home/ruser/commands, outputs:
-rbash: docker: command not found
rbash and vi commands do not work either:
-rbash: rbash: command not found
-rbash: vi: command not found
trying to modify PATH variable does not work:
export PATH=/bin
outputs:
-rbash: PATH: readonly variable
- 
Login as a user with sudo privileges 
- 
Create a script allowing ruserto run a Docker container:
vi /home/ruser/commands/runContainer
Add the following code to the runContainer file:
#!/bin/bash
export PATH=/bin
docker run -it someImageID
- Add rusertodockergroup:
sudo usermod -aG docker ruser
- 
Log in as ruser
- 
Execute runContainercommand:
runContainer
Observe that the interactive bash shell has been executed on the container.
- Exit from the container:
exit
- Verify that the PATHcontains/home/ruser/commands:
echo $PATH
- Verify that the dockercommand in therunContainerfile cannot be executed:
docker run -it someImageID
outputs:
-rbash: docker: command not found
In case you have a question or a comment concerning this tutorial, please send it to: [email protected].
