Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on swebtools.com

Search Suggest

How to Run Docker Containers - Explained with Examples

how to run docker containers, run docker containers, docker container tutorial, docker run commands, docker containers explained, docker run examples
This tutorial post will show you how to run docker containers in linux with step by step explanation and also explained which options are required and when to use?. As we explained in the previous articles, "What is Docker - Get Started from Basics".

Docker containers can be created only from the docker images and Docker Container is a read/write layer of Docker Images.To run docker containers, We must have docker images available locally. If doesn't exists locally, docker will pull directly from docker hub registry through internet as explained in "Docker Images Explained with Examples".

If you are interested in learning, Request you to go through the below recommended tutorial.
Also You can Watch this Tutorial video on our YouTube Channel - How to Run Docker Containers - Explained with Examples.



How to Run Docker Containers - Explained with Examples


how to run docker containers


Let's get started.

Use "docker images" command to list the available docker images locally in your docker host and get the Image repository name or Image ID of the docker images you wish to run.

[root@docker-host ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              49f7960eb7e4        3 weeks ago         200 MB

Let's take CentOS image for example, Image name is "docker.io/centos" and Image ID is "49f7960eb7e4".

"docker run" command is used to run docker containers.

[root@docker-host ~]# docker run docker.io/centos

where, "docker.io/centos" is the docker image name, you can also use docker image ID (e.g: docker run 49f7960eb7e4).

Use "docker ps -a" command to check the status of the docker containers.

[root@docker-host ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
86d3e24bf49b        docker.io/centos    "/bin/bash"         6 seconds ago       Exited (0) 5 seconds ago                       quirky_visvesvaraya
[root@docker-host ~]#

Our container is exited. why?

When proper option is not used along with "docker run" command, container will exit and also it wont start even when you start the container manually (e.g: docker start containername).

Because we have several options available to work with containers, it must be used based on our requirement as below.

[root@docker-host ~]# docker run -it --name container1 docker.io/centos /bin/bash
[root@2ed29364bde5 /]#

Above docker command uses our CentOS image to run container "container1" with some options and arguements.

where,
"-it" - For interactive TTY session, so we will have terminal session to login into the created container.
"--name" - To specify the container name, else docker itself will assign some random names like "quirky_visvesvaraya", dazzling_galileo and etc,.
"container1" - Name of the container, it must be next to "--name".
"docker.io/centos" - Name of the Docker Image, you can use Docker Image ID also.
"/bin/bash" - We can use any command arguement at the end of the docker command, but this will override the CMD instruction used in docker images.

Look carefully, Above "docker run" command dropped us in a interactive TTY terminal session of a docker container "container1" and "2ed29364bde5" is the docker container ID. So we can perform our tasks on top of this docker container. Once you have completed, you can exit from the container using exit command.

[root@2ed29364bde5 /]# exit
exit
[root@docker-host ~]#

But when we exit from this session, it dropped us back to the docker host terminal from docker container "container1".

Check the status of the container now using "docker ps -a" command.

[root@docker-host ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
2ed29364bde5        docker.io/centos    "/bin/bash"         4 minutes ago       Exited (0) About a minute ago                       container1
86d3e24bf49b        docker.io/centos    "/bin/bash"         25 minutes ago      Exited (0) 19 minutes ago                           quirky_visvesvaraya
[root@docker-host ~]#

Above output shows that our docker container "container1" again moved to exited status. Its not running. Dont panic.

Start the container again using "docker start" command and check the status.

[root@docker-host ~]# docker start container1
container1
[root@docker-host ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
2ed29364bde5        docker.io/centos    "/bin/bash"         5 minutes ago       Up 3 seconds                                    container1
86d3e24bf49b        docker.io/centos    "/bin/bash"         26 minutes ago      Exited (0) 20 minutes ago                       quirky_visvesvaraya
[root@docker-host ~]#

Now the docker container "container1" is started and running up for 3 seconds. If you want to login into any running container, then we must use "docker exec" command as below.

Let's login into the container using "docker exec" command and exit from the container to check again.

[root@docker-host ~]# docker exec -it container1 /bin/bash
[root@2ed29364bde5 /]# exit
exit
[root@docker-host ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
2ed29364bde5        docker.io/centos    "/bin/bash"         5 minutes ago       Up 28 seconds                                   container1
86d3e24bf49b        docker.io/centos    "/bin/bash"         26 minutes ago      Exited (0) 20 minutes ago                       quirky_visvesvaraya
[root@docker-host ~]#

This time, container1 is still running even when we exit from the docker container session. It didn't moved to exited status.

So how to run docker containers permanently in the first time itself.

use "docker run" command with option "-d" to run the container in daemonize mode. So our container will not go into exited mode until we stop it manually.

[root@docker-host ~]# docker run -d -it --name container2 docker.io/centos /bin/bash
ffc82145bcdbf87ee4f0a94e0df2f62c90c4d8e689ad441bc44676715a80aa51
[root@docker-host ~]#

Above command created and running the container "container2" with docker container ID highlighted in yellow.

Check the status of docker containers.

[root@docker-host ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
ffc82145bcdb        docker.io/centos    "/bin/bash"         53 seconds ago      Up 52 seconds                                container22ed29364bde5        docker.io/centos    "/bin/bash"         About an hour ago   Up About an hour                             container1
86d3e24bf49b        docker.io/centos    "/bin/bash"         2 hours ago         Exited (0) 2 hours ago                       quirky_visvesvaraya

This time, container2 is running in daemonize mode as expected and it shows the first few characters of the docker container ID for our identification.

Login again into the container and exit from the container terminal to ensure its running.

[root@docker-host ~]# docker exec -it container2 /bin/bash
[root@ffc82145bcdb /]#
[root@ffc82145bcdb /]# exit
[root@docker-host ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
ffc82145bcdb        docker.io/centos    "/bin/bash"         4 minutes ago       Up 4 minutes                                 container2
2ed29364bde5        docker.io/centos    "/bin/bash"         About an hour ago   Up About an hour                             container1
86d3e24bf49b        docker.io/centos    "/bin/bash"         2 hours ago         Exited (0) 2 hours ago                       quirky_visvesvaraya
[root@docker-host ~]#

Yes its still running.

So, It is depend on the requirement that how we want our container to be running? If you have configured everything in docker image itself, then use "-d" option to run directly. Else run container without "-d" option and configure it based on the requirement. Start it again manually.

There are still more options can be used to attach volumes, exposing network ports, reserving CPU and memory resources for each container.

Hope you have got an indea How to Run Docker Containers.

In the previous posts, already we have explained the below topics. Refer those links to understand this topic from basics.

How to Install Kubernetes Cluster with Docker on Linux
Create Kubernetes Deployment, Services & Pods Using Kubectl
Create Kubernetes YAML for Deployment, Service & Pods
What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
How to Run Docker Containers - Explained with Examples

Going forward we will play more with docker tool.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.