1

I am learning docker basics right now and stuck on this one: I created a docker container:

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



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

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

But when I exit from the container it keeps running and waiting for me to give an input. So i have to do CTRL+C everytime.

Then, I tried

docker container exec -it t_d_container /bin/bash

And as expected, there are 2 processes running for bash now:

[root@fd91f24a79e0 /]# ps -ef

UID        PID  PPID  C STIME TTY          TIME CMD

root         1     0  0 06:49 pts/0    00:00:00 /bin/bash

root        27     0  0 07:08 pts/1    00:00:00 /bin/bash

Is there any other way to exit from a container running in the background?

I dig in further. After I attach to the container, I am unable to execute any commands on the attached container.

 docker container attach ac9d50c03304
 [root@ds001 /]# hostname

It just sits there waiting for me to press CTRL+C. DockerVersion:19.03.9

cool_stuff_coming
  • 443
  • 1
  • 6
  • 19
  • What about `docker stop ` ? – Zeitounator May 23 '20 at 09:16
  • docker container stop stopped the container successfully. But I donot want to stop the container on exit. I want to keep it running in the background. – cool_stuff_coming May 23 '20 at 09:21
  • Does this answer your question? [Correct way to detach from a container without stopping it](https://stackoverflow.com/questions/25267372/correct-way-to-detach-from-a-container-without-stopping-it) – Zeitounator May 23 '20 at 09:39
  • nopes... i want to keep the container running all the time. And stop does what the name says. – cool_stuff_coming May 23 '20 at 09:41
  • Did you actually click on the link and read ? I read your comment, understood what you were looking for and provided a different solution from my first comment. – Zeitounator May 23 '20 at 09:56
  • Yes i have already gone through this post. I mentioned docker container exec -it t_d_container /bin/bash in my original post. But looks this is the way docker is built. – cool_stuff_coming May 23 '20 at 10:01
  • Well then it looks like we are not reading the same post since on my screen there are several solutions pointed out to attach to a running container and detach from it without stopping it, with tons of discussion on how it works internally and references to the documentation.... – Zeitounator May 23 '20 at 10:44

1 Answers1

0

If you hve started your container in interactive mode (-it), Try below steps one after another :

  1. Ctrl+p
  2. Ctrl+q

If you've started your container in deamon mode (-d) and attached to it, you can just exit it and it will still run in the background.

Amit kumar
  • 2,169
  • 10
  • 25
  • 36
  • This doesnot work if I am using a Citrix to connect to remote servers. Also, when you try to run any commands on an attached container, they dont work at all. I have to press CTRL+C to exit the container. – cool_stuff_coming May 23 '20 at 09:59
  • Actually, You can run any command in a running container via its ID/ Name : docker exec -it echo "Hello container!". But if your container is in stopped state, you need to start it first. – Amit kumar May 23 '20 at 10:11
  • Say you want to build a db server on a container. You will have to keep the running 24hrs. Also, the shell login to the container should be enabled to execute sql commands. So, here I am trying to buld a container that will be always running with, say, oracle DB hosted on it, and other team members should be having shell access to manage the db rather than running docker container exec everytime. – cool_stuff_coming May 23 '20 at 10:18
  • `docker exec -it bash` (or whatever shell available...) gives you shell access to your container already running the db. – Zeitounator May 23 '20 at 14:54