In this article we will discuss how to create and run a Docker Container from an Image. Also how to run container in Interactive mode or Override default command or provide custom names to containers.

What is a Docker Container ?

A Docker Container is like an instance of a Docker Image and it runs as a separate entity in the the host. When a container is created it takes the snapshot of file system from the docker image.

We can run multiple docker containers on our host and each running docker container is like a lightweight VM which shares the host OS kernel but has it’s own filesystem, networking and processes.

How to create and start a container

Docker provides a command line interface (CLI) to interact with Docker Engine and manage containers. One of the commands to create and run container is docker run.

Let’s discuss that in detail.

docker run

We can create and run a container using docker run command. Its syntax is like

docker run <options> <image> <arguments>

<options> and <arguments> are option fields.

Let’s create a centos container i.e.

docker run centos

It will first check if the docker engine has centos image or not. If not then it will first fetch the centos image from repository and then start the container.

But, we got our prompt back, Is container running now ?

Well our container started and stopped instantly. We can confirm this by following command

docker ps -a

First line of output must be like,
CONTAINER ID IMAGE  COMMAND     CREATED       STATUS             PORTS NAMES
f497edf4ba16 centos "/bin/bash" 4 seconds ago Exited (0) 3 seconds ago admiring_hertz

It shows that container with ID “f497edf4ba16” started from centos image but its status is Exited i.e. it ended soon after starting.

Why did that happen ?

Because when we don’t provide any <argument> while running a container in docker run command then it runs the default command set in the image.  Like in case of centos image has default command is “/bin/bash”.
So, when we created and ran the container, it started and executed command /bin/bash and exited soon after that. We can override this default command and make container to run another process on start. Let’s see how to do that.

Override default command while running a Docker container

To override the default command while running docker container pass the command in <argument> field of docker run command.

docker run <options> <image> <arguments>

Let’s pass a command in centos docker container to ping localhost when it starts i.e.
docker run centos ping localhost

It will show output like this,
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.054 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.025 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.026 ms
.....

Now it keep running for next few seconds. During that time if we open an another terminal and check all running containers i.e.
docker ps

Then will show that our container is still running i.e.
CONTAINER ID IMAGE COMMAND           CREATED            STATUS       PORTS NAMES
7d8e4ff176ba centos "ping localhost" About a minute ago Up About a minute nostalgic_jepsen

Now you can also stop this container with following command,
docker stop 7d8e4ff176ba

If you will provide any command for which there is no executable in container files ystem then it will throw error like this,
docker run centos xyz

Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"xyz\": executable file not found in $PATH": unknown.

Running a Docker container in interactive mode

If not overridden then by default centos or ubuntu docker containers run the /bin/bash command. Which starts a bash session and ends it soon after that.  What if we want to start our container and keep a live connection to its interactive shell. For that we need to pass following flags in docker run command,

  • -i (interactive) flag is to keep stdin open
  • -t flag is to allocate a terminal.

Let’s run a centos container and keep it’s shell open in interactive mode i.e.

docker run -it centos

Output:
PS C:\Varun\box> docker run -it centos
[root@459a4f9265fd /]#

Once centos container started, it executed /bin/bash by default and -it flag forced it to keep stdin open and assign a interactive terminal for us. Now we can execute commands in this shell. Let’s execute ls command i.e.
[root@459a4f9265fd /]# ls

Output:
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

Now this command was executed inside container and output was shown in our terminal. You can also run other commands and test stuff, all commands executed through this shell will run inside container only.
To exit from this terminal and go back to our host box type exit i.e.
[root@459a4f9265fd /]# exit
exit PS 
C:\Varun\box>

How to create and run a docker container with custom name

We can also pass the name flag in docker run command to assign a name to the container i.e.

docker run --name varun_centos -it centos

It will run the container in interactive mode and opens it shell. Now from another terminal if we check the list of running containers i.e.
docker ps

Output:
CONTAINER ID IMAGE  COMMAND     CREATED        STATUS    PORTS NAMES
fba1d6eb209a centos "/bin/bash" 11 seconds ago Up 10 seconds varun_centos

In the output we can see that varun_centos name is assigned to our container instead of any default name. This name of container is really useful, if we want to interact with running container then we can do that with container name only. For example let’s stop this container using it’s name i.e.
docker stop varun_centos

It will stop the container.