Working with an interactive container

In the first chapter, we ran our first Hello World container to get a feel for how the containerization technology works. In this section, we are going to run a container in interactive mode. 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.

In the following example, we are going to launch an interactive container using the ubuntu:16.04 image and /bin/bash as the command:

$ sudo docker run -i -t ubuntu:16.04 /bin/bash  

Since the ubuntu image has not been downloaded yet, if we use the docker pull subcommand, then we will get the following message and the docker run command will start pulling the ubuntu image automatically with following message:

Unable to find image 'ubuntu:16.04' locally
16.04: Pulling from library/ubuntu

As soon as the download is completed, the container will get launched along with the ubuntu:16.04 image. It will also launch a Bash shell within the container, because we have specified /bin/bash as the command to be executed. This will land us in a Bash prompt, as shown here:

root@742718c21816:/# 

The preceding Bash prompt will confirm that our container has been launched successfully and it is ready to take our input. If you are wondering about the hex number 742718c21816 in the prompt, then it is nothing but the hostname of the container. In Docker parlance, the hostname is the same as the container ID.

Let's quickly run a few commands interactively and confirm what we mentioned about the prompt is correct, as shown here:

root@742718c21816:/# hostname
742718c21816
root@742718c21816:/# id
uid=0(root) gid=0(root) groups=0(root)
root@742718c21816:/# echo $PS1
[e]0;u@h: wa]${debian_chroot:+($debian_chroot)}u@h:w$
root@742718c21816:/#

From the preceding three commands, it is quite evident that the prompt was composed using the user ID, hostname, and current working directory.

Now, let's use one of the niche features of Docker for detaching it from the interactive container and then look at the details that Docker manages for this container. Yes, we can detach it from our container using the Ctrl + P and Ctrl + Q escape sequence. This escape sequence will detach the TTY from the container and land us in the Docker host prompt $; however, the container will continue to run. The docker ps subcommand will list all the running containers and their important properties, as shown here:

$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES

742718c21816 ubuntu:16.04 "/bin/bash" About a
minute ago Up About a minute jolly_lovelace

The docker ps subcommand will list out the following details:

  • CONTAINER ID: This shows the container ID associated with the container. The container ID is a 64 hex digit long random number. By default, the docker ps subcommand will show only 12 hex digits. You can display all the 64 digits using the --no-trunc flag (For example, sudo docker ps --no-trunc).
  • IMAGE: This shows the image from which the Docker container has been crafted.
  • COMMAND: This shows you the command executed during the container launch.
  • CREATED: This tells you when the container was created.
  • STATUS: This tells you the current status of the container.
  • PORTS: This tells you if any port has been assigned to the container.
  • NAMES: The Docker Engine auto-generates a random container name by concatenating an adjective and a noun. Either the container ID or its name can be used to take further action on the container. The container name can be manually configured using the --name option in the docker run subcommand.

Having looked at the container status, let's attach back to our container using the docker attach subcommand, as shown in the following example. We can either use the container ID or its name. In this example, we have used the container name. If you don't see the prompt, then press the Enter key again:

$ sudo docker attach jolly_lovelace
root@742718c21816:/#
Docker allows attaching with a container any number of times, which proves to be very handy for screen sharing.

The docker attach subcommand takes us back to the container prompt. Let's experiment a little more with the interactive container that is up-and-running using these commands:

root@742718c21816:/# pwd
/
root@742718c21816:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@742718c21816:/# cd usr
root@742718c21816:/usr# ls
bin games include lib local sbin share src
root@742718c21816:/usr# exit
exit
$

As soon as the Bash exit command is issued to the interactive container, it will terminate the Bash shell process, which in turn will stop the container. As a result, we will land on the Docker host's prompt $.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset