Getting started with Docker part 2

Docker creates a container from a existing image and runs it, the first process started by docker will have pid of 1 and if no command is specified when running container it will run the container and immediately exit.

lets see this in action

$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              2d194b392dd1        3 weeks ago         195MB

starting a docker container from the existing image centos7

$docker run centos

The above command starts the docker container since there was no command passed docker container starts and exists.

Docker process can be checked from command docker ps

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

lets run docker with shell access

$docker run centos /bin/bash 

starts a container since we have not specified a tty access docker exists process.

running docker with a terminal tty access

ocker run -it centos /bin/bash 
[root@64e34e41902b /]# 

when we exit attached docker container it is removed because it exists the process 1 (/bin/bash)

$[root@c955676bdc94 /]# 
[root@c955676bdc94 /]# ps
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
   13 pts/0    00:00:00 ps

[root@c955676bdc94 /]# exit 
exit

If we want to avoid this and keep container active then we need run docker in daemon mode it can be done by using -d option


$docker run -itd centos /bin/bash 
67d7614a7d3b8e92d1a48ae4fdccfe330357b5ac4014b25e48ebe8fcd5f6c2fa

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
67d7614a7d3b        centos              "/bin/bash"         15 seconds ago      Up 14 seconds                           compassionate_shannon

$docker attach   67d7614a7d3b
[root@67d7614a7d3b /]# 

$[root@67d7614a7d3b /]# ps
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
   16 pts/0    00:00:00 ps