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.
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.
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.
You can run a command within the container as root using --user root
. To get a shell:
docker exec -it --user root kong sh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With