The '
docker container run' command has the following syntax:
$ docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
The command '
docker container run' is a convenient method that executes two commands:
first it creates the container (
docker container create) and then it starts it (
docker container start) .
You can choose to run a container in the foreground (default) or background (option:
-d).
When running in the foreground, you can instruct Docker
to start an interactive session and allocate a pseudo terminal (TTY) to interact with the container (option:
-it).
There are three main scenarios:
-
Run a container in the foreground using docker container run without any flags.
In this mode, only the container's stdout and stderr are redirected to the local terminal.
The terminal's stdin is not connected to the container.
-
Run a container in foreground by using docker container run -it.
In this case, Docker redirects the local terminal stdin to the container.
The container's stdout and stderr is still redirected to the local terminal.
In fact, only the -i (interactive) flag is required to keep stdin open and redirect it to the container.
The -t (tty) flag allocates a pseudo-terminal, which enables full terminal capabilities such as colored output and cursor control.
Without the pseudo-TTY, we get plain text I/O.
-
The -d (detached) flag runs the container in the background (docker container run -d) and immediately returns the container ID.
You can use the three flags
-d,
-i, and
-t to run a container in detached mode while keeping it interactive.
This allows you to reattach to the container later using the
docker attach command.
To detach from the container you will need to use
Ctrl+P followed by
Ctrl+Q, otherwise
Ctrl+C may terminate it.
Instead of using the
-dit combination,
it's preferable to start the container in the background with just the
-d flag,
and later use
docker exec -it to start an interactive session.
This allows multiple concurrent interactive sessions and avoids the limitations of docker attach.
Here's a summary when to use each option:
|---------------------------|-------------------------------------------------------------------|
| docker run | foreground, no stdin (non-interactive containers) |
|---------------------------|-------------------------------------------------------------------|
| docker run -i | foreground with terminal interaction (plain text I/O) |
|---------------------------|-------------------------------------------------------------------|
| docker run -it | foreground with terminal interaction (interactive shell sessions) |
|---------------------------|-------------------------------------------------------------------|
| docker run -d | detached background container but interactive via exec |
|---------------------------|-------------------------------------------------------------------|
| docker run -dit | detached background container but interactive via attach |
|---------------------------|-------------------------------------------------------------------|
Let's run the nginx image:
$ docker container run --rm -d nginx:latest
c8a35eab0aabf68335f88268c3ed0dd6cb77a30503db4e9ba5cdd8ca0cf8c1ab
The option '
-d' (
--detach) run the container in background and print its ID.
The option '
--rm' instructs Docker to automatically remove the container when it exits.
Let's verify that nginx is up and running:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8a35eab0aab nginx:latest "/docker-entrypoint.…" 25 seconds ago Up 25 seconds 80/tcp blissful_kar
You can notice that the name of the container "vibrant_mose" which is a random name generated by Docker.
To give a custom name to the container, you can use the option '
--name'.
Note that the container's port (80) is not published to the Docker's host.
To publish an exposed container's port and map it to a host's port, you can use the option '
-p HOST_PORT:CONTAINER_PORT'.
You can also use the option '
-P' to publish all exposed ports to random ports in the Docker's host.
Let's use the two options ('
-p', '
--name') and run again the nginx image:
$ docker container run -d -p 8080:80 --name "nginx-latest" nginx:latest
e6c31a81c909695475a97a0cb851cea2befa2fbfe1340d8e760c50f06dd2fb5c
Let's verify that both the specified port (the container port 80 is mapped to the Docker host port 8080) and the name were used:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6c31a81c909 nginx:latest "/docker-entrypoint.…" 30 seconds ago Up 29 seconds 0.0.0.0:8080->80/tcp nginx-latest
c8a35eab0aab nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp blissful_kar
You can use the option '
--no-trunc' of the '
ps' command to print the full container ID and the command used to start the nginx process.
$ docker container ls --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6c31a81c909695475a97a0cb851cea2befa2fbfe1340d8e760c50f06dd2fb5c nginx:latest "/docker-entrypoint.sh nginx -g 'daemon off;'" 56 seconds ago Up 55 seconds 0.0.0.0:8080->80/tcp nginx-latest
c8a35eab0aabf68335f88268c3ed0dd6cb77a30503db4e9ba5cdd8ca0cf8c1ab nginx:latest "/docker-entrypoint.sh nginx -g 'daemon off;'" About a minute ago Up About a minute 80/tcp blissful_kare
We can also run a container in an interactive mode by using the options
-i and
-t:
$ docker container run --rm -i -t ubuntu:latest /bin/bash
root@651d1a7e5cd7:/# exit
exit
$
The flag
-i instructs docker to start an interactive session and connect the user's terminal with the container's
stdin/
stdout stream.
It allows the user to send command to the container.
The flag
-t instructs docker to allocate a pseudo terminal (TTY).
It provides the user a terminal interface to execute commands interactively.
Notes:
-
In the example above, the terminal prompt is different when we are running commands inside the container (root@651d1a7e5cd7:/#)
from the prompt when we are running commands inside the Docker host ($).
-
The prompt $ indicates that the containers execution completed and we returned to the host's terminal.
-
The host and container prompts might look different for you depending on your configuration.
-
Usually the container prompt inside the container takes this format CONTAINER_USER@CONTAINER_ID:/# or CONTAINER_USER@CONTAINER_HOST_NAME:/#.
-
The /# at the end indicates you're in the root directory of the container
(might look different for you, depending on the WORKDIR setting or where you navigate).
-
If you're not the root user in the container, you might see $ instead of #.
-
Some containers might have custom PS1 configurations that change this default format.
-
The host prompt could be $ or a custom prompt depending on your shell configuration and user privileges.
We can also run a specific commands (foreground) in the container:
$ docker container run --rm ubuntu:latest id
uid=0(root) gid=0(root) groups=0(root)
$
$ docker container run --rm ubuntu:latest cat /etc/hosts
127.0.0.1 localhost
172.17.0.2 ffd0458ac5e8
$
We can use the pipe character (
|) to connect the output of one command to another,
but be aware that the piping will occur on the host, not within the container.
To perform piped operations within the container, wrap the full command in quotes and use
/bin/bash -c:
$ docker container run --rm ubuntu:latest /bin/bash -c "cat /etc/hosts | grep 127.0.0.1"
127.0.0.1 localhost
$
The same, if we want to run multiple commands inside the container:
$ docker container run --rm ubuntu:latest /bin/bash -c "id; cat /etc/hosts | grep 127.0.0.1"
uid=0(root) gid=0(root) groups=0(root)
127.0.0.1 localhost
$
Because we have used the flag
--rm, the Ubuntu containers we created above will be automatically removed (deleted) by Docker
when we explicitly exit/stop the container or the command/process running in the container completes or terminates.
By default, a container is not restarted when it exits.
We can adjust this behavior, by using the option '
--restart'.
It accepts four values:
no (default),
always,
unless-stopped, and
on-failure:
- The first value (no) instructs Docker to not restart the container if it exits.
- The second value (always) instructs Docker to always restart the container whenever it exits.
- The third value (unless-stopped) instructs Docker to always restart the container whenever it exits, unless it's stopped.
- The last value (on-failure) instructs Docker to always restart the container whenever it exits with a nonzero exit code.
It's possible to use
on-failure with a number to instruct Docker to restart the container the number of times specified
(e.g., on-failure:2 instructs Docker to restart the container 2 times after which the container won't be restarted again).