0

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
Vahid
  • 1,625
  • 1
  • 18
  • 33

1 Answers1

0

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.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
  • 1
    Thanks for testing. I added an `update` section to my question above. This is to highlight possible differences between yours and mine and to clarify more how I've set up my env. – Vahid May 26 '21 at 03:15
  • When I ran `sudo nsenter -t 3386 -m /usr/local/bin/bash` it worked. I still cannot explain why it is not working with just `bash` at the end. As I mentioned before `sh` works I don't have to change it to `/bin/sh`. – Vahid May 26 '21 at 04:59