How to Run a Docker Container

Running a Docker container is the most exciting part of containerization. In this tutorial, we will learn how to run a Docker container. Additionally, we will explore some important options about the docker run command. The docker run command runs a container from a certain image. Previously, we discussed in detail how to push and pull a Docker image from the Docker Hub

We already know that Docker is used to running applications in isolated environments. 

What are those isolated environments?

These environments are known as containers or Docker containers. In simple terms, a container is a running instance of a Docker image

Let’s see how to run a Docker container with the docker run command.

1. Running a Docker Container

First of all, let’s run a simple and the most popular hello-world image into a container.

[email protected]:~$ docker run hello-world
Hello from Docker!

I have pasted a short snippet of the output. You’ll see a complete message from the hello-world Docker image.

We already did that before while we installed Docker on Ubuntu 18.04 and Ubuntu 20.04

Here, we’ve run a hello-world container. The container ran and exited at the same time.

In order to see the running containers, we use the docker ps command. It’s important to know that this command will only show us the containers that are running at the moment. We won’t see the containers that were exited. 

For that purpose, we will use the following command:

[email protected]:~$ docker ps -a
f2508d6d3be3 hello-world "/hello" 54 seconds ago Exited (0) 52 seconds ago naughty_bell

Did you see that?

The container was created and exited after few seconds. It will keep running in the foreground.

2. The docker run command

Previously, we pulled an NGINX image with the docker pull command. I have that image locally on my machine. 

[email protected]:~$ docker images
nginx latest 08b152afcfae 5 days ago 133MB

Now, I will run this image into a container.

Here’s the docker run command with a few more options. Don’t worry, I’ll explain each one.

docker run -it -d -p 8090:80 --restart unless-stopped --name webserver1 nginx:latest

I’ll break down the options as:

  1. docker run command to run the container
  2. -it flag tells docker that it should open an interactive container instance.
  3. -d is for running the instance in detached mode, you can continue your work in the current shell.
  4. -p is for port mapping, 8090 is for the VM, and 80 for the container.
  5. –restart unless-stopped will boot the containers if they get stopped.
  6. –name gives the container a name.
  7. nginx:latest is our image.
the docker run command to run nginx

The most tricky part is the port mapping and you need to understand it now. The NGINX web server runs on the default port 80. So, it means the port inside the container is 80. I have mapped the VM’s port 8090 which is our source server over the container’s port 80. Therefore, I should be able to access the webserver on port 8090. 

Did you get it?

It’s just like we do in copy and paste, which is source and destination. You should get the following details about the container.

[email protected]:~$ docker ps
e8f6e1e8d595 nginx:latest "/docker-entrypoint.…" 8 hours ago Up 58 minutes>80/tcp, :::8090->80/tcp webserver1

Now, let’s check in the browser if the webserver is running on port 8090.

check nginx in web browser

We are running NGINX web server in a Docker container on port 8090.

3. Running Apache in a Docker Container

I’ll do the same drill by running an Apache container to make sure we have a better understanding of the docker run command. 

Instead of pulling the Apache image, we can directly execute the following command.

[email protected]:~$ docker run -it -d -p 8080:80 --restart unless-stopped --name webserver2 httpd:latest
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
Digest: sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Status: Downloaded newer image for httpd:latest

The Docker Engine will search for an httpd image locally on the virtual machine. If it’s not available, it will pull the image from the Docker Hub.

Let’s confirm our container in the running state.

[email protected]:~$ docker ps
5ead052cb249 httpd:latest "httpd-foreground" 6 minutes ago Up 6 minutes>80/tcp, :::8080->80/tcp webserver2

Also, we’ll check the web server in the browser.

running apache in a docker container


It works!

4. A Quick Summary

The docker run command is used for running Docker images into containers. We build images of our applications and then we run our applications into containers. There are a lot of other options that are used in the docker run command. These options include restart policies, setting ulimits, security options, and much more. You can have a quick overview of those options here. It’s not necessary to remember all the options at this point but we should have an idea about their use. 

I hope you have a good idea about running a Docker container. Please go ahead, pull some random images and run them into containers.

If you want to know more about Docker in the meanwhile, go through the following simple guides:

Let me know if you have any questions in the comments section.

Happy Docking!!!

Leave a Comment

Recent Posts

how to stop a docker container
How to Run a Docker Container
How to Push a Docker Image to a Private Repository
How to push a docker image
How to Tag a Docker Image
Software Spice
Share via
Copy link
Powered by Social Snap