2015-06-21 16:41:38 -04:00
# run
2023-01-06 13:04:05 -05:00
<!-- - MARKER_GEN_START -->
2022-03-29 17:58:36 -04:00
Create and run a new container from an image
2015-06-21 16:41:38 -04:00
2023-01-06 13:04:05 -05:00
### Aliases
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
2023-01-06 13:04:05 -05:00
`docker container run` , `docker run`
### Options
| Name | Type | Default | Description |
|:----------------------------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`--add-host` ](#add-host ) | `list` | | Add a custom host-to-IP mapping (host:ip) |
2023-04-03 04:08:36 -04:00
| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) |
2023-01-06 13:04:05 -05:00
| [`-a` ](#attach ), [`--attach` ](#attach ) | `list` | | Attach to STDIN, STDOUT or STDERR |
| `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
| `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) |
| `--cap-add` | `list` | | Add Linux capabilities |
| `--cap-drop` | `list` | | Drop Linux capabilities |
| `--cgroup-parent` | `string` | | Optional parent cgroup for the container |
| `--cgroupns` | `string` | | Cgroup namespace to use (host\|private)< br > 'host': Run the container in the Docker host's cgroup namespace< br > 'private': Run the container in its own private cgroup namespace< br > '': Use the cgroup namespace as configured by the< br > default-cgroupns-mode option on the daemon (default) |
| [`--cidfile` ](#cidfile ) | `string` | | Write the container ID to the file |
| `--cpu-count` | `int64` | `0` | CPU count (Windows only) |
| `--cpu-percent` | `int64` | `0` | CPU percent (Windows only) |
| `--cpu-period` | `int64` | `0` | Limit CPU CFS (Completely Fair Scheduler) period |
| `--cpu-quota` | `int64` | `0` | Limit CPU CFS (Completely Fair Scheduler) quota |
| `--cpu-rt-period` | `int64` | `0` | Limit CPU real-time period in microseconds |
| `--cpu-rt-runtime` | `int64` | `0` | Limit CPU real-time runtime in microseconds |
| `-c` , `--cpu-shares` | `int64` | `0` | CPU shares (relative weight) |
| `--cpus` | `decimal` | | Number of CPUs |
| `--cpuset-cpus` | `string` | | CPUs in which to allow execution (0-3, 0,1) |
| `--cpuset-mems` | `string` | | MEMs in which to allow execution (0-3, 0,1) |
| `-d` , `--detach` | | | Run container in background and print container ID |
2023-06-08 08:37:21 -04:00
| [`--detach-keys` ](#detach-keys ) | `string` | | Override the key sequence for detaching a container |
2023-01-06 13:04:05 -05:00
| [`--device` ](#device ) | `list` | | Add a host device to the container |
| [`--device-cgroup-rule` ](#device-cgroup-rule ) | `list` | | Add a rule to the cgroup allowed devices list |
| `--device-read-bps` | `list` | | Limit read rate (bytes per second) from a device |
| `--device-read-iops` | `list` | | Limit read rate (IO per second) from a device |
| `--device-write-bps` | `list` | | Limit write rate (bytes per second) to a device |
| `--device-write-iops` | `list` | | Limit write rate (IO per second) to a device |
| `--disable-content-trust` | | | Skip image verification |
| `--dns` | `list` | | Set custom DNS servers |
| `--dns-option` | `list` | | Set DNS options |
| `--dns-search` | `list` | | Set custom DNS search domains |
| `--domainname` | `string` | | Container NIS domain name |
| `--entrypoint` | `string` | | Overwrite the default ENTRYPOINT of the image |
| [`-e` ](#env ), [`--env` ](#env ) | `list` | | Set environment variables |
| `--env-file` | `list` | | Read in a file of environment variables |
| `--expose` | `list` | | Expose a port or a range of ports |
| [`--gpus` ](#gpus ) | `gpu-request` | | GPU devices to add to the container ('all' to pass all GPUs) |
| `--group-add` | `list` | | Add additional groups to join |
| `--health-cmd` | `string` | | Command to run to check health |
| `--health-interval` | `duration` | `0s` | Time between running the check (ms\|s\|m\|h) (default 0s) |
| `--health-retries` | `int` | `0` | Consecutive failures needed to report unhealthy |
| `--health-start-period` | `duration` | `0s` | Start period for the container to initialize before starting health-retries countdown (ms\|s\|m\|h) (default 0s) |
| `--health-timeout` | `duration` | `0s` | Maximum time to allow one check to run (ms\|s\|m\|h) (default 0s) |
| `--help` | | | Print usage |
| `-h` , `--hostname` | `string` | | Container host name |
| `--init` | | | Run an init inside the container that forwards signals and reaps processes |
| `-i` , `--interactive` | | | Keep STDIN open even if not attached |
| `--io-maxbandwidth` | `bytes` | `0` | Maximum IO bandwidth limit for the system drive (Windows only) |
| `--io-maxiops` | `uint64` | `0` | Maximum IOps limit for the system drive (Windows only) |
| `--ip` | `string` | | IPv4 address (e.g., 172.30.100.104) |
| `--ip6` | `string` | | IPv6 address (e.g., 2001:db8::33) |
| `--ipc` | `string` | | IPC mode to use |
| [`--isolation` ](#isolation ) | `string` | | Container isolation technology |
| `--kernel-memory` | `bytes` | `0` | Kernel memory limit |
| [`-l` ](#label ), [`--label` ](#label ) | `list` | | Set meta data on a container |
| `--label-file` | `list` | | Read in a line delimited file of labels |
| `--link` | `list` | | Add link to another container |
| `--link-local-ip` | `list` | | Container IPv4/IPv6 link-local addresses |
| `--log-driver` | `string` | | Logging driver for the container |
| `--log-opt` | `list` | | Log driver options |
| `--mac-address` | `string` | | Container MAC address (e.g., 92:d0:c6:0a:29:33) |
| [`-m` ](#memory ), [`--memory` ](#memory ) | `bytes` | `0` | Memory limit |
| `--memory-reservation` | `bytes` | `0` | Memory soft limit |
| `--memory-swap` | `bytes` | `0` | Swap limit equal to memory plus swap: '-1' to enable unlimited swap |
| `--memory-swappiness` | `int64` | `-1` | Tune container memory swappiness (0 to 100) |
| [`--mount` ](#mount ) | `mount` | | Attach a filesystem mount to the container |
| [`--name` ](#name ) | `string` | | Assign a name to the container |
| [`--network` ](#network ) | `network` | | Connect a container to a network |
| `--network-alias` | `list` | | Add network-scoped alias for the container |
| `--no-healthcheck` | | | Disable any container-specified HEALTHCHECK |
| `--oom-kill-disable` | | | Disable OOM Killer |
| `--oom-score-adj` | `int` | `0` | Tune host's OOM preferences (-1000 to 1000) |
| `--pid` | `string` | | PID namespace to use |
| `--pids-limit` | `int64` | `0` | Tune container pids limit (set -1 for unlimited) |
| `--platform` | `string` | | Set platform if server is multi-platform capable |
| [`--privileged` ](#privileged ) | | | Give extended privileges to this container |
| [`-p` ](#publish ), [`--publish` ](#publish ) | `list` | | Publish a container's port(s) to the host |
| `-P` , `--publish-all` | | | Publish all exposed ports to random ports |
| [`--pull` ](#pull ) | `string` | `missing` | Pull image before running (`always`, `missing` , `never` ) |
| `-q` , `--quiet` | | | Suppress the pull output |
2023-03-31 05:56:40 -04:00
| [`--read-only` ](#read-only ) | | | Mount the container's root filesystem as read only |
2023-01-06 13:04:05 -05:00
| [`--restart` ](#restart ) | `string` | `no` | Restart policy to apply when a container exits |
| `--rm` | | | Automatically remove the container when it exits |
| `--runtime` | `string` | | Runtime to use for this container |
| [`--security-opt` ](#security-opt ) | `list` | | Security Options |
| `--shm-size` | `bytes` | `0` | Size of /dev/shm |
| `--sig-proxy` | | | Proxy received signals to the process |
| [`--stop-signal` ](#stop-signal ) | `string` | | Signal to stop the container |
| [`--stop-timeout` ](#stop-timeout ) | `int` | `0` | Timeout (in seconds) to stop a container |
| [`--storage-opt` ](#storage-opt ) | `list` | | Storage driver options for the container |
| [`--sysctl` ](#sysctl ) | `map` | `map[]` | Sysctl options |
| [`--tmpfs` ](#tmpfs ) | `list` | | Mount a tmpfs directory |
| `-t` , `--tty` | | | Allocate a pseudo-TTY |
| [`--ulimit` ](#ulimit ) | `ulimit` | | Ulimit options |
| `-u` , `--user` | `string` | | Username or UID (format: < name \|uid > [:< group \|gid > ]) |
| `--userns` | `string` | | User namespace to use |
| `--uts` | `string` | | UTS namespace to use |
| [`-v` ](#volume ), [`--volume` ](#volume ) | `list` | | Bind mount a volume |
| `--volume-driver` | `string` | | Optional volume driver for the container |
| [`--volumes-from` ](#volumes-from ) | `list` | | Mount volumes from the specified container(s) |
| [`-w` ](#workdir ), [`--workdir` ](#workdir ) | `string` | | Working directory inside the container |
<!-- - MARKER_GEN_END -->
2015-06-21 16:41:38 -04:00
2017-03-09 13:04:51 -05:00
## Description
2017-02-07 18:42:48 -05:00
2023-03-31 05:56:40 -04:00
The `docker run` command runs a command in a new container, pulling the image if needed and starting the container.
You can restart a stopped container with all its previous changes intact using `docker start` .
Use `docker ps -a` to view a list of all containers, including those that are stopped.
2015-06-21 16:41:38 -04:00
## Examples
2023-01-06 13:28:29 -05:00
### <a name="name"></a> Assign name and allocate pseudo-TTY (--name, -it)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --name test -it debian
root@d6c0fe130dba:/# exit 13
$ echo $?
13
$ docker ps -a | grep test
d6c0fe130dba debian:7 "/bin/bash" 26 seconds ago Exited (13) 17 seconds ago test
```
2015-06-21 16:41:38 -04:00
2015-10-08 10:20:06 -04:00
This example runs a container named `test` using the `debian:latest`
2015-07-06 07:53:50 -04:00
image. The `-it` instructs Docker to allocate a pseudo-TTY connected to
the container's stdin; creating an interactive `bash` shell in the container.
2023-03-31 05:56:40 -04:00
The example quits the `bash` shell by entering
`exit 13` , passing the exit code on to the caller of
`docker run` , and recording it in the `test` container's metadata.
2015-06-21 16:41:38 -04:00
2023-01-06 13:28:29 -05:00
### <a name="cidfile"></a> Capture container ID (--cidfile)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
This creates a container and prints `test` to the console. The `cidfile`
2015-06-21 16:41:38 -04:00
flag makes Docker attempt to create a new file and write the container ID to it.
2023-03-31 05:56:40 -04:00
If the file exists already, Docker returns an error. Docker closes this
2015-06-21 16:41:38 -04:00
file when `docker run` exits.
2023-01-06 13:28:29 -05:00
### <a name="privileged"></a> Full container capabilities (--privileged)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -t -i --rm ubuntu bash
root@bc338942ef20:/# mount -t tmpfs none /mnt
mount: permission denied
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
This *doesn't* work, because by default, Docker drops most potentially dangerous kernel
capabilities, including `CAP_SYS_ADMIN ` (which is required to mount
filesystems). However, the `--privileged` flag allows it to run:
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -t -i --privileged ubuntu bash
root@50e3f57e16e6:/# mount -t tmpfs none /mnt
root@50e3f57e16e6:/# df -h
Filesystem Size Used Avail Use% Mounted on
none 1.9G 0 1.9G 0% /mnt
```
2015-06-21 16:41:38 -04:00
The `--privileged` flag gives *all* capabilities to the container, and it also
lifts all the limitations enforced by the `device` cgroup controller. In other
words, the container can then do almost everything that the host can do. This
flag exists to allow special use-cases, like running Docker within Docker.
2023-01-06 13:28:29 -05:00
### <a name="workdir"></a> Set working directory (-w, --workdir)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -w /path/to/dir/ -i -t ubuntu pwd
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
The `-w` option runs the command executed inside the directory specified, in this example,
`/path/to/dir/` . If the path does not exist, Docker creates it inside the container.
2015-06-21 16:41:38 -04:00
2023-01-06 13:28:29 -05:00
### <a name="storage-opt"></a> Set storage driver options per container (--storage-opt)
2016-03-20 00:42:58 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -it --storage-opt size=120G fedora /bin/bash
```
2016-03-20 00:42:58 -04:00
2023-03-31 05:56:40 -04:00
This (size) constraints the container filesystem size to 120G at creation time.
2016-09-18 15:35:55 -04:00
This option is only available for the `devicemapper` , `btrfs` , `overlay2` ,
2023-03-31 05:56:40 -04:00
`windowsfilter` and `zfs` storage drivers.
2016-09-18 15:35:55 -04:00
For the `overlay2` storage driver, the size option is only available if the
2022-10-28 11:12:54 -04:00
backing filesystem is `xfs` and mounted with the `pquota` mount option.
2023-03-31 05:56:40 -04:00
Under these conditions, you can pass any size less than the backing filesystem size.
For the `windowsfilter` , `devicemapper` , `btrfs` , and `zfs` storage drivers,
you cannot pass a size less than the Default BaseFS Size.
2016-03-20 00:42:58 -04:00
2023-01-06 13:28:29 -05:00
### <a name="tmpfs"></a> Mount tmpfs (--tmpfs)
2015-12-01 13:39:34 -05:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -d --tmpfs /run:rw,noexec,nosuid,size=65536k my_image
```
2015-12-01 13:39:34 -05:00
2016-01-26 20:39:13 -05:00
The `--tmpfs` flag mounts an empty tmpfs into the container with the `rw` ,
`noexec` , `nosuid` , `size=65536k` options.
2015-12-01 13:39:34 -05:00
2023-03-31 05:56:40 -04:00
### <a name="volume"></a> Mount volume (-v)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2023-03-31 05:56:40 -04:00
$ docker run -v $(pwd):$(pwd) -w $(pwd) -i -t ubuntu pwd
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
The example above mounts the current directory into the container at the same path
using the `-v` flag, sets it as the working directory, and then runs the `pwd` command inside the container.
As of Docker Engine version 23, you can use relative paths on the host.
```console
$ docker run -v ./content:/content -w /content -i -t ubuntu pwd
```
The example above mounts the `content` directory in the current directory into the container at the
`/content` path using the `-v` flag, sets it as the working directory, and then
runs the `pwd` command inside the container.
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash
```
2015-06-21 16:41:38 -04:00
When the host directory of a bind-mounted volume doesn't exist, Docker
2023-03-31 05:56:40 -04:00
automatically creates this directory on the host for you. In the
example above, Docker creates the `/doesnt/exist`
2015-06-21 16:41:38 -04:00
folder before starting your container.
2023-03-31 05:56:40 -04:00
### <a name="read-only"></a> Mount volume read-only (--read-only)
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --read-only -v /icanwrite busybox touch /icanwrite/here
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
You can use volumes in combination with the `--read-only` flag to control where
2015-06-21 16:41:38 -04:00
a container writes files. The `--read-only` flag mounts the container's root
filesystem as read only prohibiting writes to locations other than the
specified volumes for the container.
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v /path/to/static-docker-binary:/usr/bin/docker busybox sh
```
2015-06-21 16:41:38 -04:00
2022-10-28 11:12:54 -04:00
By bind-mounting the Docker Unix socket and statically linked Docker
binary (refer to [get the Linux binary ](https://docs.docker.com/engine/install/binaries/#install-static-binaries )),
2016-01-19 00:30:33 -05:00
you give the container the full access to create and manipulate the host's
Docker daemon.
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
On Windows, you must specify the paths using Windows-style path semantics.
2016-11-23 17:13:50 -05:00
2017-02-07 18:42:48 -05:00
```powershell
PS C:\> docker run -v c:\foo:c:\dest microsoft/nanoserver cmd /s /c type c:\dest\somefile.txt
Contents of file
2016-11-23 17:13:50 -05:00
2017-02-07 18:42:48 -05:00
PS C:\> docker run -v c:\foo:d: microsoft/nanoserver cmd /s /c type d:\somefile.txt
Contents of file
```
2023-03-31 05:56:40 -04:00
The following examples fails when using Windows-based containers, as the
2017-08-19 10:13:29 -04:00
destination of a volume or bind mount inside the container must be one of:
2023-03-31 05:56:40 -04:00
a non-existing or empty directory; or a drive other than `C:` . Further, the source
2016-11-23 17:13:50 -05:00
of a bind mount must be a local directory, not a file.
2017-02-07 18:42:48 -05:00
```powershell
net use z: \\remotemachine\share
docker run -v z:\foo:c:\dest ...
docker run -v \\uncpath\to\directory:c:\dest ...
docker run -v c:\foo\somefile.txt:c:\dest ...
docker run -v c:\foo:c: ...
docker run -v c:\foo:c:\existing-directory-with-contents ...
```
2016-11-23 17:13:50 -05:00
2020-04-19 09:43:08 -04:00
For in-depth information about volumes, refer to [manage data in containers ](https://docs.docker.com/storage/volumes/ )
2016-08-15 17:51:15 -04:00
2023-01-06 13:28:29 -05:00
### <a name="mount"></a> Add bind mounts or volumes using the --mount flag
2017-03-31 02:41:45 -04:00
2023-03-31 05:56:40 -04:00
The `--mount` flag allows you to mount volumes, host-directories, and `tmpfs`
2017-03-31 02:41:45 -04:00
mounts in a container.
2023-03-31 05:56:40 -04:00
The `--mount` flag supports most options supported by the `-v` or the
2017-03-31 02:41:45 -04:00
`--volume` flag, but uses a different syntax. For in-depth information on the
`--mount` flag, and a comparison between `--volume` and `--mount` , refer to
2022-10-28 11:12:54 -04:00
[Bind mounts ](https://docs.docker.com/storage/bind-mounts/ ).
2017-03-31 02:41:45 -04:00
Even though there is no plan to deprecate `--volume` , usage of `--mount` is recommended.
Examples:
2021-08-21 08:54:14 -04:00
```console
2017-03-31 02:41:45 -04:00
$ docker run --read-only --mount type=volume,target=/icanwrite busybox touch /icanwrite/here
```
2021-08-21 08:54:14 -04:00
```console
2017-03-31 02:41:45 -04:00
$ docker run -t -i --mount type=bind,src=/data,dst=/data busybox sh
```
2023-01-06 13:28:29 -05:00
### <a name="publish"></a> Publish or expose port (-p, --expose)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2018-02-26 01:27:11 -05:00
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2018-02-26 01:27:11 -05:00
This binds port `8080` of the container to TCP port `80` on `127.0.0.1` of the host
machine. You can also specify `udp` and `sctp` ports.
2020-04-19 09:43:08 -04:00
The [Docker User Guide ](https://docs.docker.com/network/links/ )
2023-03-31 05:56:40 -04:00
explains in detail how to use ports in Docker.
2015-06-21 16:41:38 -04:00
2019-09-26 14:12:54 -04:00
Note that ports which are not bound to the host (i.e., `-p 80:80` instead of
2023-03-31 05:56:40 -04:00
`-p 127.0.0.1:80:80` ) are externally accessible. This also applies if
2021-08-26 02:36:25 -04:00
you configured UFW to block this specific port, as Docker manages its
2019-09-26 14:12:54 -04:00
own iptables rules. [Read more ](https://docs.docker.com/network/iptables/ )
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --expose 80 ubuntu bash
```
2015-06-21 16:41:38 -04:00
2015-09-30 16:11:36 -04:00
This exposes port `80` of the container without publishing the port to the host
system's interfaces.
2015-06-21 16:41:38 -04:00
2021-12-03 16:55:07 -05:00
### <a name="pull"></a> Set the pull policy (--pull)
Use the `--pull` flag to set the image pull policy when creating (and running)
the container.
The `--pull` flag can take one of these values:
| Value | Description |
|:--------------------|:------------------------------------------------------------------------------------------------------------------|
| `missing` (default) | Pull the image if it was not found in the image cache, or use the cached image otherwise. |
| `never` | Do not pull the image, even if it's missing, and produce an error if the image does not exist in the image cache. |
| `always` | Always perform a pull before creating the container. |
When creating (and running) a container from an image, the daemon checks if the
image exists in the local image cache. If the image is missing, an error is
2022-10-28 11:12:54 -04:00
returned to the CLI, allowing it to initiate a pull.
2021-12-03 16:55:07 -05:00
2023-03-31 05:56:40 -04:00
The default (`missing`) is to only pull the image if it's not present in the
2021-12-03 16:55:07 -05:00
daemon's image cache. This default allows you to run images that only exist
locally (for example, images you built from a Dockerfile, but that have not
been pushed to a registry), and reduces networking.
The `always` option always initiates a pull before creating the container. This
option makes sure the image is up-to-date, and prevents you from using outdated
images, but may not be suitable in situations where you want to test a locally
built image before pushing (as pulling the image overwrites the existing image
in the image cache).
The `never` option disables (implicit) pulling images when creating containers,
and only uses images that are available in the image cache. If the specified
image is not found, an error is produced, and the container is not created.
This option is useful in situations where networking is not available, or to
prevent images from being pulled implicitly when creating containers.
The following example shows `docker run` with the `--pull=never` option set,
which produces en error as the image is missing in the image-cache:
```console
$ docker run --pull=never hello-world
docker: Error response from daemon: No such image: hello-world:latest.
```
2023-01-06 13:28:29 -05:00
### <a name="env"></a> Set environment variables (-e, --env, --env-file)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash
```
2015-06-21 16:41:38 -04:00
2017-03-28 13:19:28 -04:00
Use the `-e` , `--env` , and `--env-file` flags to set simple (non-array)
environment variables in the container you're running, or overwrite variables
2023-03-31 05:56:40 -04:00
defined in the Dockerfile of the image you're running.
2015-06-21 16:41:38 -04:00
2017-03-28 13:19:28 -04:00
You can define the variable and its value when running the container:
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-03-28 13:19:28 -04:00
$ docker run --env VAR1=value1 --env VAR2=value2 ubuntu env | grep VAR
VAR1=value1
VAR2=value2
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
You can also use variables exported to your local environment:
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-03-28 13:19:28 -04:00
export VAR1=value1
export VAR2=value2
$ docker run --env VAR1 --env VAR2 ubuntu env | grep VAR
VAR1=value1
VAR2=value2
```
When running the command, the Docker CLI client checks the value the variable
has in your local environment and passes it to the container.
If no `=` is provided and that variable is not exported in your local
2023-03-31 05:56:40 -04:00
environment, the variable isn't set in the container.
2017-03-28 13:19:28 -04:00
You can also load the environment variables from a file. This file should use
2017-04-27 05:12:48 -04:00
the syntax `<variable>=value` (which sets the variable to the given value) or
`<variable>` (which takes the value from the local environment), and `#` for comments.
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-03-28 13:19:28 -04:00
$ cat env.list
# This is a comment
VAR1=value1
VAR2=value2
2017-04-27 05:12:48 -04:00
USER
2017-02-07 18:42:48 -05:00
2021-02-22 16:00:43 -05:00
$ docker run --env-file env.list ubuntu env | grep -E 'VAR|USER'
2017-03-28 13:19:28 -04:00
VAR1=value1
VAR2=value2
2021-02-22 16:00:43 -05:00
USER=jonzeolla
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2023-01-06 13:28:29 -05:00
### <a name="label"></a> Set metadata on container (-l, --label, --label-file)
2015-09-27 21:45:10 -04:00
A label is a `key=value` pair that applies metadata to a container. To label a container with two labels:
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -l my-label --label com.example.foo=bar ubuntu bash
```
2015-06-21 16:41:38 -04:00
The `my-label` key doesn't specify a value so the label defaults to an empty
2018-09-28 17:40:22 -04:00
string (`""`). To add multiple labels, repeat the label flag (`-l` or `--label` ).
2015-06-21 16:41:38 -04:00
The `key=value` must be unique to avoid overwriting the label value. If you
specify labels with identical keys but different values, each subsequent value
overwrites the previous. Docker uses the last `key=value` you supply.
Use the `--label-file` flag to load multiple labels from a file. Delimit each
label in the file with an EOL mark. The example below loads labels from a
labels file in the current directory:
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --label-file ./labels ubuntu bash
```
2015-06-21 16:41:38 -04:00
The label-file format is similar to the format for loading environment
variables. (Unlike environment variables, labels are not visible to processes
2023-03-31 05:56:40 -04:00
running inside a container.) The following example shows a label-file
2015-06-21 16:41:38 -04:00
format:
2020-05-11 11:32:52 -04:00
```console
2017-02-07 18:42:48 -05:00
com.example.label1="a label"
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
# this is a comment
com.example.label2=another\ label
com.example.label3
```
2015-06-21 16:41:38 -04:00
You can load multiple label-files by supplying multiple `--label-file` flags.
For additional information on working with labels, see [*Labels - custom
2020-04-19 09:43:08 -04:00
metadata in Docker*](https://docs.docker.com/config/labels-custom-metadata/) in
the Docker User Guide.
2015-06-21 16:41:38 -04:00
2023-01-06 13:28:29 -05:00
### <a name="network"></a> Connect a container to a network (--network)
2015-09-30 16:11:36 -04:00
2023-03-31 05:56:40 -04:00
To start a container and connect it to a network, use the `--network` option.
The following commands create a network named `my-net` and adds a `busybox` container
2022-10-28 11:12:54 -04:00
to the `my-net` network.
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2022-10-28 11:12:54 -04:00
$ docker network create my-net
2016-06-06 19:33:00 -04:00
$ docker run -itd --network=my-net busybox
2015-09-30 16:11:36 -04:00
```
2015-06-21 16:41:38 -04:00
2016-01-11 21:03:40 -05:00
You can also choose the IP addresses for the container with `--ip` and `--ip6`
2023-06-26 09:42:50 -04:00
flags when you start the container on a user-defined network. To assign a
static IP to containers, you must specify subnet block for the network.
2016-01-11 21:03:40 -05:00
2021-08-21 08:54:14 -04:00
```console
2023-06-26 09:42:50 -04:00
$ docker network create --subnet 192.0.2.0/24 my-net
$ docker run -itd --network=my-net --ip=192.0.2.69 busybox
2016-01-11 21:03:40 -05:00
```
2015-09-30 16:11:36 -04:00
If you want to add a running container to a network use the `docker network connect` subcommand.
2015-06-21 16:41:38 -04:00
2015-09-30 16:11:36 -04:00
You can connect multiple containers to the same network. Once connected, the
2023-03-31 05:56:40 -04:00
containers can communicate using only another container's IP address
2015-09-30 16:11:36 -04:00
or name. For `overlay` networks or custom plugins that support multi-host
connectivity, containers connected to the same multi-host network but launched
from different Engines can also communicate in this way.
2015-06-21 16:41:38 -04:00
2020-04-19 09:43:08 -04:00
> **Note**
>
2022-10-28 11:12:54 -04:00
> The default bridge network only allow containers to communicate with each other using
> internal IP addresses. User-created bridge networks provide DNS resolution between
> containers using container names.
2015-09-30 16:11:36 -04:00
You can disconnect a container from a network using the `docker network
disconnect` command.
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
For more information on connecting a container to a network when using the `run` command, see the ["*Docker network overview*" ](https://docs.docker.com/network/ ).
2023-01-06 13:28:29 -05:00
### <a name="volumes-from"></a> Mount volumes from container (--volumes-from)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --volumes-from 777f7dc92da7 --volumes-from ba8c0c54f0f2:ro -i -t ubuntu pwd
```
2015-06-21 16:41:38 -04:00
The `--volumes-from` flag mounts all the defined volumes from the referenced
2023-03-31 05:56:40 -04:00
containers. You can specify more than one container by repetitions of the `--volumes-from`
2015-06-21 16:41:38 -04:00
argument. The container ID may be optionally suffixed with `:ro` or `:rw` to
mount the volumes in read-only or read-write mode, respectively. By default,
2023-03-31 05:56:40 -04:00
Docker mounts the volumes in the same mode (read write or read only) as
2015-06-21 16:41:38 -04:00
the reference container.
2023-03-31 05:56:40 -04:00
Labeling systems like SELinux require placing proper labels on volume
2015-06-21 16:41:38 -04:00
content mounted into a container. Without a label, the security system might
prevent the processes running inside the container from using the content. By
default, Docker does not change the labels set by the OS.
To change the label in the container context, you can add either of two suffixes
`:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file
objects on the shared volumes. The `z` option tells Docker that two containers
share the volume content. As a result, Docker labels the content with a shared
content label. Shared volume labels allow all containers to read/write content.
The `Z` option tells Docker to label the content with a private unshared label.
Only the current container can use a private volume.
2023-01-06 13:28:29 -05:00
### <a name="attach"></a> Attach to STDIN/STDOUT/STDERR (-a, --attach)
2015-09-27 21:45:10 -04:00
2022-03-30 09:05:29 -04:00
The `--attach` (or `-a` ) flag tells `docker run` to bind to the container's
`STDIN` , `STDOUT` or `STDERR` . This makes it possible to manipulate the output
and input as needed.
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ echo "test" | docker run -i -a stdin ubuntu cat -
```
2015-06-21 16:41:38 -04:00
This pipes data into a container and prints the container's ID by attaching
only to the container's `STDIN` .
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -a stderr ubuntu echo test
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
This isn't going to print anything to the console unless there's an error because output
is only attached to the `STDERR` of the container. The container's logs
still store what's written to `STDERR` and `STDOUT` .
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ cat somefile | docker run -i -a stdin mybuilder dobuild
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
This example shows a way of using `--attach` to pipe a file into a container.
The command prints the container's ID after the build completes and you can retrieve
the build logs using `docker logs` . This is
2015-06-21 16:41:38 -04:00
useful if you need to pipe a file or something else into a container and
retrieve the container's ID once the container has finished running.
2022-10-31 15:24:11 -04:00
See also [the `docker cp` command ](cp.md ).
2022-10-28 11:12:54 -04:00
2023-06-08 08:37:21 -04:00
### <a name="detach-keys"></a> Override the detach sequence (--detach-keys)
Use the `--detach-keys` option to override the Docker key sequence for detach.
This is useful if the Docker default sequence conflicts with key sequence you
use for other applications. There are two ways to define your own detach key
sequence, as a per-container override or as a configuration property on your
entire configuration.
To override the sequence for an individual container, use the
`--detach-keys="<sequence>"` flag with the `docker attach` command. The format of
the `<sequence>` is either a letter [a-Z], or the `ctrl-` combined with any of
the following:
* `a-z` (a single lowercase alpha character )
* `@` (at sign)
* `[` (left bracket)
* `\\` (two backward slashes)
* `_` (underscore)
* `^` (caret)
These `a` , `ctrl-a` , `X` , or `ctrl-\\` values are all examples of valid key
sequences. To configure a different configuration default key sequence for all
containers, see [**Configuration file** section ](cli.md#configuration-files ).
2023-01-06 13:28:29 -05:00
### <a name="device"></a> Add host device to container (--device)
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2022-10-10 18:29:42 -04:00
$ docker run -it --rm \
--device=/dev/sdc:/dev/xvdc \
--device=/dev/sdd \
--device=/dev/zero:/dev/foobar \
ubuntu ls -l /dev/{xvdc,sdd,foobar}
2017-02-07 18:42:48 -05:00
brw-rw---- 1 root disk 8, 2 Feb 9 16:05 /dev/xvdc
brw-rw---- 1 root disk 8, 3 Feb 9 16:05 /dev/sdd
2022-10-10 18:29:42 -04:00
crw-rw-rw- 1 root root 1, 5 Feb 9 16:05 /dev/foobar
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
It's often necessary to directly expose devices to a container. The `--device`
option enables that. For example, adding a specific block storage device or loop
device or audio device to an otherwise unprivileged container
2015-06-21 16:41:38 -04:00
(without the `--privileged` flag) and have the application directly access it.
2023-03-31 05:56:40 -04:00
By default, the container is able to `read` , `write` and `mknod` these devices.
2015-06-21 16:41:38 -04:00
This can be overridden using a third `:rwm` set of options to each `--device`
2023-03-31 05:56:40 -04:00
flag. If the container is running in privileged mode, then Docker ignores the
specified permissions.
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
Command (m for help): q
$ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc
You will not be able to write the partition table.
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
Command (m for help): q
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
$ docker run --device=/dev/sda:/dev/xvdc:rw --rm -it ubuntu fdisk /dev/xvdc
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
Command (m for help): q
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
$ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc
fdisk: unable to open /dev/xvdc: Operation not permitted
```
2015-06-21 16:41:38 -04:00
2020-04-19 09:43:08 -04:00
> **Note**
>
2023-03-31 05:56:40 -04:00
> The `--device` option cannot be safely used with ephemeral devices. You shouldn't
> add block devices that may be removed to untrusted containers with `--device`.
2015-06-21 16:41:38 -04:00
2019-02-07 12:56:05 -05:00
For Windows, the format of the string passed to the `--device` option is in
the form of `--device=<IdType>/<Id>` . Beginning with Windows Server 2019
and Windows 10 October 2018 Update, Windows only supports an IdType of
`class` and the Id as a [device interface class
GUID](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/overview-of-device-interface-classes).
Refer to the table defined in the [Windows container
docs](https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/hardware-devices-in-containers)
for a list of container-supported device interface class GUIDs.
2023-03-31 05:56:40 -04:00
If you specify this option for a process-isolated Windows container, Docker makes
_all_ devices that implement the requested device interface class GUID
2019-02-07 12:56:05 -05:00
available in the container. For example, the command below makes all COM
ports on the host visible in the container.
```powershell
PS C:\> docker run --device=class/86E0D1E0-8089-11D0-9CE4-08003E301F73 mcr.microsoft.com/windows/servercore:ltsc2019
```
2020-04-19 09:43:08 -04:00
> **Note**
>
2022-03-01 06:46:49 -05:00
> The `--device` option is only supported on process-isolated Windows containers,
> and produces an error if the container isolation is `hyperv`.
2019-02-07 12:56:05 -05:00
2022-03-01 06:29:54 -05:00
### <a name="device-cgroup-rule"></a> Using dynamically created devices (--device-cgroup-rule)
2023-03-31 05:56:40 -04:00
Docker assigns devices available to a container at creation time. The
assigned devices are added to the cgroup.allow file and
created into the container when it runs. This poses a problem when
you need to add a new device to running container.
2022-03-01 06:29:54 -05:00
2023-03-31 05:56:40 -04:00
One solution is to add a more permissive rule to a container
2022-03-01 06:29:54 -05:00
allowing it access to a wider range of devices. For example, supposing
2023-03-31 05:56:40 -04:00
the container needs access to a character device with major `42` and
any number of minor numbers (added as new devices appear), add the
following rule:
2022-03-01 06:29:54 -05:00
```console
$ docker run -d --device-cgroup-rule='c 42:* rmw' -name my-container my-image
```
Then, a user could ask `udev` to execute a script that would `docker exec my-container mknod newDevX c 42 <minor>`
the required device when it is added.
2023-03-31 05:56:40 -04:00
> **Note**: You still need to explicitly add initially present devices to the
2022-03-01 06:29:54 -05:00
> `docker run` / `docker create` command.
2023-01-06 13:28:29 -05:00
### <a name="gpus"></a> Access an NVIDIA GPU
2019-06-21 18:40:02 -04:00
2022-03-01 06:29:54 -05:00
The `--gpus` flag allows you to access NVIDIA GPU resources. First you need to
2023-03-31 05:56:40 -04:00
install the [nvidia-container-runtime ](https://nvidia.github.io/nvidia-container-runtime/ ).
Read [Specify a container's resources ](https://docs.docker.com/config/containers/resource_constraints/ )
2019-06-21 18:40:02 -04:00
for more information.
2023-03-31 05:56:40 -04:00
To use `--gpus` , specify which GPUs (or all) to use. If you provide no value, Docker uses all
available GPUs. The example below exposes all available GPUs.
2019-06-21 18:40:02 -04:00
2021-08-21 08:54:14 -04:00
```console
2019-06-21 18:40:02 -04:00
$ docker run -it --rm --gpus all ubuntu nvidia-smi
```
Use the `device` option to specify GPUs. The example below exposes a specific
GPU.
2021-08-21 08:54:14 -04:00
```console
2019-06-21 18:40:02 -04:00
$ docker run -it --rm --gpus device=GPU-3a23c669-1f69-c64e-cf85-44e9b07e7a2a ubuntu nvidia-smi
```
The example below exposes the first and third GPUs.
2021-08-21 08:54:14 -04:00
```console
2022-01-30 03:23:43 -05:00
$ docker run -it --rm --gpus '"device=0,2"' nvidia-smi
2019-06-21 18:40:02 -04:00
```
2023-01-06 13:28:29 -05:00
### <a name="restart"></a> Restart policies (--restart)
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
Use the `--restart` flag to specify a container's *restart policy* . A restart
2015-06-21 16:41:38 -04:00
policy controls whether the Docker daemon restarts a container after exit.
Docker supports the following restart policies:
2017-12-14 16:50:56 -05:00
| Policy | Result |
|:---------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `no` | Do not automatically restart the container when it exits. This is the default. |
| `on-failure[:max-retries]` | Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts. |
2023-03-31 05:56:40 -04:00
| `unless-stopped` | Restart the container unless it's explicitly stopped or Docker itself is stopped or restarted. |
| `always` | Always restart the container regardless of the exit status. When you specify always, the Docker daemon tries to restart the container indefinitely. The container always starts on daemon startup, regardless of the current state of the container. |
2017-02-07 18:42:48 -05:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --restart=always redis
```
2015-06-21 16:41:38 -04:00
This will run the `redis` container with a restart policy of **always**
2023-03-31 05:56:40 -04:00
so that if the container exits, Docker restarts it.
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
You can find more detailed information on restart policies in the
2018-03-21 14:00:11 -04:00
[Restart Policies (--restart) ](../run.md#restart-policies---restart )
2015-06-21 16:41:38 -04:00
section of the Docker run reference page.
2023-01-06 13:28:29 -05:00
### <a name="add-host"></a> Add entries to container hosts file (--add-host)
2015-06-21 16:41:38 -04:00
You can add other hosts into a container's `/etc/hosts` file by using one or
more `--add-host` flags. This example adds a static address for a host named
`docker` :
2021-08-21 08:54:14 -04:00
```console
$ docker run --add-host=docker:93.184.216.34 --rm -it alpine
2017-02-07 18:42:48 -05:00
2021-08-21 08:54:14 -04:00
/ # ping docker
PING docker (93.184.216.34): 56 data bytes
64 bytes from 93.184.216.34: seq=0 ttl=37 time=93.052 ms
64 bytes from 93.184.216.34: seq=1 ttl=37 time=92.467 ms
64 bytes from 93.184.216.34: seq=2 ttl=37 time=92.252 ms
^C
--- docker ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 92.209/92.495/93.052 ms
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2023-07-03 10:34:55 -04:00
The `--add-host` flag supports a special `host-gateway` value that resolves to
the internal IP address of the host. This is useful when you want containers to
connect to services running on the host machine.
2015-06-21 16:41:38 -04:00
2023-07-03 10:34:55 -04:00
It's conventional to use `host.docker.internal` as the hostname referring to
`host-gateway` . Docker Desktop automatically resolves this hostname, see
[Explore networking features ](https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host ).
The following example shows how the special `host-gateway` value works. The
example runs an HTTP server that serves a file from host to container over the
`host.docker.internal` hostname, which resolves to the host's internal IP.
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2023-07-03 10:34:55 -04:00
$ echo "hello from host!" > ./hello
$ python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
$ docker run \
--add-host host.docker.internal:host-gateway \
curlimages/curl -s host.docker.internal:8000/hello
hello from host!
2017-02-07 18:42:48 -05:00
```
2015-06-21 16:41:38 -04:00
2023-01-06 13:28:29 -05:00
### <a name="ulimit"></a> Set ulimits in container (--ulimit)
2015-06-21 16:41:38 -04:00
Since setting `ulimit` settings in a container requires extra privileges not
available in the default container, you can set these using the `--ulimit` flag.
2023-03-31 05:56:40 -04:00
Specify `--ulimit` with a soft and hard limit in the format
`<type>=<soft limit>[:<hard limit>]` . For example:
2015-06-21 16:41:38 -04:00
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --ulimit nofile=1024:1024 --rm debian sh -c "ulimit -n"
1024
```
2015-06-21 16:41:38 -04:00
2020-04-19 09:43:08 -04:00
> **Note**
>
2023-03-31 05:56:40 -04:00
> If you don't provide a hard limit value, Docker uses the soft limit value
> for both values. If you don't provide any values, they are inherited from
> the default `ulimits` set on the daemon.
> **Note**
>
> The `as` option is deprecated.
2015-06-21 16:41:38 -04:00
> In other words, the following script is not supported:
2017-02-07 18:42:48 -05:00
>
2021-08-21 08:54:14 -04:00
> ```console
2021-10-18 08:49:18 -04:00
> $ docker run -it --ulimit as=1024 fedora /bin/bash
2017-02-07 18:42:48 -05:00
> ```
2015-06-21 16:41:38 -04:00
2023-03-31 05:56:40 -04:00
Docker sends the values to the appropriate OS `syscall` and doesn't perform any byte conversion.
Take this into account when setting the values.
2015-06-28 10:40:53 -04:00
2015-09-27 21:45:10 -04:00
#### For `nproc` usage
2015-06-28 10:40:53 -04:00
2023-03-31 05:56:40 -04:00
Be careful setting `nproc` with the `ulimit` flag as Linux uses `nproc` to set the
2022-10-28 11:12:54 -04:00
maximum number of processes available to a user, not to a container. For example, start four
2015-06-28 10:40:53 -04:00
containers with `daemon` user:
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run -d -u daemon --ulimit nproc=3 busybox top
$ docker run -d -u daemon --ulimit nproc=3 busybox top
$ docker run -d -u daemon --ulimit nproc=3 busybox top
$ docker run -d -u daemon --ulimit nproc=3 busybox top
```
2015-06-28 10:40:53 -04:00
2023-03-31 05:56:40 -04:00
The 4th container fails and reports a "[8] System error: resource temporarily unavailable" error.
2015-10-08 10:20:06 -04:00
This fails because the caller set `nproc=3` resulting in the first three containers using up
2015-06-28 10:40:53 -04:00
the three processes quota set for the `daemon` user.
2015-08-18 13:30:44 -04:00
2023-01-06 13:28:29 -05:00
### <a name="stop-signal"></a> Stop container with signal (--stop-signal)
2015-08-18 13:30:44 -04:00
2023-03-31 05:56:40 -04:00
The `--stop-signal` flag sends the system call signal to the
2021-08-21 07:50:05 -04:00
container to exit. This signal can be a signal name in the format `SIG<NAME>` ,
for instance `SIGKILL` , or an unsigned number that matches a position in the
kernel's syscall table, for instance `9` .
2023-03-31 05:56:40 -04:00
The default value is defined by [`STOPSIGNAL` ](https://docs.docker.com/engine/reference/builder/#stopsignal )
2021-08-11 05:00:27 -04:00
in the image, or `SIGTERM` if the image has no `STOPSIGNAL` defined.
2015-11-09 04:11:10 -05:00
2023-01-06 13:28:29 -05:00
### <a name="security-opt"></a> Optional security options (--security-opt)
2016-06-07 15:15:50 -04:00
2023-03-31 05:56:40 -04:00
On Windows, you can use this flag to specify the `credentialspec` option.
2016-10-19 13:25:45 -04:00
The `credentialspec` must be in the format `file://spec.txt` or `registry://keyname` .
2016-06-07 15:15:50 -04:00
2023-01-06 13:28:29 -05:00
### <a name="stop-timeout"></a> Stop container with timeout (--stop-timeout)
2016-05-26 16:34:48 -04:00
2021-08-21 07:50:05 -04:00
The `--stop-timeout` flag sets the number of seconds to wait for the container
to stop after sending the pre-defined (see `--stop-signal` ) system call signal.
2023-03-31 05:56:40 -04:00
If the container does not exit after the timeout elapses, it's forcibly killed
2021-08-21 07:50:05 -04:00
with a `SIGKILL` signal.
2023-03-31 05:56:40 -04:00
If you set `--stop-timeout` to `-1` , no timeout is applied, and the daemon
waits indefinitely for the container to exit.
2021-08-21 07:50:05 -04:00
2023-03-31 05:56:40 -04:00
The Daemon determines the default, and is 10 seconds for Linux containers,
2021-08-21 07:50:05 -04:00
and 30 seconds for Windows containers.
2016-05-26 16:34:48 -04:00
2023-01-06 13:28:29 -05:00
### <a name="isolation"></a> Specify isolation technology for container (--isolation)
2015-11-09 04:11:10 -05:00
This option is useful in situations where you are running Docker containers on
2022-03-01 06:29:54 -05:00
Windows. The `--isolation=<value>` option sets a container's isolation technology.
On Linux, the only supported is the `default` option which uses Linux namespaces.
These two commands are equivalent on Linux:
2015-11-09 04:11:10 -05:00
2021-08-21 08:54:14 -04:00
```console
2015-11-09 04:11:10 -05:00
$ docker run -d busybox top
$ docker run -d --isolation default busybox top
```
2017-01-03 19:17:55 -05:00
On Windows, `--isolation` can take one of these values:
2015-11-09 04:11:10 -05:00
2022-03-01 06:29:54 -05:00
| Value | Description |
|:----------|:-------------------------------------------------------------------------------------------|
| `default` | Use the value specified by the Docker daemon's `--exec-opt` or system default (see below). |
| `process` | Shared-kernel namespace isolation. |
| `hyperv` | Hyper-V hypervisor partition-based isolation. |
2015-11-09 04:11:10 -05:00
2022-03-01 06:29:54 -05:00
The default isolation on Windows server operating systems is `process` , and `hyperv`
2022-10-28 11:12:54 -04:00
on Windows client operating systems, such as Windows 10. Process isolation has better
performance, but requires that the image and host use the same kernel version.
2015-11-09 04:11:10 -05:00
2017-01-03 19:17:55 -05:00
On Windows server, assuming the default configuration, these commands are equivalent
and result in `process` isolation:
2015-11-09 04:11:10 -05:00
2019-05-13 19:37:30 -04:00
```powershell
2017-01-03 19:17:55 -05:00
PS C:\> docker run -d microsoft/nanoserver powershell echo process
PS C:\> docker run -d --isolation default microsoft/nanoserver powershell echo process
PS C:\> docker run -d --isolation process microsoft/nanoserver powershell echo process
2015-11-09 04:11:10 -05:00
```
2017-01-03 19:17:55 -05:00
If you have set the `--exec-opt isolation=hyperv` option on the Docker `daemon` , or
are running against a Windows client-based daemon, these commands are equivalent and
result in `hyperv` isolation:
2019-05-13 19:37:30 -04:00
```powershell
2017-01-03 19:17:55 -05:00
PS C:\> docker run -d microsoft/nanoserver powershell echo hyperv
PS C:\> docker run -d --isolation default microsoft/nanoserver powershell echo hyperv
PS C:\> docker run -d --isolation hyperv microsoft/nanoserver powershell echo hyperv
2015-11-09 04:11:10 -05:00
```
2016-03-29 08:24:28 -04:00
2023-01-06 13:28:29 -05:00
### <a name="memory"></a> Specify hard limits on memory available to containers (-m, --memory)
2017-04-26 09:30:09 -04:00
2023-03-31 05:56:40 -04:00
These parameters always set an upper limit on the memory available to the container. Linux sets this
on the cgroup and applications in a container can query it at `/sys/fs/cgroup/memory/memory.limit_in_bytes` .
2017-04-26 09:30:09 -04:00
2023-03-31 05:56:40 -04:00
On Windows, this affects containers differently depending on what type of isolation you use.
2017-04-26 09:30:09 -04:00
2023-03-31 05:56:40 -04:00
- With `process` isolation, Windows reports the full memory of the host system, not the limit to applications running inside the container
2017-08-16 11:53:57 -04:00
2017-04-26 09:30:09 -04:00
```powershell
2017-08-16 11:53:57 -04:00
PS C:\> docker run -it -m 2GB --isolation=process microsoft/nanoserver powershell Get-ComputerInfo *memory*
2017-04-26 09:30:09 -04:00
CsTotalPhysicalMemory : 17064509440
CsPhyicallyInstalledMemory : 16777216
OsTotalVisibleMemorySize : 16664560
OsFreePhysicalMemory : 14646720
OsTotalVirtualMemorySize : 19154928
OsFreeVirtualMemory : 17197440
OsInUseVirtualMemory : 1957488
OsMaxProcessMemorySize : 137438953344
```
2017-08-16 11:53:57 -04:00
2023-03-31 05:56:40 -04:00
- With `hyperv` isolation, Windows creates a utility VM that is big enough to hold the memory limit, plus the minimal OS needed to host the container. That size is reported as "Total Physical Memory."
2017-08-16 11:53:57 -04:00
2017-04-26 09:30:09 -04:00
```powershell
2017-08-16 11:53:57 -04:00
PS C:\> docker run -it -m 2GB --isolation=hyperv microsoft/nanoserver powershell Get-ComputerInfo *memory*
2017-04-26 09:30:09 -04:00
CsTotalPhysicalMemory : 2683355136
CsPhyicallyInstalledMemory :
OsTotalVisibleMemorySize : 2620464
OsFreePhysicalMemory : 2306552
OsTotalVirtualMemorySize : 2620464
OsFreeVirtualMemory : 2356692
OsInUseVirtualMemory : 263772
OsMaxProcessMemorySize : 137438953344
```
2023-01-06 13:28:29 -05:00
### <a name="sysctl"></a> Configure namespaced kernel parameters (sysctls) at runtime (--sysctl)
2016-03-29 08:24:28 -04:00
The `--sysctl` sets namespaced kernel parameters (sysctls) in the
container. For example, to turn on IP forwarding in the containers
network namespace, run this command:
2021-08-21 08:54:14 -04:00
```console
2017-02-07 18:42:48 -05:00
$ docker run --sysctl net.ipv4.ip_forward=1 someimage
```
2016-03-29 08:24:28 -04:00
2020-04-19 09:43:08 -04:00
> **Note**
>
> Not all sysctls are namespaced. Docker does not support changing sysctls
2016-10-19 13:25:45 -04:00
> inside of a container that also modify the host system. As the kernel
2016-03-29 08:24:28 -04:00
> evolves we expect to see more sysctls become namespaced.
2023-03-31 05:56:40 -04:00
2016-03-29 08:24:28 -04:00
#### Currently supported sysctls
2020-04-19 09:43:08 -04:00
IPC Namespace:
2016-03-29 08:24:28 -04:00
2020-04-19 09:43:08 -04:00
- `kernel.msgmax` , `kernel.msgmnb` , `kernel.msgmni` , `kernel.sem` ,
`kernel.shmall` , `kernel.shmmax` , `kernel.shmmni` , `kernel.shm_rmid_forced` .
- Sysctls beginning with `fs.mqueue.*`
- If you use the `--ipc=host` option these sysctls are not allowed.
2017-02-07 18:42:48 -05:00
2020-04-19 09:43:08 -04:00
Network Namespace:
2016-03-29 08:24:28 -04:00
2020-04-19 09:43:08 -04:00
- Sysctls beginning with `net.*`
- If you use the `--network=host` option using these sysctls are not allowed.
2023-03-31 05:56:40 -04:00
## Command internals
The `docker run` command is equivalent to the following API calls:
- `/<API version>/containers/create`
- If that call returns a 404 (image not found), and depending on the `--pull` option ("always", "missing", "never") the call can trigger a `docker pull <image>` .
- `/containers/create` again after pulling the image.
- `/containers/(id)/start` to start the container.
2023-06-26 09:42:50 -04:00
- `/containers/(id)/attach` to attach to the container when starting with the `-it` flags for interactive containers.