Docker Community Forums

Share and learn in the Docker community.

Unable to execute commands on docker container attach

Hi,

I am learning docker basics right now and stuck on this one.

Version: 19.03.9
Linux ip.compute.internal 4.18.0-147.5.1.el8_1.x86_64 #1 SMP Tue Jan 14 15:50:19 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

I created a docker container:

docker container run -d -t --name t_d_container linux
fd91f24a79e007d3676b2ab2344b34765829b918672abf4c7995836ad68d0f35

Then executed below:

docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd91f24a79e0 oraclelinux “/bin/bash” 6 seconds ago Up 5 seconds t_d_container

Then below:

docker container attach t_d_container
[root@fd91f24a79e0 /]# exit

Now the shell doesnot output anything and just sits there for me to press CTRL+C. Why is that?

When i create another container :

docker container run -d -t --name t_d_container1 linux

and attach to it , run exit, it exits from the container and stops it. How can I keep the container running in the background in this scenario?

docker container attach t_d_container1
[root@dv92f24a79e0 /]# exit

We don’t know what image you use for your test, so let’s take Debian:

$ docker container run --rm -dit debian bash
16e27515288c4e...

Notice the -i flag. Connect to it:

$ docker container attach 16e2
root@16e27515288c:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@16e27515288c:/#

Detach from it with Ctrl-p Ctrl-q (that’s the command from the documentation, but in WSL2 I have to use Ctrl-p Ctrl-q Ctrl-c).
Check if it’s still running:

$ docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED        STATUS
16e27515288c   debian    "bash"    2 minutes ago  Up 2 minutes