Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run bash as user root on alpine images with docker? su: must be suid to work properly

Edit: The answer is so clear. One may use the flag --user root when entering the container.

docker exec -it --user root mycontainername bash                 or sh

I just downloaded this official docker hub's 1.5.0-alpine image for a service (Kong API Gateway) and now I can not run apk commands to install nano, for instance.

Before, I just had to enter the container

docker exec -it kong sh 

or

docker-compose exec kong sh

and I was able to run commands like apk update or apk add nano, for instance.

But now I get these errors

$ apk update                                                                                                                                   
ERROR: Unable to lock database: Permission denied                                                                                                
ERROR: Failed to open apk database: Permission denied

$ apk add nano
ERROR: Unable to lock database: Permission denied
ERROR: Failed to open apk database: Permission denied

I also tried to run sudo and su... but I got

$ su
su: must be suid to work properly
$ su root
su: must be suid to work properly
$ suid
sh: suid: not found

Will I really need to build my own custom image? I was using the official one and it was working fine.

like image 573
ofundefined Avatar asked May 08 '20 16:05

ofundefined


People also ask

Does Docker alpine have bash?

By default, bash is not included with BusyBox and Alpine Linux. The postmarketOS project, which is designed to run on mobile devices, is based on Alpine Linux. Many Docker images are also based upon Alpine, and you may install bash shell in Docker-based images too.

How do I get root permission in a Docker container?

As an alternative, we can also access the Docker container as root. In this case, we'll use the nsenter command to access the Docker container. To use the nsenter command, we must know the PID of the running container. This allows us to access the Docker container as a root user and run any command to access any file.


1 Answers

You can run a command within the container as root using --user root. To get a shell:

docker exec -it --user root kong sh
like image 171
chash Avatar answered Sep 18 '22 20:09

chash