How to Start Docker Containers

July 18, 2024

Introduction

Docker CLI is Docker's container management toolset with options to fine-tune containers and configure multi-container app deployments. Starting containers in Docker CLI is achieved with one of the two commands – docker run and docker start.

This article explains how to start Docker containers, introduces the available options, and provides examples.

How to start Docker containers.

Prerequisites

Create and Start New Container Using docker run

The docker run command (an alias of docker container run) allows a user to create and start a container using only an image as a command argument. Below is the basic syntax of the command:

docker run [options] [image] [commands]

Once the user executes docker run, Docker creates a container using the specified image template and automatically starts it.

docker run Options

Below is a selection of the most commonly used docker run options. The list includes arguments for naming containers, setting variables, configuring ports, etc.

OptionDescription
-d, --detachRun container in the background.
-e, --envSet environment variables.
--entrypointSet a custom entry point for the container image.
-i, --interactiveAttach the container's standard input.
--labelSet metadata for the container.
-m, --memorySet container memory limit.
--nameProvide a custom name for a container.
-p, --publishPublish a container port to a host.
-t, --ttyAllocate a pseudo-TTY.

docker run Examples

The docker run command simplifies container management by fitting all the container configuration parameters into a single command. The following example uses docker run to:

  • Create a container based on the official nginx image.
  • Map the external port 8080 to the container port 80.
  • Name the container nginx-test.
  • Run the container in detached mode, i.e., without attaching the terminal session to the container's standard output.

Below is the syntax for the described command:

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

Since the container runs in detached mode, Docker prints the container ID and exits to the shell prompt.

Running an Nginx container in detached mode.

To see if the container is running correctly, use the following command for listing running containers:

docker ps

The nginx-test container appears in the output:

Listing running Docker containers.

Another useful docker run option is the ability to execute commands inside running containers. To achieve this, use -i and -t flags and add a shell command as the last argument:

docker run -it [image] [command]

Replace [command] with a path to a shell available in the container to gain access to the container's shell prompt and be able to execute more than one command in succession.

For example, the command below creates a container ubuntu-test based on the ubuntu image, then runs the Bash shell in the container:

docker run -it --name ubuntu-test ubuntu /bin/bash
A Bash shell inside an Ubuntu container.

The Bash shell prompt appears after executing the command.

Start Stopped Container Using docker start

The docker start command (an alias of docker container start) starts one or more stopped containers. Unlike docker run, this command works only with already existing containers.

Below is the general command syntax for docker start:

docker start [options] [container1-name-or-id] [container2-name-or-id] [...]

docker start Options

The docker start command features three options, which deal with standard input, output, and error connections. The following table contains the options and their descriptions:

OptionDescription
-a, --attachAttach STDOUT/STDERR.
--detach-keysOverride the container detachment key sequence.
-i, --interactiveAttach the container's standard input.

docker start Examples

To use docker start, find a stopped container on the system. The -a flag added to docker ps lists all the available containers, both running and stopped:

docker ps -a

Stopped containers report their status as Exited. The example below shows the stopped nginx-test container.

Listing all Docker containers on the system.

The following command starts the nginx-test container:

docker start nginx-test
Starting an Nginx container.

To attach the terminal session to the container's standard output, use the -i flag. For example, the command below attaches to the nginx-test output:

docker start -i nginx-test
Starting an Nginx container with standard input attached.

docker start does not provide a way to execute commands or obtain shell access to the container. If necessary, this functionality can be achieved with the docker exec command. For example, to execute an interactive shell in the nginx-test container, type the following:

docker exec -it nginx-test /bin/sh

The sh shell prompt appears.

An sh shell in an Nginx container.

Note: The docker exec command works only on running containers. Start the container with docker start before using docker exec.

Conclusion

After reading this article, you should be able to use docker run and docker start to customize how your containers start and behave. The article provided a list of options for both commands and gave examples of their use.

If you work with many containers simultaneously and want to simplify the deployment procedure, read about Docker Compose, a useful multi-container deployment tool.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Docker Image vs. Container
May 23, 2024

This article compares Docker images and containers, explains how they are related, and outlines their differences.
Read more
How to Create Docker Image from Dockerfile
April 5, 2024

The most common Docker image creation method involves Dockerfile, a file with...
Read more
How to Check and Reduce Docker Image Size
April 10, 2024

Reducing the size of Docker images helps speed up container deployment. For large-scale...
Read more
How to Update Docker Image and Container
July 11, 2024

In this tutorial, learn how to update Docker images and running containers to the latest version.
Read more