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.
Prerequisites
- Command-line access.
- Administrative privileges on the system.
- Docker installed.
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.
Option | Description |
---|---|
-d , --detach | Run container in the background. |
-e , --env | Set environment variables. |
--entrypoint | Set a custom entry point for the container image. |
-i , --interactive | Attach the container's standard input. |
--label | Set metadata for the container. |
-m , --memory | Set container memory limit. |
--name | Provide a custom name for a container. |
-p , --publish | Publish a container port to a host. |
-t , --tty | Allocate 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.
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:
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
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:
Option | Description |
---|---|
-a , --attach | Attach STDOUT/STDERR. |
--detach-keys | Override the container detachment key sequence. |
-i , --interactive | Attach 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.
The following command starts the nginx-test container:
docker start nginx-test
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
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.
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.