Reputation: 1007
If I run a container directly from an Ubuntu image using docker container run ubuntu
, I can easily restart it using a docker start <CONTAINER ID>
. However, I run the container using the image by docker run <ID IMAGE>
, once I exit the pseudo-terminal, It's completely lost.
I noticed that the status updated every time I tried to restart it, which means that the main process starts and immediately stops. Any idea?
portaivan@training-vm:~$ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest d70eaf7277ea 8 days ago 72.9MB
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4004895feff9 d70eaf7277ea "/bin/bash" 3 hours ago Exited (0) 3 hours ago heuristic_lovelace
3b1b521d62aa ubuntu "/bin/bash" 3 hours ago Exited (0) 23 seconds ago sleepy_pike
portaivan@training-vm:~$ docker start 3b1b521d62aa
3b1b521d62aa
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4004895feff9 d70eaf7277ea "/bin/bash" 3 hours ago Exited (0) 3 hours ago heuristic_lovelace
3b1b521d62aa ubuntu "/bin/bash" 3 hours ago Up 2 seconds sleepy_pike
portaivan@training-vm:~$ docker container start 4004895feff9
4004895feff9
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4004895feff9 d70eaf7277ea "/bin/bash" 3 hours ago Exited (0) 2 seconds ago heuristic_lovelace
3b1b521d62aa ubuntu "/bin/bash" 3 hours ago Up 17 seconds sleepy_pike
portaivan@training-vm:~$ docker container start -i 4004895feff9
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4004895feff9 d70eaf7277ea "/bin/bash" 3 hours ago Exited (0) 2 seconds ago heuristic_lovelace
3b1b521d62aa ubuntu "/bin/bash" 3 hours ago Up 28 seconds sleepy_pike
Upvotes: 2
Views: 4970
Reputation: 9442
Docker container lives while command is running.
Your command is "/bin/bash"
.
And that is
pseudo-terminal
So when you closing terminal command (aka container entrypoint) container stops.
If you run container without -it
arguments you start bash without creating session (pseudo-tty).
For example, you can run:
bash -c "echo 'blabla'"
And bash just runs echo
command and end process.
So, here is two reasons why container not exited:
docker container run -t --name u_container ubuntu
You will be don't able to input any commands inside container. Because STDIN is not attached. But! You create pseudo-tty by -t
parameter and docker container can be started without exiting.
-i
parameter bash
don't create tty and want to die as soon as possible. But! docker holds container running and trying to pass input infinitely, even if you not attached to container (see help). Although we can stop bash
inserting exit
.-i, --interactive Keep STDIN open even if not attached
So, simple explanation: It depends on how you run shell. As looping and listening pseudo terminal or just as command interpreter.
You can compare it with python.
If you run python
, it will be python shell. If you run python /script/script.py
, python just execute script.py
and stops.
Upvotes: 1
Reputation: 1007
After hours and hours, I got it! The problem is the STDIN close at the run of the container. I ran Container 1 and Container 2 using the option -i. I ran Container 3 without the option -i. As you can see in the following image, I was able to start Container 1 and Container 2 but not Container 3.
==== THE REASON! ====
Every process has three data streams (STDIN/ STDOUT/ STDERR). When a process is running in a container, by default the terminal is connected with STDOUT stream of the process running in the container. Hence all the output streams will be visible while running docker run command in the terminal. But if you want to provide input to the running process in the container then you have to connect with STDIN channel of the process which is not by default and is done with docker run -i command.
portaivan@training-vm:~$ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
portaivan@training-vm:~$ docker container run -it --name container01 ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6a5697faee43: Pull complete
ba13d3bc422b: Pull complete
a254829d9e55: Pull complete
Digest: sha256:fff16eea1a8ae92867721d90c59a75652ea66d29c05294e6e2f898704bdb8cf1
Status: Downloaded newer image for ubuntu:latest
root@03b44b790cfa:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@03b44b790cfa:/# exit
exit
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03b44b790cfa ubuntu "/bin/bash" 14 seconds ago Exited (0) 5 seconds ago container01
portaivan@training-vm:~$ docker container start 03b44b790cfa
03b44b790cfa
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03b44b790cfa ubuntu "/bin/bash" 28 seconds ago Up 2 seconds container01
portaivan@training-vm:~$ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest d70eaf7277ea 8 days ago 72.9MB
portaivan@training-vm:~$ docker container run -it --name container02 d70eaf7277ea
root@e6123c0bbe81:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@e6123c0bbe81:/# exit
exit
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6123c0bbe81 d70eaf7277ea "/bin/bash" 10 seconds ago Exited (0) 3 seconds ago container02
03b44b790cfa ubuntu "/bin/bash" About a minute ago Up About a minute container01
portaivan@training-vm:~$ docker container start e6123c0bbe81
e6123c0bbe81
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6123c0bbe81 d70eaf7277ea "/bin/bash" 22 seconds ago Up 2 seconds container02
03b44b790cfa ubuntu "/bin/bash" About a minute ago Up About a minute container01
portaivan@training-vm:~$ docker container run --name container03 d70eaf7277ea
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7adbb0089b56 d70eaf7277ea "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago container03
e6123c0bbe81 d70eaf7277ea "/bin/bash" 2 minutes ago Up About a minute container02
03b44b790cfa ubuntu "/bin/bash" 3 minutes ago Up 2 minutes container01
portaivan@training-vm:~$ docker container start 7adbb0089b56
7adbb0089b56
portaivan@training-vm:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7adbb0089b56 d70eaf7277ea "/bin/bash" 15 seconds ago Exited (0) 2 seconds ago container03
e6123c0bbe81 d70eaf7277ea "/bin/bash" 2 minutes ago Up About a minute container02
03b44b790cfa ubuntu "/bin/bash" 3 minutes ago Up 3 minutes container01
portaivan@training-vm:~$
REFERENCES
https://docs-stage.docker.com/engine/reference/commandline/container_run/ https://www.howtogeek.com/435903/what-are-stdin-stdout-and-stderr-on-linux/
Upvotes: 1