How to run a Docker Container

how to run docker container
How to run Docker container –

How to interact with Docker

In the last post, we explained how to install Docker and ensure the Docker daemon is ready. In this post we’re going to see how to work with our containers. This post will provide you with the basics of how to run a docker container and how to interact with Docker.

Ensuring Docker is installed

We’re going to check that Docker is working correctly. Let’s check that the docker process is installed and responding:

sudo docker info

You should see an output similar to this:

Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 18.09.5

Here, we’ve passed the info command to the docker binary, which returns a list of containers, images (the building blocks Docker uses to build containers), the execution and storage drivers Docker is using, and its basic configuration.

Running a container

Now let’s launch a container with Docker using the docker run command to create a container. The docker run command provides all of the ”launch” capabilities for Docker.

sudo docker run -i -t ubuntu /bin/bash
 Unable to find image 'ubuntu:latest' locally
 latest: Pulling from library/ubuntu
 898c46f3b1a1: Pull complete 
 63366dfa0a50: Pull complete 
 041d4cd74a92: Pull complete 
 6e1bee0f8701: Pull complete 
 Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
 Status: Downloaded newer image for ubuntu:latest

Let me explain what just happened. First, we told Docker to run a command using docker run . We passed it two command line flags: -i and -t . The -i flag keeps STDIN open from the container, even if we’re not attached to it. This persistent standard input is one half of what we need for an interactive shell. The -t flag is the other half and tells Docker to assign a pseudo-tty to the container we’re about to create. This provides us with an interactive shell in the new container. This line is the base configuration needed to create a container with which we plan to interact on the command line rather than run as a daemonized service.

Next, we told Docker which image to use to create a container, in this case the ubuntu image. The ubuntu image is a base image, provided by Docker, Inc., on the Docker Hub registry. You can use base images like the ubuntu base image (and the similar debian , centos , etc., images) as the basis for building your own images on the OS of your choice.
For now, we’re just running the base image as the basis for our container and not adding anything to it.

So what was happening after we run the command? First, Docker checked locally for the ubuntu image. If it can’t find the image on our local Docker host, it will reach out to the Docker Hub registry run by Docker, Inc., and look for it there.
Once Docker had found the image, it downloaded the image and stored it on the local host.
Docker then used this image to create a new container inside a filesystem. The container has a network, IP address, and a bridge interface to talk to the local host. Finally, we told Docker which command to run in our new container, in this case launching a Bash shell with the /bin/bash command.
When the container had been created, Docker ran the /bin/bash command inside it; the container’s shell was presented to us like so:


Working with our container

We are now logged into a new container as root user. This is a Ubuntu system, and we can do anything we like in it. However, you will find out that it may not have all the tools you are used to. But, they can be installed if needed.

Let’s explore it a bit, starting with asking for its hostname.

root@0f780b63f710:/# hostname

We see that our container’s hostname is the container ID. Let’s have a look at the /etc/hosts file too.

root@0f780b63f710:/# cat /etc/hosts    localhost
 ::1    localhost ip6-localhost ip6-loopback
 fe00::0    ip6-localnet
 ff00::0    ip6-mcastprefix
 ff02::1    ip6-allnodes
 ff02::2    ip6-allrouters    0f780b63f710

Docker has also added a host entry for our container with its IP address.
Let’s also check out its networking configuration.

If you are use to work with Linux you may try to run ifconfig or ip to get the IP address of the container, if you do so you may get this:

root@0f780b63f710:/# ifconfig
 bash: ifconfig: command not found
 root@0f780b63f710:/# ip
 bash: ip: command not found

ifconfig and ip commands are not installed by default on this container image. You could install them, but remember the idea of containers is to keep them to the minimum, so a handy trick to check the ip address from within the container is to run ‘hostname -i

root@0f780b63f710:/# hostname -i

We can also check its running processes

root@0f780b63f710:/# ps -aux 
 root         1  0.0  0.3  18508  3476 pts/0    Ss   14:59   0:00 /bin/bash
 root        34  0.0  0.2  34400  2928 pts/0    R+   15:09   0:00 ps -aux

We can see the container is quite light-weight, bare-minimum I would say, as only the bash shell and the ps command are shown as running process. Compare this to the output of the same command on a newly installed Virtual machine and that clearly shows why Containers are becoming so popular and why they are so fast!

When you’re done, type exit , and you’ll return to the command prompt of your Ubuntu host.

You may be wondering what happens when you exit the container. Well, it has now stopped running. The container only runs for as long as the command we specified, /bin/bash , is running. Once we exited the container, that command ended, and the container was stopped.
The container however still exists; we can show a list of current containers using the docker ps -a command.

$ sudo docker ps -a
 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
 0f780b63f710        ubuntu              "/bin/bash"         13 minutes ago      Exited (0) 18 seconds ago                       awesome_leakey

By default, when we run just docker ps , we will only see the running containers. When we specify the -a flag, the docker ps command will show us all containers, both stopped and running.

This is all for now. On the next post we will continue exploring containers, we are just starting!