Docker Run: Start Docker Containers from an Image

Once you have the Docker service installed and running on your Linux system, the next step is to start running Docker containers. In this tutorial, we will learn how to create new containers on Docker Engine using the docker run command.

Command syntax

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

docker run [OPTIONS] IMAGE [COMMAND]

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 locally, Docker will download it from Docker Hub, then create and start the new container.

Example: Create a New Docker Container from an Image

The following command will start a new Ubuntu container called 'ubuntu-server' from the ubuntu image:

docker run -d -t --name ubuntu-server ubuntu
-dRun the container in the background.
-tAllocate a pseudo terminal.

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 either their container ID or their name. By default, if a name is not specified, the Docker daemon assigns a random name to the container. However, you can assign a custom name to the container using the --name option.

docker run --name some-name image

Run Containers in the Background

Docker containers 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 uses your current console to process’s standard output, and standard error.

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

docker run -d redis

With the -d option the 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 starts with the docker run command. This is done with the -t option.

docker run -d -t ubuntu

Note that not all containers need to be started with the -t option. However, if you are running operating systems like Ubuntu or Fedora in Docker containers, it's essential to include the -t option to allocate a pseudo-TTY for the container.

Port Mapping

We need to perform port mapping to expose the container's internal ports to the outside world. This is achieved using the -p option, where we map a container's internal port to 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

Here is another example:

docker run -d -p 8080:80 --name nginx-web nginx

In the above example, we're publishing port 8080 on the Docker host to port 80 on the nginx server. This configuration redirects any traffic coming to port 8080 on the Docker host to port 80 on the nginx container.

When starting a new container, we can link another container to it using the --link option.

In the following example, first we create a MySQL container:

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

Next, we'll create an Apache HTTPD container with a link to the MySQL container:

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

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_serve

Mount Volumes from the Host to Containers

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

docker run -v host_dir:/container_dir image

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

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

Docker Run Command Options

-dRun the container in the background.
-tAllocate a pseudo terminal.
--name=""Assign a name to the container. If not specified, docker will assign some random name to the docker container.
-pPublish a container's port, or range of ports, to the host.
-vMount folders and files from the host to the containers.
--rmAutomatically removes the container when it exits.
-hSets the hostname of the docker container.
Start new Ubuntu container.
Start new Ubuntu container.
List of all containers
List of all containers.
Start a new Apache HTTPD container with host port 80 maps to the container port 80
Start a new Apache HTTPD container with host port 80 maps to the container port 80.