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

ofundefined picture ofundefined · May 8, 2020 · Viewed 13.4k times · Source

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.

Answer

chash picture chash · May 8, 2020

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

docker exec -it --user root kong sh