Detached and interactive containers

November 11, 2021 - Reading time: 4 minutes

Detached containers run in the background (not attached to input/output stream)

To launch a detached container:

test@localhost: sudo docker run --detach --name web nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
7d63c13d9b9b: Pull complete
15641ef07d80: Pull complete
392f7fc44052: Pull complete
8765c7b04ad8: Pull complete
8ddffa52b5c7: Pull complete
353f1054328a: Pull complete
Digest: sha256:dfef797ddddfc01645503cef9036369f03ae920cac82d344d58b637ee861fda1
Status: Downloaded newer image for nginx:latest
096112267c5a0d3e4df38cac68cef1aaaa17caa70ca06db8d8985f4aebea6a43

To run an interactive container, we can specify -i
an interacitve container won't have a tty by default:

test@localhost: sudo docker container run --name web_test --interactive busybox
ls
bin
dev
etc
home
proc
root
sys
tmp
usr
var                          #### <---- Exited  with ctrl + d 
test@localhost: sudo docker container ls --all
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS                      PORTS       NAMES
cc5dd5327298   busybox                     "sh"                     25 seconds ago   Exited (0) 16 seconds ago               web_test
8652860736fa   dockerinaction/ch2_mailer   "/mailer/mailer.sh"      12 minutes ago   Up 12 minutes               33333/tcp   mailer
096112267c5a   nginx:latest                "/docker-entrypoint.…"   15 minutes ago   Up 15 minutes               80/tcp      web

To get a tty we use --tty

test@localhost: sudo docker container run --name web_test --interactive --tty busybox
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ #   ### <--- Exited with ctrl + d 

We can use --link to create a link between containers running in the same system. For example, I want my busybox container to access nginx and load the index.html page:

test@localhost: sudo docker container run --name web_test --interactive --tty --link web:theWebServer busybox
/ # ping theWebServer
PING theWebServer (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.086 ms
[...]
/ # ping web
PING web (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.054 ms
[...]
/ # ip -4 addr show dev eth0
28: eth0@if29: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

To run an interactive container, and detach without exiting the process we need to use Ctrl + p Ctrl + q

test@localhost: sudo docker run --interactive --tty --name agent --link web:insideweb --link mailer:insidemailer dockerinaction/ch2_agent
[...]
System up.
System up.
System up.    ## <--- detached with ctrl +p ctrl + q 
test@localhost:

Check all containers running in the system with ps

test@localhost: sudo docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS       NAMES
5723dbb7f8c8   dockerinaction/ch2_agent    "/watcher/watcher.sh"    4 minutes ago    Up 4 minutes                agent
8652860736fa   dockerinaction/ch2_mailer   "/mailer/mailer.sh"      39 minutes ago   Up 39 minutes   33333/tcp   mailer
096112267c5a   nginx:latest                "/docker-entrypoint.…"   42 minutes ago   Up 42 minutes   80/tcp      web

Check the stdout / stderr of a container using the log command. Optionally specify tail and follow (like tail -f)

test@localhost: sudo docker logs web --follow --tail 1
172.17.0.4 - - [12/Nov/2021:00:28:31 +0000] "GET / HTTP/1.0" 200 615 "-" "-" "-"
172.17.0.4 - - [12/Nov/2021:00:28:32 +0000] "GET / HTTP/1.0" 200 615 "-" "-" "-"
172.17.0.4 - - [12/Nov/2021:00:28:33 +0000] "GET / HTTP/1.0" 200 615 "-" "-" "-"
172.17.0.4 - - [12/Nov/2021:00:28:34 +0000] "GET / HTTP/1.0" 200 615 "-" "-" "-"
^C

Now we can stop containers (sends SIGTERM) after SIGTERM, tries to send SIGKILL (if process didn't stop).
We could also send SIGKILL with a specific signal using docker kill --signal 11

test@localhost: sudo docker stop web
web
test@localhost: sudo docker stop agent
agent
test@localhost: sudo docker stop agent mailer
agent

mailer