<p>Solution is to use <code>exec</code> as:</p>
<pre><code>root@doge:~# 
root@doge:~# docker run -ti     -d     --name basexhttp     --publish 1984:1984     --publish 8984:8984     --volume &quot;$(pwd)/basex/data&quot;:/srv/basex/data     basex/basexhttp:latest
5699af1dc04aa9c78066222e4b0b8e5621aa2ab9dd2bd46bcd87c6a0377c1960
root@doge:~# 
root@doge:~# docker container list
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                      NAMES
5699af1dc04a        basex/basexhttp:latest   &quot;/usr/local/bin/mvn-…&quot;   33 seconds ago      Up 30 seconds       0.0.0.0:1984-&gt;1984/tcp, 0.0.0.0:8984-&gt;8984/tcp, 8985/tcp   basexhttp
root@doge:~# 
root@doge:~# docker exec -ti --user root basexhttp bash
bash-4.4# 
bash-4.4# whoami
root
bash-4.4# 
bash-4.4# su basex
~ $ 
~ $ whoami
basex
~ $ 
~ $ exit
bash-4.4# exit
exit
root@doge:~# 
root@doge:~# docker container list
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                                      NAMES
5699af1dc04a        basex/basexhttp:latest   &quot;/usr/local/bin/mvn-…&quot;   About a minute ago   Up About a minute   0.0.0.0:1984-&gt;1984/tcp, 0.0.0.0:8984-&gt;8984/tcp, 8985/tcp   basexhttp
root@doge:~# 
root@doge:~# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                                      NAMES
5699af1dc04a        basex/basexhttp:latest   &quot;/usr/local/bin/mvn-…&quot;   About a minute ago   Up About a minute   0.0.0.0:1984-&gt;1984/tcp, 0.0.0.0:8984-&gt;8984/tcp, 8985/tcp   basexhttp
root@doge:~# 
</code></pre>

CC BY-SA 4.0
2020-09-04T19:19:58.383Z
You should combine both your answers and accept the resulting one. Otherwise it's a bit confusing.
Konrad Botor
CC BY-SA 4.0
2020-09-04T08:28:23.713Z
2020-09-04T08:28:23.713Z
Nicholas Saunders
<p>Able to get a <a href="https://devops.stackexchange.com/q/12344/23443">response</a> from the docker hub <code>basex</code> container:</p>
<pre><code>nicholas $ 
nicholas $ lynx http://localhost:8984/ --dump
No function found that matches the request.
nicholas $ 
</code></pre>
<p>Ran the container as:</p>
<pre><code>root $ 
root $ docker run -ti     -d     --name basexhttp     --publish 1984:1984     --publish 8984:8984     --volume &quot;$(pwd)/basex/data&quot;:/srv/basex/data     basex/basexhttp:latest
71cbb84cf53143d00e6009d1ea5caa9de96c094c5d6aefc11cad95188b976776
root $ 
root $ docker container list
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                      NAMES
71cbb84cf531        basex/basexhttp:latest   &quot;/usr/local/bin/mvn-…&quot;   26 seconds ago      Up 23 seconds       0.0.0.0:1984-&gt;1984/tcp, 0.0.0.0:8984-&gt;8984/tcp, 8985/tcp   basexhttp
root $ 
root $ docker run -ti basex/basexhttp:latest bash
bash-4.4$ 
bash-4.4$ whoami
basex
bash-4.4$ 
bash-4.4$ pwd
/srv
bash-4.4$ 
bash-4.4$ sudo su -
bash: sudo: command not found
bash-4.4$ 
bash-4.4$ su -
su: must be suid to work properly
bash-4.4$ 
</code></pre>
<p>but how do I gain root access?</p>

<docker><containers><databases><bash><images>
How to gain root access to an existing docker container?
Nicholas Saunders
# How to gain root access to an existing docker container?

(Asked by: username_0 on 2020-09-04)

Able to get a response from the docker hub basex container:
nicholas $ 
nicholas $ lynx http://localhost:8984/ --dump
No function found that matches the request.
nicholas $ 

Ran the container as:
root $ 
root $ docker run -ti     -d     --name basexhttp     --publish 1984:1984     --publish 8984:8984     --volume "$(pwd)/basex/data":/srv/basex/data     basex/basexhttp:latest
71cbb84cf53143d00e6009d1ea5caa9de96c094c5d6aefc11cad95188b976776
root $ 
root $ docker container list
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                      NAMES
71cbb84cf531        basex/basexhttp:latest   "/usr/local/bin/mvn-…"   26 seconds ago      Up 23 seconds       0.0.0.0:1984->1984/tcp, 0.0.0.0:8984->8984/tcp, 8985/tcp   basexhttp
root $ 
root $ docker run -ti basex/basexhttp:latest bash
bash-4.4$ 
bash-4.4$ whoami
basex
bash-4.4$ 
bash-4.4$ pwd
/srv
bash-4.4$ 
bash-4.4$ sudo su -
bash: sudo: command not found
bash-4.4$ 
bash-4.4$ su -
su: must be suid to work properly
bash-4.4$ 

but how do I gain root access?

**Tags:** docker, containers, databases, bash, images


---

## 1 answers

### Answer by username_0 on 2020-09-04. Score: 1

Solution is to use exec as:
root@doge:~# 
root@doge:~# docker run -ti     -d     --name basexhttp     --publish 1984:1984     --publish 8984:8984     --volume "$(pwd)/basex/data":/srv/basex/data     basex/basexhttp:latest
5699af1dc04aa9c78066222e4b0b8e5621aa2ab9dd2bd46bcd87c6a0377c1960
root@doge:~# 
root@doge:~# docker container list
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                      NAMES
5699af1dc04a        basex/basexhttp:latest   "/usr/local/bin/mvn-…"   33 seconds ago      Up 30 seconds       0.0.0.0:1984->1984/tcp, 0.0.0.0:8984->8984/tcp, 8985/tcp   basexhttp
root@doge:~# 
root@doge:~# docker exec -ti --user root basexhttp bash
bash-4.4# 
bash-4.4# whoami
root
bash-4.4# 
bash-4.4# su basex
~ $ 
~ $ whoami
basex
~ $ 
~ $ exit
bash-4.4# exit
exit
root@doge:~# 
root@doge:~# docker container list
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                                      NAMES
5699af1dc04a        basex/basexhttp:latest   "/usr/local/bin/mvn-…"   About a minute ago   Up About a minute   0.0.0.0:1984->1984/tcp, 0.0.0.0:8984->8984/tcp, 8985/tcp   basexhttp
root@doge:~# 
root@doge:~# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                                      NAMES
5699af1dc04a        basex/basexhttp:latest   "/usr/local/bin/mvn-…"   About a minute ago   Up About a minute   0.0.0.0:1984->1984/tcp, 0.0.0.0:8984->8984/tcp, 8985/tcp   basexhttp
root@doge:~#

> **Comments:**
> **username_1**: You should combine both your answers and accept the resulting one. Otherwise it's a bit confusing.  

---

