Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Become root in a docker container

Tags:

docker

I installed Oracle Database in a Docker container, but can't figure out how to become root. If I to this from the host

sudo docker exec -it -u 0 oracle18se /bin/bash

or

sudo docker exec -it --user root oracle18se /bin/bash

I get

OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "chdir to cwd (\"/home/oracle\") set in config.json failed: permission denied": unknown

If I do

sudo docker exec -it oracle18se /bin/bash

from the host, and then

su -

from the container, it asks the root password, but I do not know it.

Hy host OS is Ubuntu 18.04, link to docker file

EDIT1:

Found a Docker bug.

like image 633
Alexey Starinsky Avatar asked Dec 17 '19 14:12

Alexey Starinsky


People also ask

How do I become a root user in 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.

Can a container run as root?

Running a container as root means that the software packaged in a container is set to start as the root, or system administrator, user. This user is special in Linux systems, because it has all permissions needed to administer a system.

Should I run docker containers as root?

Running the container as root brings a lot of risks. Although being root inside the container is not the same as root on the host machine (some more details here) and you're able to deny a lot of capabilities during container startup, it is still the recommended approach to avoid being root .

Why containers should not run as root?

Root inside the container is unprivileged and has restricted capabilities. This prevents the container from using system administration commands unless you manually add capabilities or use privileged mode when you start your containers. Despite this mitigation, allowing applications to run as root remains a hazard.


2 Answers

You can exec into an existing container

docker exec -u root -it <container-id> /bin/bash

Output (as seen in Terminal):

root@<container-id>:/#

And to set root password use this:

Type the following command to become root user and issue passwd:

sudo -i
passwd

OR set a password for root user in a single go:

sudo passwd root

Test it your root password by typing the following command:

su -

like image 166
DUDANF Avatar answered Oct 13 '22 20:10

DUDANF


You can connect as root in docker container using:

docker exec -u 0 -it <container_id> /bin/bash
like image 38
banuj Avatar answered Oct 13 '22 20:10

banuj