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

<options> and <arguments> are option fields.

Let’s create a centos container i.e.

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

First line of output must be like,

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.

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

It will show output like this,

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

Then will show that our container is still running i.e.

Now you can also stop this container with following command,

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

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.


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.


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.

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.

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.


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.

It will stop the container.

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.