Cannot get into a container namespace with bin/bash

2.8k Views Asked by At

In one terminal I have a running container

docker container run --rm -it bash

In another terminal I want to run bin/bash in the same namespace as the running container above. For that to happen I followed these steps:

Grab the PID of the running container

docker inspect --format {{.State.Pid}} 32d7a757bc05

Let say the PID is 3386. When I run

sudo nsenter --target 3386 --mount --uts --ipc --net --pid bash

I get this error

nsenter: failed to execute bash: No such file or directory

But if I change bash to sh as below it works

sudo nsenter --target 3386 --mount --uts --ipc --net --pid sh

I'm on Centos 7, docker version 20.10.6 and as you have noticed I'm running my container from bash image. I cannot understand why bash is not working. Can someone please explain this?

Update

Just giving a little bit more background. I'm running my Centos as a Vagrant VM. I used vagrant ssh to connect to both terminals.

Vagrantfile

Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provider "hyperv"
end
1

There are 1 best solutions below

2
On

What you described actually worked for me:

sudo docker container run --rm -it bash

In a second terminal:

sudo nsenter --target 604861 --mount --uts --ipc --net --pid bash
bash-5.1# which bash
/usr/local/bin/bash

where I found the PID the way you described, using docker inspect --format {{.State.Pid}} CONTAINERID.