Start Docker Containers with docker run Command

Once you have the Docker service installed and running on your Linux system, Next step is to start running docker containers. In the following tutorial we are going to learn how to create new containers on docker engine with docker run command.

The docker run command creates new docker containers from images. The syntax for docker run is as follows:


The only required command parameter is a docker image. To start a new docker container we need a docker image, If the image does not exist, docker will download the image from the docker hub, then create and start the new container.

Example: Create a new docker container from image

Following command will start a new Ubuntu container called 'ubuntu-server' from the ubuntu:16.04 image.

docker run -d -t --name ubuntu-server ubuntu:16.04
  • -d - Run the container in the background.
  • -t - Allocate a pseudo-TTY.

You can get a shell session to the container with docker exec command:

docker exec -it ubuntu-server bash

List running containers with docker ps:

docker ps

To list all containers, Type:

docker ps -a

Container Name

Docker containers can be identified by container ID or Name. By default, the Docker daemon assigns a random name to the container if name is not specified. We can assign a name to the container with --name option.

docker run --name some-name image

Run Docker Container in Background

Docker container can be started in a detached mode (background) or foreground mode.

The default mode is the foreground mode in which the container starts in the foreground and use your current console to process’s standard input, output, and standard error.

To run containers in the background, use the -d option.

docker run -d --name some-redis redis

With the -d option container will start in the background and keep running in the background.

Allocate a Pseudo Terminal to the container

Some containers need to allocate a pseudo terminal when a container start with the docker run command. This is done with the -t option.

docker run -d -t --name ubuntu-server ubuntu:16.04

Note that not all containers need to start with -t option. Most of the time you will need to use -t option if you run operating systems like Ubuntu or Fedora on docker containers.

Exposing a port while starting a container

We need do port mapping to expose container's internal ports to the outside. With the -p option, we map container's internal port with a port on the host machine.

docker run -d -p host_port:container_port image

In the following example, we create a new Apache HTTPD container with host port 80 maps to the container port 80.

docker run -d -p 80:80 --name apache-web httpd:latest

When we start a new container we can link another container to the new container with --link flag.

In the following example, First we create a mysql container:

docker run -d --name my_server -e MYSQL_ROOT_PASSWORD=123456  mysql:5.7

Next, we create httpd container with a link to the mysql container:

docker run -d -p 80:80 --name apache --link my_server:mysql httpd:latest

The apache container now has access to the databases inside the mysql instance. Inside the apache instance, mysql host should be either Name or ID of the mysql container.

mysql -p -h my_server

Mount Volume from Host machine

With -v option we can mount directories and files on the host to the containers.

docker run -v host_dir:/container_dir image:tag

In the following example, we start a new Apache container and the content will serve from the /var/www/html directory on the host.

docker run -d -p 80:80 -v /var/www:/var/www httpd:latest

Docker Run Command Options

-d Run the container in the background.
-t Allocate a pseudo-TTY.
-e Set environment variables inside of the Containers.
-h Sets the hostname of the docker container.
--link=[] Add link to another container.
-m, --memory="" Limit the memory available to a container, unit can be b, k, m or g. If a limit of 0 is specified (not using -m), the container's memory is not limited.
--mac-address="" Set Container MAC address (e.g. 92:d0:c6:0a:29:33).
--name="" Assign a name to the container. If not specified, docker will assign some random name to the docker container.
-p Publish a container's port, or range of ports, to the host.
-P Publish all exposed ports to random ports on the host interfaces. The default is false.
--rm Automatically remove the container when it exits.