“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

After that, why is the hello-world container in Exited state after startup? Here, we introduce two ways to start containers.

interactive

We know that the docker run command is used to start the container, but Docker allows us to open a pseudo terminal (parameter: -t) when starting the container. At this time, the standard input of the container should be kept open to achieve the purpose of interaction (parameter: -i).

Start the Ubuntu container:

root@phyger-VirtualBox:~# docker run -i -t ubuntu /bin/bash
root@0acc66615217:/# hostname
0acc66615217
root@0acc66615217:/#
Copy the code

Exit the Ubuntu container:

root@0acc66615217:/# exit
exit
root@phyger-VirtualBox:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
0acc66615217        ubuntu              "/bin/bash"         2 minutes ago       Exited (0) 5 seconds ago                       tender_archimedes
c3d23ead2fc6        hello-world         "/hello"            23 hours ago        Exited (0) 23 hours ago                        sharp_sinoussi
root@phyger-VirtualBox:~#
Copy the code

After exiting, the container state is Exited. Why?

Because the container’s survival depends on daemon process 1, if there is no daemon 1, the container will exit after executing the instructions specified by the user. If daemon 1 exists all the time, the container will remain UP.

For interactively started containers, we can use Ctrl+Q+P to exit, so that the container will keep /bin/bash as process 1, keeping the container UP.

root@phyger-VirtualBox:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0acc66615217        ubuntu              "/bin/bash"         11 minutes ago      Up 5 minutes                            tender_archimedes
Copy the code

To view processes in a container:

root@phyger-VirtualBox:~# docker attach 0acc
root@0acc66615217:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 13:39 pts/0    00:00:00 /bin/bash
root         8     1  0 13:41 pts/0    00:00:00 ps -ef
root@0acc66615217:/#
Copy the code

Guardian type

If we want the container to automatically run in the background after startup, we need to use the -d parameter

Start the Ubuntu container:

root@phyger-VirtualBox:~# docker run -d ubuntu /bin/bash -c "while true; do echo hello docker; sleep 1; done" 742eece654dbb6c2797a65e77b012d830c67185a327faa78295fd280967af3ff root@phyger-VirtualBox:~#Copy the code

After starting the container in daemon mode, it returns to the host terminal interface.

The following sh command constructs the daemon by writing an infinite loop so that the daemon never exits and the container stays UP.

View ubuntu containers:

root@phyger-VirtualBox:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 742eece654db ubuntu "/bin/bash - c 'while..." 2 minutes ago Up About a minute nice_khorana 0acc66615217 ubuntu "/bin/bash" 22 minutes ago Up 9 minutes tender_archimedes root@phyger-VirtualBox:~#Copy the code

Enter the Ubuntu container:

root@phyger-VirtualBox:~# docker exec -it 742 /bin/bash root@742eece654db:/# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 13:47 ? 00:00:00 /bin/bash -c while true; do echo hello docker; sleep 1; done root 258 0 23 13:51 pts/0 00:00:00 /bin/bash root 267 1 0 13:51 ? 00:00:00 sleep 1 root 268 258 0 13:51 pts/0 00:00:00 ps -ef root@742eece654db:/#Copy the code

Exit the Ubuntu container:

root@742eece654db:/# exit exit root@phyger-VirtualBox:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 742eece654db Ubuntu "/bin/bash -c 'while..." 5 minutes ago Up 5 minutes nice_khorana 0acc66615217 ubuntu "/bin/bash" 25 minutes ago Up 13 minutes tender_archimedes root@phyger-VirtualBox:~#Copy the code

Stop start container

Stop: docker stop

Start: docker start

Remove the container

Command: docker rm [-f]

Running containers are not allowed to be deleted directly. You need to stop them before deleting them. If you want to force a running container to be removed, you can add the -f argument.

About ATTACH and exec

We can use docker Exec and Docker Attach to enter the container, so what is the difference between them?

By attach, the standard input from the terminal is attached to the terminal of the container. The standard output from the container is printed to the terminal at the same time. If you exit the current terminal, the container also exits.

Exec executes commands in a running container to exit the current terminal. The container will not exit, so we recommend using exec.