Introduction

When we create a container the general philosophy is that the container comes up, does the task it’s intended to perform and then goes away. Containers are basically meant to be ephemeral in nature but we can make a container last a little while longer. In this post we’ll talk about running containers, logging into containers and the container life cycle in general. First, let’s answer the question why would you want a container to last? If you’ve dockerized an existing application or wrote some code to run within the container you might want the container to hang around a bit to validate that your code is working as per expectation.

Working with containers:
Let’s first check which images do we have available on the system.

[sahil@linuxnix ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> e1c1d07a11b5 5 hours ago 182MB
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
[sahil@linuxnix ~]$

From the above output, you can see that we have three images available with us. The hello-world image we ran to validate our install, the ubuntu 16.04 image that we pulled from docker hub and finally the untagged container that we created using our dockerfile and based it off the ubuntu container.

We’ll now run a container from the image that we created and name it as well all in a single docker run command which is as follows:

[sahil@linuxnix ~]$ docker run -it --name linuxnix-python e1c1d07a11b5
root@ec1251de1c52:/#

-i flag implies interactive and the -t flag tells docker to allocate a pseudo-terminal for the container being run. When we use these two flags we’ll be logged into the container once the docker run command executes and you can verify the same by noticing the change in out terminal prompt. Let’s check the version of Ubuntu running in this container and verify that python3 is indeed installed in it.

root@ec1251de1c52:/# cat /etc/issue
Ubuntu 16.04.6 LTS \n \l

root@ec1251de1c52:/# python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>

We’ll now create a small Python script and execute it.

root@ec1251de1c52:/# vim hello.py
root@ec1251de1c52:/# chmod +x hello.py
root@ec1251de1c52:/# ./hello.py
Hello World
root@ec1251de1c52:/#

Consider a scenario wherein you have developers working on a system that does not have python3 available. Sharing this container image with python3 would be a nice way to allow for uniformity in the environment to be maintained. Now comes a caveat. The hello.py script that we created will be gone once we exit the container. This is because when we exit a container that container gets removed thereby deleting any files that we created in the container. I’ve exited the container and launched a container again from our image.

[sahil@linuxnix ~]$ docker run -it e1c1d07a11b5
root@09471626cb6f:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@09471626cb6f:/# exit

As you can see, the script is gone. We can work around it to an extent in case you need access to the data a little longer. The docker container ls command displays all running containers and the docker ls -a command shows running containers as well as those containers that have exited recently.

[sahil@linuxnix ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec1251de1c52 e1c1d07a11b5 "/bin/bash" 11 minutes ago Up 11 minutes linuxnix-python
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec1251de1c52 e1c1d07a11b5 "/bin/bash" 11 minutes ago Up 11 minutes linuxnix-python
4215e6ce539c hello-world "/hello" 46 hours ago Exited (0) 46 hours ago condescending_hugle
28f6d7dfe5ff hello-world "/hello" 46 hours ago Exited (0) 46 hours ago boring_wescoff

If we wish to regain access to the linuxnix-python container then we can do use by issuing the docker start command to the container id.

[sahil@linuxnix ~]$ docker start ec1251de1c52
ec1251de1c52

This will start the container in the background. To access the container type docker attach followed by the container id.

[sahil@linuxnix ~]$ docker attach ec1251de1c52
root@ec1251de1c52:/#
root@ec1251de1c52:/# ls
bin boot dev etc hello.py home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@ec1251de1c52:/#

As you may have observed our hello.py script is still available. What if we wanted to run this script from within the container while it’s still running? We can easily accomplish this with the docker exec command. Let’s first ensure that our container is still running.

[sahil@linuxnix ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec1251de1c52 e1c1d07a11b5 "/bin/bash" 2 hours ago Up About a minute linuxnix-python

Now we will run the script inside the container using docker exec as shown below:

[sahil@linuxnix ~]$ docker exec -it ec1251de1c52 ./hello.py
Hello World
[sahil@linuxnix ~]$

You could execute an arbitrary command as well.

[sahil@linuxnix ~]$ docker exec -it ec1251de1c52 uptime
16:06:32 up 6:10, 0 users, load average: 0.00, 0.01, 0.05
[sahil@linuxnix ~]$ docker exec -it ec1251de1c52 date
Tue Jul 9 16:06:40 UTC 2019
[sahil@linuxnix ~]$

Detaching from containers without stopping them: It might be annoying for some users that whenever you exit from a container the container just stops. We can easily keep a container running after exiting from.  When you want to exit from a container press ctrl+p followed by ctrl+q instead of typing exit. You will be returned back to your command prompt and the container will continue to run. Here’s a demonstration:

[sahil@linuxnix ~]$ docker attach ec1251de1c52
root@ec1251de1c52:/#
root@ec1251de1c52:/#
root@ec1251de1c52:/# read escape sequence
[sahil@linuxnix ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec1251de1c52 e1c1d07a11b5 "/bin/bash" 2 hours ago Up 8 minutes linuxnix-python
[sahil@linuxnix ~]$

In case we would like our docker container to just run a command and then exit then we could specify that command with the docker run command itself as shown in the below example:

[sahil@linuxnix ~]$ docker run --name trusty_tim5 e1c1d07a11b5 uname -a
Linux 3f7c3d9a8555 3.10.0-957.12.1.el7.x86_64 #1 SMP Mon Apr 29 14:59:59 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[sahil@linuxnix ~]$

To remove a container we need to use the docker container rm command followed by the container id or container name. Given below is an example:

[sahil@linuxnix ~]$ docker container rm ec1251de1c52
ec1251de1c52
[sahil@linuxnix ~]$

 

Removing a container image:
To remove a container image we need to use the docker image rm command followed by the image id. To demonstrate we’ll remove the hello-world image from our system.

[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> e1c1d07a11b5 7 hours ago 182MB
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
[sahil@linuxnix ~]$ docker image rm fce289e99eb9
Error response from daemon: conflict: unable to delete fce289e99eb9 (must be forced) - image is being used by stopped container 28f6d7dfe5ff
[sahil@linuxnix ~]$

We received an error because this image is in use by a stopped container. So we first need to remove the stopped container with the docker container rm command and then we can delete this image.

[sahil@linuxnix ~]$ docker image rm fce289e99eb9
Untagged: hello-world:latest
Untagged: hello-world@sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> e1c1d07a11b5 7 hours ago 182MB
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
[sahil@linuxnix ~]$

Conclusion

In this article we explained some of the different actions we could perform with containers once we create them. We hope you found this article to be interesting and useful and we encourage you to try out the examples demonstrated in the post.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.