In continuation of my previous blog on “5. DevOps: How to work with Docker Images”, in this blog I would like to show some lab practice on Interactive Docker containers.
Working with an interactive Docker container:
In the previous lab session, we worked with first Hello World container. And we came to know how the containerization works. Now, we are going to run a container in interactive mode.
What is docker run command ?:
The docker run subcommand takes an image as an input and launches it as a container.
What flags we need to use ?:
We 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, it makes the container interactive by grabbing the standard input (STDIN) of the container into the terminal.
The -t flag allocates a pseudo-TTY or a pseudo Terminal (Terminal emulator) and then assigns that to the container.
Note: Please note in the earlier session we have executed a container on unbuntu name.
But now, we will explore completely the interactive container operations.
In the below 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
=========== Output ============>
vskumar@ubuntu:/var/log$ sudo docker run -i -t ubuntu:16.04 /bin/bash
Unable to find image ‘ubuntu:16.04’ locally
16.04: Pulling from library/ubuntu
Digest: sha256:7c67a2206d3c04703e5c23518707bdd4916c057562dd51c74b99b2ba26af0f79
Status: Downloaded newer image for ubuntu:16.04
root@53a7751d4673:/#
===================>
Why the error messages [Unable to find image] appear ?:
As the ubuntu 16.04 image is not downloaded yet, we get the above message and with the docker run command it will start pulling the ubuntu 16.04 image automatically with following message:
Unable to find image 'ubuntu:16.04' locally 16.04: Pulling from library/ubuntu
When 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 landed us in a Bash prompt, as shown below:
What is ’53a7751d4673′?:
It is the hostname of the container. In Docker, the hostname is the same as the container ID.
Now, let us run a few commands interactively and confirm what we mentioned about the prompt is correct, as shown below:
To check the hostname below commands need to be executed:
root@53a7751d4673:/# hostname
root@53a7751d4673:/# id
root@53a7751d4673:/# echo $PS1
When we execute them, can see the below output:
==============>
root@53a7751d4673:/#
root@53a7751d4673:/# hostname
53a7751d4673
root@53a7751d4673:/# id
uid=0(root) gid=0(root) groups=0(root)
root@53a7751d4673:/# echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
root@53a7751d4673:/#
=====================>
So, we have seen the Host name as ’53a7751d4673′.
Id as ‘root ‘
Using ‘PS1’, —>Displays username, hostname and current working directory in the prompt.
PS1 in this example displays the following three information in the prompt:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
- \u – Username
- \h – Hostname
- \w – Full path of the current working directory
==============>
root@53a7751d4673:/# pwd
/
root@53a7751d4673:/#
==========>
Note, we are within the ubuntu 16.04 container and it works as Linux machine. So we can try some Linux commands also:
===============>
root@53a7751d4673:/# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
26 pts/0 00:00:00 ps
root@53a7751d4673:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:28 pts/0 00:00:00 /bin/bash
root 27 1 0 11:48 pts/0 00:00:00 ps -ef
root@53a7751d4673:/#
root@53a7751d4673:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@53a7751d4673:/# ls -l
total 64
drwxr-xr-x 2 root root 4096 Nov 14 13:49 bin
drwxr-xr-x 2 root root 4096 Apr 12 2016 boot
drwxr-xr-x 5 root root 360 Nov 25 11:28 dev
drwxr-xr-x 45 root root 4096 Nov 25 11:28 etc
drwxr-xr-x 2 root root 4096 Apr 12 2016 home
drwxr-xr-x 8 root root 4096 Sep 13 2015 lib
drwxr-xr-x 2 root root 4096 Nov 14 13:49 lib64
drwxr-xr-x 2 root root 4096 Nov 14 13:48 media
drwxr-xr-x 2 root root 4096 Nov 14 13:48 mnt
drwxr-xr-x 2 root root 4096 Nov 14 13:48 opt
dr-xr-xr-x 250 root root 0 Nov 25 11:28 proc
drwx—— 2 root root 4096 Nov 14 13:49 root
drwxr-xr-x 6 root root 4096 Nov 14 13:49 run
drwxr-xr-x 2 root root 4096 Nov 17 21:59 sbin
drwxr-xr-x 2 root root 4096 Nov 14 13:48 srv
dr-xr-xr-x 13 root root 0 Nov 25 11:28 sys
drwxrwxrwt 2 root root 4096 Nov 14 13:49 tmp
drwxr-xr-x 11 root root 4096 Nov 14 13:48 usr
drwxr-xr-x 13 root root 4096 Nov 14 13:49 var
root@53a7751d4673:/#
================>
So, ubuntu 16.04 container is nothing but a linux machine and we executed the above commands.
Now, I want to change the root permissions as below:
==============>
root@53a7751d4673:/# chmod +777 root
root@53a7751d4673:/# ls -l
total 64
drwxr-xr-x 2 root root 4096 Nov 14 13:49 bin
drwxr-xr-x 2 root root 4096 Apr 12 2016 boot
drwxr-xr-x 5 root root 360 Nov 25 11:28 dev
drwxr-xr-x 45 root root 4096 Nov 25 11:28 etc
drwxr-xr-x 2 root root 4096 Apr 12 2016 home
drwxr-xr-x 8 root root 4096 Sep 13 2015 lib
drwxr-xr-x 2 root root 4096 Nov 14 13:49 lib64
drwxr-xr-x 2 root root 4096 Nov 14 13:48 media
drwxr-xr-x 2 root root 4096 Nov 14 13:48 mnt
drwxr-xr-x 2 root root 4096 Nov 14 13:48 opt
dr-xr-xr-x 255 root root 0 Nov 25 11:28 proc
drwxrwxrwx 2 root root 4096 Nov 14 13:49 root
drwxr-xr-x 6 root root 4096 Nov 14 13:49 run
drwxr-xr-x 2 root root 4096 Nov 17 21:59 sbin
drwxr-xr-x 2 root root 4096 Nov 14 13:48 srv
dr-xr-xr-x 13 root root 0 Nov 25 11:48 sys
drwxrwxrwt 2 root root 4096 Nov 14 13:49 tmp
drwxr-xr-x 11 root root 4096 Nov 14 13:48 usr
drwxr-xr-x 13 root root 4096 Nov 14 13:49 var
root@53a7751d4673:/#
================>
Now, I want to exit from container and come back to host machine.
==================>
root@53a7751d4673:/#
root@53a7751d4673:/# exit
exit
vskumar@ubuntu:/var/log$
===============>
Whenever the Bash exit command is used in the interactive container, it will terminate the Bash shell process.
In turn it will stop the container and returns to the docker host machine.
As a result, we can see the Docker host’s prompt $
You can see the status of docker images as below when I used ‘sudo docker images’ :
==================>
vskumar@ubuntu:/var/log$
vskumar@ubuntu:/var/log$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 4 days ago 1.85kB
ubuntu 16.04 20c44cd7596f 7 days ago 123MB
ubuntu latest 20c44cd7596f 7 days ago 123MB
busybox latest 6ad733544a63 3 weeks ago 1.13MB
busybox 1.24 47bcc53f74dc 20 months ago 1.11MB
vskumar@ubuntu:/var/log$
=====================>
You can see whatever containers; we have have used in the past exercises.
At this point, I would like to stop this lab session. And in the next blog we can see on “How to track changes in a container?”.