Docker: Running Docker commands


This tutorial is a continuation of a previous post Docker: Installation and basics

The command to run a container from an image is:

[root@gw20-lap-doc1 ~]# docker run <image name>

The command docker run image = docker create <image name> and docker start <container ID>

Now let us create a new container from the image centos which we downloaded in the previous post

[root@gw20-lap-doc1 ~]# docker create centos echo “I am good”

This should return a <container ID>

Now let us see the status of all containers in the host

[root@gw20-lap-doc1 ~]# docker ps -all

You will notice that the container is listed, and the STATUS will be Created.

To start the container issue the following command:

[root@gw20-lap-doc1 ~]# docker start <Container ID>

By this container started, ran the command echo “I am good” and exited. However you would not have seen the output of the echo command on the screen.

Ensure the container existed. Check the status by running the following command.

[root@gw20-lap-doc1 ~]# docker ps -all

Now let us get the actual output of the command run by the container by running the following command:

[root@gw20-lap-doc1 ~]# docker start -a e32e4a84ee7d

The switch -a did the trick, which showed the output on the screen.

In case we want to see all outputs created when a container ran after the docker existed, we can run the logs command:

[root@gw20-lap-doc1 ~]# docker logs <container ID>

Though the containers are stopped, they may still reside in the server taking space. The was to get rid of them is:

[root@gw20-lap-doc1 ~]# docker system prune

If you note, when running the above command will remove all build cache. This are the images downloaded from Docker Hub.

If we have a running container and we want to stop it, we can run the following command:

[root@gw20-lap-doc1 ~]# docker stop <container ID>

The stop command send a signal to the container to do a graceful shutdown. However there is another command called kill that send an immediate command to shutdown the container, without any proper housekeeping.

Let us try to execute a command in a running container. For this let us run a centos container that will keep pinging google in the background.

[root@gw20-lap-doc1 ~]# docker create centos ping

[root@gw20-lap-doc1 ~]# docker start <container ID>

[root@gw20-lap-doc1 ~]# docker ps -all

Ensure the container is Up in the STATUS.

Now let us execute another command in the container by running the exec command

[root@gw20-lap-doc1 ~]# docker exec -it c5c3403885d2 /bin/bash

This will open a bash application and the -it switch (or -i -t) will show the output on the docker’s terminal window. The -i enables us to connect to the Standard input channel of the container, and the -t switch formats the input and output view.

Running commands in the background:

At times we may want to start a container by running the commands in the background so that we can get the terminal to do other operations.

When we run the following command, the ping command will start and the output will be displayed in on the terminal window, and you will not be able to use that terminal to do any thing. Also you we will need to keep the terminal opened.

[root@gw20-lap-doc1 ~]# docker run centos ping

However the following command will run the container, pinging google in the background.

[root@gw20-lap-doc1 ~]# docker run -d centos ping

You may verify the same by running the exec command, and taking a list of the running command.