Docker - Controlling Container

Table of Contents

Interactive Container

The docker run subcommand takes an image as an input and launches it as a container.

You have to pass the -t and -i flags to the docker run subcommand in order to make the container interactive.

  • The -i flag is the key driver, which makes the container interactive by grabbing the standard input (STDIN) of the container.
  • The -t flag allocates a pseudo-TTY or a pseudo terminal (terminal emulator) and then assigns that to the container.
$ docker run -i -t ubuntu:18.04 /bin/bash 
Unable to find image 'ubuntu:18.04' locally
18.04: Pulling from library/ubuntu
5c939e3a4d10: Pull complete                                                                                                                                                                                                                                                    Status: Downloaded newer image for ubuntu:18.04
root@31dd6a04032e:/# pwd
root@31dd6a04032e:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Controlling Containers

The Docker engine enables you to start, stop, and restart a container with a set of docker subcommands.

When a user issues docker stop, the Docker engine sends SIGTERM(-15) to the main process, which is running inside the container. The SIGTERM signal requests the process to terminate itself gracefully.

However, if this process fails to do so, then the Docker engine will wait for a grace period. Even after the grace period, if the process has not been terminated, then the Docker engine will forcefully terminate the process. The forceful termination is achieved by sending SIGKILL(-9).

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
  -t, --time int   Seconds to wait for stop before killing it (default 10)

The restart command is a combination of the stop and the start functionality.

By default, the docker start subcommand will not attach to the container. You can attach it to the container either by using the -a option in the docker start subcommand or by explicitly using the docker attach subcommand, as shown here:

$ sudo docker attach da1c0f7daa2a 

The next important set of container-controlling subcommands are docker pause and docker unpause. The docker pause subcommands will essentially freeze the execution of all the processes within that container. Conversely, the docker unpause subcommand will unfreeze the execution of all the processes within that container and resume the execution from the point where it was frozen.

Run the follwoing commands in two Screens and explain what you did.

In screen 1

$ docker run -i -t ubuntu:18.04 /bin/bash 
root@f6d8d21591e5:/# pwd
root@f6d8d21591e5:/# ls

In screen 2

$ docker ps
$ docker stop f6d8d21591e5
$ docker ps
$ docker start f6d8d21591e5
$ docker attach f6d8d21591e5

In screen 1

$ docker run -i -t ubuntu:14.04 /bin/bash 
root@25188ce97a8a:/# while true; do date; sleep 5; done

In screen 2

$ docker pause 25188ce97a8a
$ docker unpause 25188ce97a8a