2015-06-07 23:07:20 -04:00
|
|
|
|
<!--[metadata]>
|
|
|
|
|
+++
|
|
|
|
|
title = "Docker run reference"
|
|
|
|
|
description = "Configure containers at runtime"
|
|
|
|
|
keywords = ["docker, run, configure, runtime"]
|
|
|
|
|
[menu.main]
|
|
|
|
|
parent = "mn_reference"
|
|
|
|
|
+++
|
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-03-08 12:42:14 -04:00
|
|
|
|
<!-- TODO (@thaJeztah) define more flexible table/td classes -->
|
|
|
|
|
<style>
|
|
|
|
|
.content-body table .no-wrap {
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
2014-10-20 20:48:58 -04:00
|
|
|
|
# Docker run reference
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-08-17 18:16:11 -04:00
|
|
|
|
Docker runs processes in isolated containers. A container is a process
|
|
|
|
|
which runs on a host. The host may be local or remote. When an operator
|
|
|
|
|
executes `docker run`, the container process that runs is isolated in
|
|
|
|
|
that it has its own file system, its own networking, and its own
|
|
|
|
|
isolated process tree separate from the host.
|
|
|
|
|
|
|
|
|
|
This page details how to use the `docker run` command to define the
|
|
|
|
|
container's resources at runtime.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## General form
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-05-21 17:05:19 -04:00
|
|
|
|
The basic `docker run` command takes this form:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-08-17 18:16:11 -04:00
|
|
|
|
The `docker run` command must specify an [*IMAGE*](/reference/glossary/#image)
|
|
|
|
|
to derive the container from. An image developer can define image
|
|
|
|
|
defaults related to:
|
2015-03-25 10:48:35 -04:00
|
|
|
|
|
|
|
|
|
* detached or foreground running
|
|
|
|
|
* container identification
|
|
|
|
|
* network settings
|
|
|
|
|
* runtime constraints on CPU and memory
|
|
|
|
|
* privileges and LXC configuration
|
|
|
|
|
|
2015-08-17 18:16:11 -04:00
|
|
|
|
With the `docker run [OPTIONS]` an operator can add to or override the
|
|
|
|
|
image defaults set by a developer. And, additionally, operators can
|
|
|
|
|
override nearly all the defaults set by the Docker runtime itself. The
|
|
|
|
|
operator's ability to override image and Docker runtime defaults is why
|
|
|
|
|
[*run*](/reference/commandline/cli/run/) has more options than any
|
|
|
|
|
other `docker` command.
|
|
|
|
|
|
|
|
|
|
To learn how to interpret the types of `[OPTIONS]`, see [*Option
|
|
|
|
|
types*](/reference/commandline/cli/#option-types).
|
|
|
|
|
|
|
|
|
|
> **Note**: Depending on your Docker system configuration, you may be
|
|
|
|
|
> required to preface the `docker run` command with `sudo`. To avoid
|
|
|
|
|
> having to use `sudo` with the `docker` command, your system
|
|
|
|
|
> administrator can create a Unix group called `docker` and add users to
|
|
|
|
|
> it. For more information about this configuration, refer to the Docker
|
|
|
|
|
> installation documentation for your operating system.
|
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Operator exclusive options
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-17 18:55:24 -04:00
|
|
|
|
Only the operator (the person executing `docker run`) can set the
|
|
|
|
|
following options.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-07-27 20:12:04 -04:00
|
|
|
|
- [Detached vs foreground](#detached-vs-foreground)
|
2014-04-23 16:48:28 -04:00
|
|
|
|
- [Detached (-d)](#detached-d)
|
|
|
|
|
- [Foreground](#foreground)
|
2015-07-27 20:12:04 -04:00
|
|
|
|
- [Container identification](#container-identification)
|
2014-05-15 10:50:59 -04:00
|
|
|
|
- [Name (--name)](#name-name)
|
2015-07-27 20:12:04 -04:00
|
|
|
|
- [PID equivalent](#pid-equivalent)
|
|
|
|
|
- [IPC settings (--ipc)](#ipc-settings-ipc)
|
|
|
|
|
- [Network settings](#network-settings)
|
|
|
|
|
- [Restart policies (--restart)](#restart-policies-restart)
|
|
|
|
|
- [Clean up (--rm)](#clean-up-rm)
|
|
|
|
|
- [Runtime constraints on resources](#runtime-constraints-on-resources)
|
|
|
|
|
- [Runtime privilege, Linux capabilities, and LXC configuration](#runtime-privilege-linux-capabilities-and-lxc-configuration)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Detached vs foreground
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
When starting a Docker container, you must first decide if you want to
|
|
|
|
|
run the container in the background in a "detached" mode or in the
|
|
|
|
|
default foreground mode:
|
|
|
|
|
|
|
|
|
|
-d=false: Detached mode: Run container in the background, print new container id
|
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
|
### Detached (-d)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-17 18:55:24 -04:00
|
|
|
|
In detached mode (`-d=true` or just `-d`), all I/O should be done
|
|
|
|
|
through network connections or shared volumes because the container is
|
2014-05-21 17:05:19 -04:00
|
|
|
|
no longer listening to the command line where you executed `docker run`.
|
2014-04-17 18:55:24 -04:00
|
|
|
|
You can reattach to a detached container with `docker`
|
2015-08-18 15:25:59 -04:00
|
|
|
|
[*attach*](/reference/commandline/attach). If you choose to run a
|
2014-04-17 18:55:24 -04:00
|
|
|
|
container in the detached mode, then you cannot use the `--rm` option.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
|
### Foreground
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
In foreground mode (the default when `-d` is not specified), `docker
|
|
|
|
|
run` can start the process in the container and attach the console to
|
|
|
|
|
the process's standard input, output, and standard error. It can even
|
|
|
|
|
pretend to be a TTY (this is what most command line executables expect)
|
|
|
|
|
and pass along signals. All of that is configurable:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
-a=[] : Attach to `STDIN`, `STDOUT` and/or `STDERR`
|
2014-04-15 20:53:12 -04:00
|
|
|
|
-t=false : Allocate a pseudo-tty
|
2014-11-13 04:40:45 -05:00
|
|
|
|
--sig-proxy=true: Proxify all received signal to the process (non-TTY mode only)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
-i=false : Keep STDIN open even if not attached
|
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
If you do not specify `-a` then Docker will [attach all standard
|
2014-07-24 18:19:50 -04:00
|
|
|
|
streams]( https://github.com/docker/docker/blob/
|
2014-06-29 22:25:12 -04:00
|
|
|
|
75a7f4d90cde0295bcfb7213004abce8d4779b75/commands.go#L1797). You can
|
|
|
|
|
specify to which of the three standard streams (`STDIN`, `STDOUT`,
|
|
|
|
|
`STDERR`) you'd like to connect instead, as in:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -a stdin -a stdout -i -t ubuntu /bin/bash
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-12-05 19:50:56 -05:00
|
|
|
|
For interactive processes (like a shell), you must use `-i -t` together in
|
2015-03-19 15:41:06 -04:00
|
|
|
|
order to allocate a tty for the container process. `-i -t` is often written `-it`
|
|
|
|
|
as you'll see in later examples. Specifying `-t` is forbidden when the client
|
|
|
|
|
standard output is redirected or piped, such as in:
|
2015-03-25 10:48:35 -04:00
|
|
|
|
`echo test | docker run -i busybox cat`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-04-13 12:33:59 -04:00
|
|
|
|
>**Note**: A process running as PID 1 inside a container is treated
|
|
|
|
|
>specially by Linux: it ignores any signal with the default action.
|
|
|
|
|
>So, the process will not terminate on `SIGINT` or `SIGTERM` unless it is
|
|
|
|
|
>coded to do so.
|
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Container identification
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-11-04 23:26:17 -05:00
|
|
|
|
### Name (--name)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
The operator can identify a container in three ways:
|
|
|
|
|
|
|
|
|
|
- UUID long identifier
|
|
|
|
|
("f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778")
|
|
|
|
|
- UUID short identifier ("f78375b1c487")
|
2014-04-23 16:48:28 -04:00
|
|
|
|
- Name ("evil_ptolemy")
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
The UUID identifiers come from the Docker daemon, and if you do not
|
2014-06-29 22:25:12 -04:00
|
|
|
|
assign a name to the container with `--name` then the daemon will also
|
|
|
|
|
generate a random string name too. The name can become a handy way to
|
|
|
|
|
add meaning to a container since you can use this name when defining
|
2014-12-15 23:25:37 -05:00
|
|
|
|
[*links*](/userguide/dockerlinks) (or any
|
2014-06-29 22:25:12 -04:00
|
|
|
|
other place you need to identify a container). This works for both
|
|
|
|
|
background and foreground Docker containers.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
### PID equivalent
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
Finally, to help with automation, you can have Docker write the
|
2014-04-15 20:53:12 -04:00
|
|
|
|
container ID out to a file of your choosing. This is similar to how some
|
2014-06-29 22:25:12 -04:00
|
|
|
|
programs might write out their process ID to a file (you've seen them as
|
2014-04-15 20:53:12 -04:00
|
|
|
|
PID files):
|
|
|
|
|
|
|
|
|
|
--cidfile="": Write the container ID to the file
|
2015-03-08 12:42:14 -04:00
|
|
|
|
|
2014-07-16 19:39:44 -04:00
|
|
|
|
### Image[:tag]
|
|
|
|
|
|
|
|
|
|
While not strictly a means of identifying a container, you can specify a version of an
|
|
|
|
|
image you'd like to run the container with by adding `image[:tag]` to the command. For
|
|
|
|
|
example, `docker run ubuntu:14.04`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-02-26 21:23:50 -05:00
|
|
|
|
### Image[@digest]
|
|
|
|
|
|
|
|
|
|
Images using the v2 or later image format have a content-addressable identifier
|
|
|
|
|
called a digest. As long as the input used to generate the image is unchanged,
|
|
|
|
|
the digest value is predictable and referenceable.
|
|
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
|
## PID settings (--pid)
|
2015-05-05 18:32:36 -04:00
|
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
|
--pid="" : Set the PID (Process) Namespace mode for the container,
|
|
|
|
|
'host': use the host's PID namespace inside the container
|
2015-03-08 12:42:14 -04:00
|
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
|
By default, all containers have the PID namespace enabled.
|
|
|
|
|
|
|
|
|
|
PID namespace provides separation of processes. The PID Namespace removes the
|
|
|
|
|
view of the system processes, and allows process ids to be reused including
|
|
|
|
|
pid 1.
|
|
|
|
|
|
|
|
|
|
In certain cases you want your container to share the host's process namespace,
|
|
|
|
|
basically allowing processes within the container to see all of the processes
|
|
|
|
|
on the system. For example, you could build a container with debugging tools
|
|
|
|
|
like `strace` or `gdb`, but want to use these tools when debugging processes
|
|
|
|
|
within the container.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --pid=host rhel7 strace -p 1234
|
2014-11-25 15:10:53 -05:00
|
|
|
|
|
|
|
|
|
This command would allow you to use `strace` inside the container on pid 1234 on
|
|
|
|
|
the host.
|
|
|
|
|
|
2015-05-05 18:32:36 -04:00
|
|
|
|
## UTS settings (--uts)
|
|
|
|
|
|
|
|
|
|
--uts="" : Set the UTS namespace mode for the container,
|
|
|
|
|
'host': use the host's UTS namespace inside the container
|
|
|
|
|
|
|
|
|
|
The UTS namespace is for setting the hostname and the domain that is visible
|
|
|
|
|
to running processes in that namespace. By default, all containers, including
|
|
|
|
|
those with `--net=host`, have their own UTS namespace. The `host` setting will
|
|
|
|
|
result in the container using the same UTS namespace as the host.
|
|
|
|
|
|
|
|
|
|
You may wish to share the UTS namespace with the host if you would like the
|
|
|
|
|
hostname of the container to change as the hostname of the host changes. A
|
|
|
|
|
more advanced use case would be changing the host's hostname from a container.
|
|
|
|
|
|
|
|
|
|
> **Note**: `--uts="host"` gives the container full access to change the
|
|
|
|
|
> hostname of the host and is therefore considered insecure.
|
|
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
|
## IPC settings (--ipc)
|
2015-03-08 12:42:14 -04:00
|
|
|
|
|
2014-11-10 16:14:17 -05:00
|
|
|
|
--ipc="" : Set the IPC mode for the container,
|
2015-03-08 12:42:14 -04:00
|
|
|
|
'container:<name|id>': reuses another container's IPC namespace
|
|
|
|
|
'host': use the host's IPC namespace inside the container
|
|
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
|
By default, all containers have the IPC namespace enabled.
|
2014-11-10 16:14:17 -05:00
|
|
|
|
|
2015-07-13 09:46:39 -04:00
|
|
|
|
IPC (POSIX/SysV IPC) namespace provides separation of named shared memory
|
2015-03-08 12:42:14 -04:00
|
|
|
|
segments, semaphores and message queues.
|
2014-11-10 16:14:17 -05:00
|
|
|
|
|
|
|
|
|
Shared memory segments are used to accelerate inter-process communication at
|
|
|
|
|
memory speed, rather than through pipes or through the network stack. Shared
|
2015-07-13 09:46:39 -04:00
|
|
|
|
memory is commonly used by databases and custom-built (typically C/OpenMPI,
|
2014-11-10 16:14:17 -05:00
|
|
|
|
C++/using boost libraries) high performance applications for scientific
|
|
|
|
|
computing and financial services industries. If these types of applications
|
|
|
|
|
are broken into multiple containers, you might need to share the IPC mechanisms
|
|
|
|
|
of the containers.
|
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Network settings
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-10-03 17:02:17 -04:00
|
|
|
|
--dns=[] : Set custom dns servers for the container
|
|
|
|
|
--net="bridge" : Set the Network mode for the container
|
2015-03-08 12:42:14 -04:00
|
|
|
|
'bridge': creates a new network stack for the container on the docker bridge
|
|
|
|
|
'none': no networking for this container
|
|
|
|
|
'container:<name|id>': reuses another container network stack
|
|
|
|
|
'host': use the host network stack inside the container
|
2014-10-03 17:02:17 -04:00
|
|
|
|
--add-host="" : Add a line to /etc/hosts (host:IP)
|
2014-11-04 09:19:47 -05:00
|
|
|
|
--mac-address="" : Sets the container's Ethernet device's MAC address
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
By default, all containers have networking enabled and they can make any
|
|
|
|
|
outgoing connections. The operator can completely disable networking
|
2014-06-29 22:25:12 -04:00
|
|
|
|
with `docker run --net none` which disables all incoming and outgoing
|
|
|
|
|
networking. In cases like this, you would perform I/O through files or
|
|
|
|
|
`STDIN` and `STDOUT` only.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-07-13 09:46:39 -04:00
|
|
|
|
Publishing ports and linking to other containers will not work
|
|
|
|
|
when `--net` is anything other than the default (bridge).
|
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
|
Your container will use the same DNS servers as the host by default, but
|
|
|
|
|
you can override this with `--dns`.
|
|
|
|
|
|
2015-05-10 01:36:16 -04:00
|
|
|
|
By default, the MAC address is generated using the IP address allocated to the
|
|
|
|
|
container. You can set the container's MAC address explicitly by providing a
|
|
|
|
|
MAC address via the `--mac-address` parameter (format:`12:34:56:78:9a:bc`).
|
2014-10-03 17:02:17 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
Supported networking modes are:
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
2015-03-08 12:42:14 -04:00
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th class="no-wrap">Mode</th>
|
|
|
|
|
<th>Description</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>none</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
No networking in the container.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>bridge</strong> (default)</td>
|
|
|
|
|
<td>
|
|
|
|
|
Connect the container to the bridge via veth interfaces.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>host</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
Use the host's network stack inside the container.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>container</strong>:<name|id></td>
|
|
|
|
|
<td>
|
|
|
|
|
Use the network stack of another container, specified via
|
|
|
|
|
its *name* or *id*.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
|
|
|
|
#### Mode: none
|
2014-06-29 22:25:12 -04:00
|
|
|
|
|
|
|
|
|
With the networking mode set to `none` a container will not have a
|
|
|
|
|
access to any external routes. The container will still have a
|
|
|
|
|
`loopback` interface enabled in the container but it does not have any
|
|
|
|
|
routes to external traffic.
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
|
|
|
|
#### Mode: bridge
|
2014-06-29 22:25:12 -04:00
|
|
|
|
|
|
|
|
|
With the networking mode set to `bridge` a container will use docker's
|
|
|
|
|
default networking setup. A bridge is setup on the host, commonly named
|
|
|
|
|
`docker0`, and a pair of `veth` interfaces will be created for the
|
|
|
|
|
container. One side of the `veth` pair will remain on the host attached
|
|
|
|
|
to the bridge while the other side of the pair will be placed inside the
|
|
|
|
|
container's namespaces in addition to the `loopback` interface. An IP
|
|
|
|
|
address will be allocated for containers on the bridge's network and
|
|
|
|
|
traffic will be routed though this bridge to the container.
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
|
|
|
|
#### Mode: host
|
2014-06-29 22:25:12 -04:00
|
|
|
|
|
2014-05-02 18:32:26 -04:00
|
|
|
|
With the networking mode set to `host` a container will share the host's
|
2014-06-29 22:25:12 -04:00
|
|
|
|
network stack and all interfaces from the host will be available to the
|
|
|
|
|
container. The container's hostname will match the hostname on the host
|
2015-07-13 09:46:39 -04:00
|
|
|
|
system. Note that `--add-host` `--hostname` `--dns` `--dns-search` and
|
|
|
|
|
`--mac-address` is invalid in `host` netmode.
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
2015-03-28 21:32:00 -04:00
|
|
|
|
Compared to the default `bridge` mode, the `host` mode gives *significantly*
|
|
|
|
|
better networking performance since it uses the host's native networking stack
|
2015-04-07 14:15:05 -04:00
|
|
|
|
whereas the bridge has to go through one level of virtualization through the
|
2015-03-28 21:32:00 -04:00
|
|
|
|
docker daemon. It is recommended to run containers in this mode when their
|
|
|
|
|
networking performance is critical, for example, a production Load Balancer
|
|
|
|
|
or a High Performance Web Server.
|
|
|
|
|
|
2015-03-08 12:42:14 -04:00
|
|
|
|
> **Note**: `--net="host"` gives the container full access to local system
|
|
|
|
|
> services such as D-bus and is therefore considered insecure.
|
|
|
|
|
|
2014-05-02 18:32:26 -04:00
|
|
|
|
#### Mode: container
|
2014-06-29 22:25:12 -04:00
|
|
|
|
|
|
|
|
|
With the networking mode set to `container` a container will share the
|
|
|
|
|
network stack of another container. The other container's name must be
|
2015-07-13 09:46:39 -04:00
|
|
|
|
provided in the format of `--net container:<name|id>`. Note that `--add-host`
|
|
|
|
|
`--hostname` `--dns` `--dns-search` and `--mac-address` is invalid
|
2015-05-27 03:31:06 -04:00
|
|
|
|
in `container` netmode, and `--publish` `--publish-all` `--expose` are also
|
|
|
|
|
invalid in `container` netmode.
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
Example running a Redis container with Redis binding to `localhost` then
|
|
|
|
|
running the `redis-cli` command and connecting to the Redis server over the
|
|
|
|
|
`localhost` interface.
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -d --name redis example/redis --bind 127.0.0.1
|
2014-05-02 18:32:26 -04:00
|
|
|
|
$ # use the redis container's network stack to access localhost
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --rm -it --net container:redis example/redis-cli -h 127.0.0.1
|
2014-05-02 18:32:26 -04:00
|
|
|
|
|
2014-09-13 00:35:59 -04:00
|
|
|
|
### Managing /etc/hosts
|
|
|
|
|
|
|
|
|
|
Your container will have lines in `/etc/hosts` which define the hostname of the
|
|
|
|
|
container itself as well as `localhost` and a few other common things. The
|
2015-07-13 09:46:39 -04:00
|
|
|
|
`--add-host` flag can be used to add additional lines to `/etc/hosts`.
|
2014-09-13 00:35:59 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts
|
2014-09-13 00:35:59 -04:00
|
|
|
|
172.17.0.22 09d03f76bf2c
|
|
|
|
|
fe00::0 ip6-localnet
|
|
|
|
|
ff00::0 ip6-mcastprefix
|
|
|
|
|
ff02::1 ip6-allnodes
|
|
|
|
|
ff02::2 ip6-allrouters
|
|
|
|
|
127.0.0.1 localhost
|
|
|
|
|
::1 localhost ip6-localhost ip6-loopback
|
|
|
|
|
86.75.30.9 db-static
|
|
|
|
|
|
2015-03-01 15:13:33 -05:00
|
|
|
|
## Restart policies (--restart)
|
|
|
|
|
|
|
|
|
|
Using the `--restart` flag on Docker run you can specify a restart policy for
|
|
|
|
|
how a container should or should not be restarted on exit.
|
|
|
|
|
|
|
|
|
|
When a restart policy is active on a container, it will be shown as either `Up`
|
2015-08-18 15:25:59 -04:00
|
|
|
|
or `Restarting` in [`docker ps`](/reference/commandline/ps). It can also be
|
|
|
|
|
useful to use [`docker events`](/reference/commandline/events) to see the
|
2015-03-01 15:13:33 -05:00
|
|
|
|
restart policy in effect.
|
|
|
|
|
|
|
|
|
|
Docker supports the following restart policies:
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Policy</th>
|
|
|
|
|
<th>Result</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><strong>no</strong></td>
|
|
|
|
|
<td>
|
2015-07-13 09:46:39 -04:00
|
|
|
|
Do not automatically restart the container when it exits. This is the
|
2015-03-01 15:13:33 -05:00
|
|
|
|
default.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>
|
|
|
|
|
<span style="white-space: nowrap">
|
|
|
|
|
<strong>on-failure</strong>[:max-retries]
|
|
|
|
|
</span>
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
|
|
|
|
Restart only if the container exits with a non-zero exit status.
|
2015-07-13 09:46:39 -04:00
|
|
|
|
Optionally, limit the number of restart retries the Docker
|
2015-03-01 15:13:33 -05:00
|
|
|
|
daemon attempts.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><strong>always</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
Always restart the container regardless of the exit status.
|
|
|
|
|
When you specify always, the Docker daemon will try to restart
|
|
|
|
|
the container indefinitely.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
An ever increasing delay (double the previous delay, starting at 100
|
|
|
|
|
milliseconds) is added before each restart to prevent flooding the server.
|
|
|
|
|
This means the daemon will wait for 100 ms, then 200 ms, 400, 800, 1600,
|
|
|
|
|
and so on until either the `on-failure` limit is hit, or when you `docker stop`
|
|
|
|
|
or `docker rm -f` the container.
|
|
|
|
|
|
2015-04-25 14:57:01 -04:00
|
|
|
|
If a container is successfully restarted (the container is started and runs
|
2015-03-01 15:13:33 -05:00
|
|
|
|
for at least 10 seconds), the delay is reset to its default value of 100 ms.
|
|
|
|
|
|
|
|
|
|
You can specify the maximum amount of times Docker will try to restart the
|
|
|
|
|
container when using the **on-failure** policy. The default is that Docker
|
|
|
|
|
will try forever to restart the container. The number of (attempted) restarts
|
|
|
|
|
for a container can be obtained via [`docker inspect`](
|
2015-08-18 15:25:59 -04:00
|
|
|
|
/reference/commandline/inspect). For example, to get the number of restarts
|
2015-03-01 15:13:33 -05:00
|
|
|
|
for container "my-container";
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker inspect -f "{{ .RestartCount }}" my-container
|
2015-03-01 15:13:33 -05:00
|
|
|
|
# 2
|
|
|
|
|
|
|
|
|
|
Or, to get the last time the container was (re)started;
|
|
|
|
|
|
|
|
|
|
$ docker inspect -f "{{ .State.StartedAt }}" my-container
|
|
|
|
|
# 2015-03-04T23:47:07.691840179Z
|
|
|
|
|
|
2015-07-13 09:46:39 -04:00
|
|
|
|
You cannot set any restart policy in combination with
|
2015-03-01 15:13:33 -05:00
|
|
|
|
["clean up (--rm)"](#clean-up-rm). Setting both `--restart` and `--rm`
|
|
|
|
|
results in an error.
|
|
|
|
|
|
2015-07-08 14:57:20 -04:00
|
|
|
|
### Examples
|
2015-03-01 15:13:33 -05:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --restart=always redis
|
2015-03-01 15:13:33 -05:00
|
|
|
|
|
|
|
|
|
This will run the `redis` container with a restart policy of **always**
|
|
|
|
|
so that if the container exits, Docker will restart it.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --restart=on-failure:10 redis
|
2015-03-01 15:13:33 -05:00
|
|
|
|
|
2015-07-13 09:46:39 -04:00
|
|
|
|
This will run the `redis` container with a restart policy of **on-failure**
|
2015-03-01 15:13:33 -05:00
|
|
|
|
and a maximum restart count of 10. If the `redis` container exits with a
|
|
|
|
|
non-zero exit status more than 10 times in a row Docker will abort trying to
|
|
|
|
|
restart the container. Providing a maximum restart limit is only valid for the
|
|
|
|
|
**on-failure** policy.
|
|
|
|
|
|
2014-11-04 23:26:17 -05:00
|
|
|
|
## Clean up (--rm)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
|
By default a container's file system persists even after the container
|
2014-04-15 20:53:12 -04:00
|
|
|
|
exits. This makes debugging a lot easier (since you can inspect the
|
|
|
|
|
final state) and you retain all your data by default. But if you are
|
|
|
|
|
running short-term **foreground** processes, these container file
|
2014-04-23 16:48:28 -04:00
|
|
|
|
systems can really pile up. If instead you'd like Docker to
|
2014-04-15 20:53:12 -04:00
|
|
|
|
**automatically clean up the container and remove the file system when
|
|
|
|
|
the container exits**, you can add the `--rm` flag:
|
|
|
|
|
|
|
|
|
|
--rm=false: Automatically remove the container when it exits (incompatible with -d)
|
|
|
|
|
|
2015-08-20 06:26:17 -04:00
|
|
|
|
> **Note**: When you set the `--rm` flag, Docker also removes the volumes
|
|
|
|
|
associated with the container when the container is removed. This is similar
|
|
|
|
|
to running `docker rm -v my-container`.
|
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Security configuration
|
2014-09-29 06:44:32 -04:00
|
|
|
|
--security-opt="label:user:USER" : Set the label user for the container
|
|
|
|
|
--security-opt="label:role:ROLE" : Set the label role for the container
|
|
|
|
|
--security-opt="label:type:TYPE" : Set the label type for the container
|
|
|
|
|
--security-opt="label:level:LEVEL" : Set the label level for the container
|
|
|
|
|
--security-opt="label:disable" : Turn off label confinement for the container
|
2015-07-13 09:46:39 -04:00
|
|
|
|
--security-opt="apparmor:PROFILE" : Set the apparmor profile to be applied
|
2014-09-29 20:59:29 -04:00
|
|
|
|
to the container
|
2014-09-29 06:44:32 -04:00
|
|
|
|
|
2014-10-03 11:43:22 -04:00
|
|
|
|
You can override the default labeling scheme for each container by specifying
|
|
|
|
|
the `--security-opt` flag. For example, you can specify the MCS/MLS level, a
|
|
|
|
|
requirement for MLS systems. Specifying the level in the following command
|
|
|
|
|
allows you to share the same content between containers.
|
2014-09-29 06:44:32 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --security-opt label:level:s0:c100,c200 -i -t fedora bash
|
2014-09-29 06:44:32 -04:00
|
|
|
|
|
2014-10-03 11:43:22 -04:00
|
|
|
|
An MLS example might be:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --security-opt label:level:TopSecret -i -t rhel7 bash
|
2014-10-03 11:43:22 -04:00
|
|
|
|
|
|
|
|
|
To disable the security labeling for this container versus running with the
|
|
|
|
|
`--permissive` flag, use the following command:
|
2014-09-29 06:44:32 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --security-opt label:disable -i -t fedora bash
|
2014-09-29 06:44:32 -04:00
|
|
|
|
|
2014-10-03 11:43:22 -04:00
|
|
|
|
If you want a tighter security policy on the processes within a container,
|
|
|
|
|
you can specify an alternate type for the container. You could run a container
|
|
|
|
|
that is only allowed to listen on Apache ports by executing the following
|
|
|
|
|
command:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --security-opt label:type:svirt_apache_t -i -t centos bash
|
2014-10-03 11:43:22 -04:00
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
> **Note**: You would have to write policy defining a `svirt_apache_t` type.
|
2014-09-29 06:44:32 -04:00
|
|
|
|
|
2015-04-17 22:01:16 -04:00
|
|
|
|
## Specifying custom cgroups
|
|
|
|
|
|
|
|
|
|
Using the `--cgroup-parent` flag, you can pass a specific cgroup to run a
|
|
|
|
|
container in. This allows you to create and manage cgroups on their own. You can
|
|
|
|
|
define custom resources for those cgroups and put containers under a common
|
|
|
|
|
parent group.
|
|
|
|
|
|
2015-03-24 06:48:08 -04:00
|
|
|
|
## Runtime constraints on resources
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
The operator can also adjust the performance parameters of the
|
|
|
|
|
container:
|
|
|
|
|
|
2015-08-18 22:11:08 -04:00
|
|
|
|
| Option | Description |
|
|
|
|
|
|----------------------------|---------------------------------------------------------------------------------------------|
|
|
|
|
|
| `-m`, `--memory="" ` | Memory limit (format: `<number>[<unit>]`, where unit = b, k, m or g) |
|
|
|
|
|
| `--memory-swap=""` | Total memory limit (memory + swap, format: `<number>[<unit>]`, where unit = b, k, m or g) |
|
2015-08-19 11:56:55 -04:00
|
|
|
|
| `--kernel-memory=""` | Kernel memory limit (format: `<number>[<unit>]`, where unit = b, k, m or g) |
|
2015-08-18 22:11:08 -04:00
|
|
|
|
| `-c`, `--cpu-shares=0` | CPU shares (relative weight) |
|
|
|
|
|
| `--cpu-period=0` | Limit the CPU CFS (Completely Fair Scheduler) period |
|
|
|
|
|
| `--cpuset-cpus="" ` | CPUs in which to allow execution (0-3, 0,1) |
|
|
|
|
|
| `--cpuset-mems=""` | Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems. |
|
|
|
|
|
| `--cpu-quota=0` | Limit the CPU CFS (Completely Fair Scheduler) quota |
|
|
|
|
|
| `--blkio-weight=0` | Block IO weight (relative weight) accepts a weight value between 10 and 1000. |
|
|
|
|
|
| `--oom-kill-disable=false` | Whether to disable OOM Killer for the container or not. |
|
|
|
|
|
| `--memory-swappiness="" ` | Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100. |
|
2015-03-03 21:17:46 -05:00
|
|
|
|
|
2015-08-19 11:56:55 -04:00
|
|
|
|
### User memory constraints
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-08-19 11:56:55 -04:00
|
|
|
|
We have four ways to set user memory usage:
|
2015-02-04 20:12:56 -05:00
|
|
|
|
|
2015-03-08 12:42:14 -04:00
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Option</th>
|
|
|
|
|
<th>Result</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap">
|
|
|
|
|
<strong>memory=inf, memory-swap=inf</strong> (default)
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
|
|
|
|
There is no memory limit for the container. The container can use
|
|
|
|
|
as much memory as needed.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>memory=L<inf, memory-swap=inf</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
(specify memory and set memory-swap as <code>-1</code>) The container is
|
|
|
|
|
not allowed to use more than L bytes of memory, but can use as much swap
|
|
|
|
|
as is needed (if the host supports swap memory).
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>memory=L<inf, memory-swap=2*L</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
(specify memory without memory-swap) The container is not allowed to
|
|
|
|
|
use more than L bytes of memory, swap *plus* memory usage is double
|
|
|
|
|
of that.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap">
|
|
|
|
|
<strong>memory=L<inf, memory-swap=S<inf, L<=S</strong>
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
|
|
|
|
(specify both memory and memory-swap) The container is not allowed to
|
|
|
|
|
use more than L bytes of memory, swap *plus* memory usage is limited
|
|
|
|
|
by S.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2015-02-04 20:12:56 -05:00
|
|
|
|
|
2015-08-19 11:56:55 -04:00
|
|
|
|
Examples:
|
2015-03-24 01:45:16 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -ti ubuntu:14.04 /bin/bash
|
2015-03-24 01:45:16 -04:00
|
|
|
|
|
|
|
|
|
We set nothing about memory, this means the processes in the container can use
|
|
|
|
|
as much memory and swap memory as they need.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -ti -m 300M --memory-swap -1 ubuntu:14.04 /bin/bash
|
2015-03-24 01:45:16 -04:00
|
|
|
|
|
|
|
|
|
We set memory limit and disabled swap memory limit, this means the processes in
|
|
|
|
|
the container can use 300M memory and as much swap memory as they need (if the
|
|
|
|
|
host supports swap memory).
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -ti -m 300M ubuntu:14.04 /bin/bash
|
2015-03-24 01:45:16 -04:00
|
|
|
|
|
|
|
|
|
We set memory limit only, this means the processes in the container can use
|
|
|
|
|
300M memory and 300M swap memory, by default, the total virtual memory size
|
|
|
|
|
(--memory-swap) will be set as double of memory, in this case, memory + swap
|
|
|
|
|
would be 2*300M, so processes can use 300M swap memory as well.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -ti -m 300M --memory-swap 1G ubuntu:14.04 /bin/bash
|
2015-03-24 01:45:16 -04:00
|
|
|
|
|
|
|
|
|
We set both memory and swap memory, so the processes in the container can use
|
|
|
|
|
300M memory and 700M swap memory.
|
|
|
|
|
|
2015-07-07 21:51:10 -04:00
|
|
|
|
By default, kernel kills processes in a container if an out-of-memory (OOM)
|
2015-02-26 06:53:55 -05:00
|
|
|
|
error occurs. To change this behaviour, use the `--oom-kill-disable` option.
|
|
|
|
|
Only disable the OOM killer on containers where you have also set the
|
|
|
|
|
`-m/--memory` option. If the `-m` flag is not set, this can result in the host
|
|
|
|
|
running out of memory and require killing the host's system processes to free
|
|
|
|
|
memory.
|
|
|
|
|
|
|
|
|
|
The following example limits the memory to 100M and disables the OOM killer for
|
|
|
|
|
this container:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti -m 100M --oom-kill-disable ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
The following example, illustrates a dangerous way to use the flag:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --oom-kill-disable ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
The container has unlimited memory which can cause the host to run out memory
|
|
|
|
|
and require killing system processes to free memory.
|
|
|
|
|
|
2015-08-19 11:56:55 -04:00
|
|
|
|
### Kernel memory constraints
|
|
|
|
|
|
|
|
|
|
Kernel memory is fundamentally different than user memory as kernel memory can't
|
|
|
|
|
be swapped out. The inability to swap makes it possible for the container to
|
|
|
|
|
block system services by consuming too much kernel memory. Kernel memory includes:
|
|
|
|
|
|
|
|
|
|
- stack pages
|
|
|
|
|
- slab pages
|
|
|
|
|
- sockets memory pressure
|
|
|
|
|
- tcp memory pressure
|
|
|
|
|
|
|
|
|
|
You can setup kernel memory limit to constrain these kinds of memory. For example,
|
|
|
|
|
every process consumes some stack pages. By limiting kernel memory, you can
|
|
|
|
|
prevent new processes from being created when the kernel memory usage is too high.
|
|
|
|
|
|
|
|
|
|
Kernel memory is never completely independent of user memory. Instead, you limit
|
|
|
|
|
kernel memory in the context of the user memory limit. Assume "U" is the user memory
|
|
|
|
|
limit and "K" the kernel limit. There are three possible ways to set limits:
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Option</th>
|
|
|
|
|
<th>Result</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>U != 0, K = inf</strong> (default)</td>
|
|
|
|
|
<td>
|
|
|
|
|
This is the standard memory limitation mechanism already present before using
|
|
|
|
|
kernel memory. Kernel memory is completely ignored.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>U != 0, K < U</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
Kernel memory is a subset of the user memory. This setup is useful in
|
|
|
|
|
deployments where the total amount of memory per-cgroup is overcommited.
|
|
|
|
|
Overcommiting kernel memory limits is definitely not recommended, since the
|
|
|
|
|
box can still run out of non-reclaimable memory.
|
|
|
|
|
In this case, the you can configure K so that the sum of all groups is
|
|
|
|
|
never greater than the total memory. Then, freely set U at the expense of
|
|
|
|
|
the system's service quality.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="no-wrap"><strong>U != 0, K > U</strong></td>
|
|
|
|
|
<td>
|
|
|
|
|
Since kernel memory charges are also fed to the user counter and reclaimation
|
|
|
|
|
is triggered for the container for both kinds of memory. This configuration
|
|
|
|
|
gives the admin a unified view of memory. It is also useful for people
|
|
|
|
|
who just want to track kernel memory usage.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti -m 500M --kernel-memory 50M ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
We set memory and kernel memory, so the processes in the container can use
|
|
|
|
|
500M memory in total, in this 500M memory, it can be 50M kernel memory tops.
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --kernel-memory 50M ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
We set kernel memory without **-m**, so the processes in the container can
|
|
|
|
|
use as much memory as they want, but they can only use 50M kernel memory.
|
|
|
|
|
|
2015-07-12 03:46:33 -04:00
|
|
|
|
### Swappiness constraint
|
|
|
|
|
|
|
|
|
|
By default, a container's kernel can swap out a percentage of anonymous pages.
|
|
|
|
|
To set this percentage for a container, specify a `--memory-swappiness` value
|
|
|
|
|
between 0 and 100. A value of 0 turns off anonymous page swapping. A value of
|
2015-07-20 04:10:10 -04:00
|
|
|
|
100 sets all anonymous pages as swappable. By default, if you are not using
|
|
|
|
|
`--memory-swappiness`, memory swappiness value will be inherited from the parent.
|
2015-07-12 03:46:33 -04:00
|
|
|
|
|
|
|
|
|
For example, you can set:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --memory-swappiness=0 ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
Setting the `--memory-swappiness` option is helpful when you want to retain the
|
|
|
|
|
container's working set and to avoid swapping performance penalties.
|
|
|
|
|
|
2015-03-03 21:17:46 -05:00
|
|
|
|
### CPU share constraint
|
|
|
|
|
|
|
|
|
|
By default, all containers get the same proportion of CPU cycles. This proportion
|
|
|
|
|
can be modified by changing the container's CPU share weighting relative
|
|
|
|
|
to the weighting of all other running containers.
|
|
|
|
|
|
|
|
|
|
To modify the proportion from the default of 1024, use the `-c` or `--cpu-shares`
|
|
|
|
|
flag to set the weighting to 2 or higher.
|
|
|
|
|
|
|
|
|
|
The proportion will only apply when CPU-intensive processes are running.
|
|
|
|
|
When tasks in one container are idle, other containers can use the
|
|
|
|
|
left-over CPU time. The actual amount of CPU time will vary depending on
|
|
|
|
|
the number of containers running on the system.
|
|
|
|
|
|
|
|
|
|
For example, consider three containers, one has a cpu-share of 1024 and
|
|
|
|
|
two others have a cpu-share setting of 512. When processes in all three
|
|
|
|
|
containers attempt to use 100% of CPU, the first container would receive
|
2015-04-08 11:23:47 -04:00
|
|
|
|
50% of the total CPU time. If you add a fourth container with a cpu-share
|
2015-03-03 21:17:46 -05:00
|
|
|
|
of 1024, the first container only gets 33% of the CPU. The remaining containers
|
|
|
|
|
receive 16.5%, 16.5% and 33% of the CPU.
|
|
|
|
|
|
|
|
|
|
On a multi-core system, the shares of CPU time are distributed over all CPU
|
|
|
|
|
cores. Even if a container is limited to less than 100% of CPU time, it can
|
|
|
|
|
use 100% of each individual CPU core.
|
|
|
|
|
|
|
|
|
|
For example, consider a system with more than three cores. If you start one
|
|
|
|
|
container `{C0}` with `-c=512` running one process, and another container
|
|
|
|
|
`{C1}` with `-c=1024` running two processes, this can result in the following
|
|
|
|
|
division of CPU shares:
|
|
|
|
|
|
|
|
|
|
PID container CPU CPU share
|
|
|
|
|
100 {C0} 0 100% of CPU0
|
|
|
|
|
101 {C1} 1 100% of CPU1
|
|
|
|
|
102 {C1} 2 100% of CPU2
|
2014-11-24 16:20:18 -05:00
|
|
|
|
|
2015-04-08 04:58:59 -04:00
|
|
|
|
### CPU period constraint
|
|
|
|
|
|
|
|
|
|
The default CPU CFS (Completely Fair Scheduler) period is 100ms. We can use
|
2015-07-13 09:46:39 -04:00
|
|
|
|
`--cpu-period` to set the period of CPUs to limit the container's CPU usage.
|
2015-04-08 04:58:59 -04:00
|
|
|
|
And usually `--cpu-period` should work with `--cpu-quota`.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --cpu-period=50000 --cpu-quota=25000 ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms.
|
|
|
|
|
|
|
|
|
|
For more information, see the [CFS documentation on bandwidth limiting](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt).
|
|
|
|
|
|
2015-03-24 06:48:08 -04:00
|
|
|
|
### Cpuset constraint
|
|
|
|
|
|
|
|
|
|
We can set cpus in which to allow execution for containers.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -ti --cpuset-cpus="1,3" ubuntu:14.04 /bin/bash
|
2015-03-24 06:48:08 -04:00
|
|
|
|
|
|
|
|
|
This means processes in container can be executed on cpu 1 and cpu 3.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -ti --cpuset-cpus="0-2" ubuntu:14.04 /bin/bash
|
2015-03-24 06:48:08 -04:00
|
|
|
|
|
|
|
|
|
This means processes in container can be executed on cpu 0, cpu 1 and cpu 2.
|
|
|
|
|
|
2015-04-14 21:33:46 -04:00
|
|
|
|
We can set mems in which to allow execution for containers. Only effective
|
|
|
|
|
on NUMA systems.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --cpuset-mems="1,3" ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
This example restricts the processes in the container to only use memory from
|
|
|
|
|
memory nodes 1 and 3.
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --cpuset-mems="0-2" ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
This example restricts the processes in the container to only use memory from
|
|
|
|
|
memory nodes 0, 1 and 2.
|
|
|
|
|
|
2015-04-20 11:16:47 -04:00
|
|
|
|
### CPU quota constraint
|
|
|
|
|
|
|
|
|
|
The `--cpu-quota` flag limits the container's CPU usage. The default 0 value
|
|
|
|
|
allows the container to take 100% of a CPU resource (1 CPU). The CFS (Completely Fair
|
|
|
|
|
Scheduler) handles resource allocation for executing processes and is default
|
|
|
|
|
Linux Scheduler used by the kernel. Set this value to 50000 to limit the container
|
|
|
|
|
to 50% of a CPU resource. For multiple CPUs, adjust the `--cpu-quota` as necessary.
|
|
|
|
|
For more information, see the [CFS documentation on bandwidth limiting](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt).
|
|
|
|
|
|
2015-05-06 23:55:58 -04:00
|
|
|
|
### Block IO bandwidth (Blkio) constraint
|
|
|
|
|
|
|
|
|
|
By default, all containers get the same proportion of block IO bandwidth
|
|
|
|
|
(blkio). This proportion is 500. To modify this proportion, change the
|
|
|
|
|
container's blkio weight relative to the weighting of all other running
|
|
|
|
|
containers using the `--blkio-weight` flag.
|
|
|
|
|
|
|
|
|
|
The `--blkio-weight` flag can set the weighting to a value between 10 to 1000.
|
|
|
|
|
For example, the commands below create two containers with different blkio
|
|
|
|
|
weight:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --name c1 --blkio-weight 300 ubuntu:14.04 /bin/bash
|
|
|
|
|
$ docker run -ti --name c2 --blkio-weight 600 ubuntu:14.04 /bin/bash
|
|
|
|
|
|
|
|
|
|
If you do block IO in the two containers at the same time, by, for example:
|
|
|
|
|
|
|
|
|
|
$ time dd if=/mnt/zerofile of=test.out bs=1M count=1024 oflag=direct
|
|
|
|
|
|
|
|
|
|
You'll find that the proportion of time is the same as the proportion of blkio
|
|
|
|
|
weights of the two containers.
|
|
|
|
|
|
|
|
|
|
> **Note:** The blkio weight setting is only available for direct IO. Buffered IO
|
|
|
|
|
> is not currently supported.
|
|
|
|
|
|
2015-06-17 16:25:53 -04:00
|
|
|
|
## Additional groups
|
|
|
|
|
--group-add: Add Linux capabilities
|
|
|
|
|
|
|
|
|
|
By default, the docker container process runs with the supplementary groups looked
|
|
|
|
|
up for the specified user. If one wants to add more to that list of groups, then
|
|
|
|
|
one can use this flag:
|
|
|
|
|
|
|
|
|
|
$ docker run -ti --rm --group-add audio --group-add dbus --group-add 777 busybox id
|
|
|
|
|
uid=0(root) gid=0(root) groups=10(wheel),29(audio),81(dbus),777
|
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Runtime privilege, Linux capabilities, and LXC configuration
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-07-10 19:50:45 -04:00
|
|
|
|
--cap-add: Add Linux capabilities
|
|
|
|
|
--cap-drop: Drop Linux capabilities
|
2014-04-15 20:53:12 -04:00
|
|
|
|
--privileged=false: Give extended privileges to this container
|
2014-09-07 22:49:46 -04:00
|
|
|
|
--device=[]: Allows you to run devices inside the container without the --privileged flag.
|
2015-02-03 22:51:35 -05:00
|
|
|
|
--lxc-conf=[]: Add custom lxc options
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
By default, Docker containers are "unprivileged" and cannot, for
|
|
|
|
|
example, run a Docker daemon inside a Docker container. This is because
|
|
|
|
|
by default a container is not allowed to access any devices, but a
|
2014-04-23 16:48:28 -04:00
|
|
|
|
"privileged" container is given access to all devices (see [lxc-template.go](
|
2014-07-24 18:19:50 -04:00
|
|
|
|
https://github.com/docker/docker/blob/master/daemon/execdriver/lxc/lxc_template.go)
|
2014-04-23 16:48:28 -04:00
|
|
|
|
and documentation on [cgroups devices](
|
|
|
|
|
https://www.kernel.org/doc/Documentation/cgroups/devices.txt)).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-17 18:55:24 -04:00
|
|
|
|
When the operator executes `docker run --privileged`, Docker will enable
|
|
|
|
|
to access to all devices on the host as well as set some configuration
|
2014-09-07 22:49:46 -04:00
|
|
|
|
in AppArmor or SELinux to allow the container nearly all the same access to the
|
2014-04-17 18:55:24 -04:00
|
|
|
|
host as processes running outside containers on the host. Additional
|
|
|
|
|
information about running with `--privileged` is available on the
|
2014-07-01 20:30:25 -04:00
|
|
|
|
[Docker Blog](http://blog.docker.com/2013/09/docker-can-now-run-within-docker/).
|
2014-04-23 16:48:28 -04:00
|
|
|
|
|
2014-09-07 22:49:46 -04:00
|
|
|
|
If you want to limit access to a specific device or devices you can use
|
|
|
|
|
the `--device` flag. It allows you to specify one or more devices that
|
|
|
|
|
will be accessible within the container.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --device=/dev/snd:/dev/snd ...
|
2014-09-07 22:49:46 -04:00
|
|
|
|
|
2014-10-07 23:10:31 -04:00
|
|
|
|
By default, the container will be able to `read`, `write`, and `mknod` these devices.
|
|
|
|
|
This can be overridden using a third `:rwm` set of options to each `--device` flag:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc
|
2014-10-07 23:10:31 -04:00
|
|
|
|
|
2015-03-08 12:42:14 -04:00
|
|
|
|
Command (m for help): q
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc
|
2015-03-08 12:42:14 -04:00
|
|
|
|
You will not be able to write the partition table.
|
2014-10-07 23:10:31 -04:00
|
|
|
|
|
2015-03-08 12:42:14 -04:00
|
|
|
|
Command (m for help): q
|
2014-10-07 23:10:31 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --device=/dev/sda:/dev/xvdc:w --rm -it ubuntu fdisk /dev/xvdc
|
2014-10-07 23:10:31 -04:00
|
|
|
|
crash....
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc
|
2015-03-08 12:42:14 -04:00
|
|
|
|
fdisk: unable to open /dev/xvdc: Operation not permitted
|
2014-10-07 23:10:31 -04:00
|
|
|
|
|
2014-07-11 19:24:44 -04:00
|
|
|
|
In addition to `--privileged`, the operator can have fine grain control over the
|
2014-07-10 19:50:45 -04:00
|
|
|
|
capabilities using `--cap-add` and `--cap-drop`. By default, Docker has a default
|
2015-03-30 22:41:49 -04:00
|
|
|
|
list of capabilities that are kept. The following table lists the Linux capability options which can be added or dropped.
|
2015-03-27 04:41:06 -04:00
|
|
|
|
|
2015-03-27 08:06:49 -04:00
|
|
|
|
| Capability Key | Capability Description |
|
2015-07-01 06:37:39 -04:00
|
|
|
|
| -------------- | ---------------------- |
|
2015-03-27 08:06:49 -04:00
|
|
|
|
| SETPCAP | Modify process capabilities. |
|
|
|
|
|
| SYS_MODULE| Load and unload kernel modules. |
|
|
|
|
|
| SYS_RAWIO | Perform I/O port operations (iopl(2) and ioperm(2)). |
|
|
|
|
|
| SYS_PACCT | Use acct(2), switch process accounting on or off. |
|
|
|
|
|
| SYS_ADMIN | Perform a range of system administration operations. |
|
|
|
|
|
| SYS_NICE | Raise process nice value (nice(2), setpriority(2)) and change the nice value for arbitrary processes. |
|
2015-03-30 22:41:49 -04:00
|
|
|
|
| SYS_RESOURCE | Override resource Limits. |
|
2015-03-27 08:06:49 -04:00
|
|
|
|
| SYS_TIME | Set system clock (settimeofday(2), stime(2), adjtimex(2)); set real-time (hardware) clock. |
|
|
|
|
|
| SYS_TTY_CONFIG | Use vhangup(2); employ various privileged ioctl(2) operations on virtual terminals. |
|
|
|
|
|
| MKNOD | Create special files using mknod(2). |
|
|
|
|
|
| AUDIT_WRITE | Write records to kernel auditing log. |
|
|
|
|
|
| AUDIT_CONTROL | Enable and disable kernel auditing; change auditing filter rules; retrieve auditing status and filtering rules. |
|
|
|
|
|
| MAC_OVERRIDE | Allow MAC configuration or state changes. Implemented for the Smack LSM. |
|
|
|
|
|
| MAC_ADMIN | Override Mandatory Access Control (MAC). Implemented for the Smack Linux Security Module (LSM). |
|
|
|
|
|
| NET_ADMIN | Perform various network-related operations. |
|
|
|
|
|
| SYSLOG | Perform privileged syslog(2) operations. |
|
|
|
|
|
| CHOWN | Make arbitrary changes to file UIDs and GIDs (see chown(2)). |
|
|
|
|
|
| NET_RAW | Use RAW and PACKET sockets. |
|
|
|
|
|
| DAC_OVERRIDE | Bypass file read, write, and execute permission checks. |
|
|
|
|
|
| FOWNER | Bypass permission checks on operations that normally require the file system UID of the process to match the UID of the file. |
|
|
|
|
|
| DAC_READ_SEARCH | Bypass file read permission checks and directory read and execute permission checks. |
|
|
|
|
|
| FSETID | Don't clear set-user-ID and set-group-ID permission bits when a file is modified. |
|
|
|
|
|
| KILL | Bypass permission checks for sending signals. |
|
|
|
|
|
| SETGID | Make arbitrary manipulations of process GIDs and supplementary GID list. |
|
|
|
|
|
| SETUID | Make arbitrary manipulations of process UIDs. |
|
|
|
|
|
| LINUX_IMMUTABLE | Set the FS_APPEND_FL and FS_IMMUTABLE_FL i-node flags. |
|
2015-03-30 22:41:49 -04:00
|
|
|
|
| NET_BIND_SERVICE | Bind a socket to internet domain privileged ports (port numbers less than 1024). |
|
2015-03-27 08:06:49 -04:00
|
|
|
|
| NET_BROADCAST | Make socket broadcasts, and listen to multicasts. |
|
|
|
|
|
| IPC_LOCK | Lock memory (mlock(2), mlockall(2), mmap(2), shmctl(2)). |
|
|
|
|
|
| IPC_OWNER | Bypass permission checks for operations on System V IPC objects. |
|
|
|
|
|
| SYS_CHROOT | Use chroot(2), change root directory. |
|
|
|
|
|
| SYS_PTRACE | Trace arbitrary processes using ptrace(2). |
|
|
|
|
|
| SYS_BOOT | Use reboot(2) and kexec_load(2), reboot and load a new kernel for later execution. |
|
|
|
|
|
| LEASE | Establish leases on arbitrary files (see fcntl(2)). |
|
|
|
|
|
| SETFCAP | Set file capabilities.|
|
|
|
|
|
| WAKE_ALARM | Trigger something that will wake up the system. |
|
|
|
|
|
| BLOCK_SUSPEND | Employ features that can block system suspend. |
|
|
|
|
|
|
2015-03-30 22:41:49 -04:00
|
|
|
|
Further reference information is available on the [capabilities(7) - Linux man page](http://linux.die.net/man/7/capabilities)
|
2015-03-27 04:41:06 -04:00
|
|
|
|
|
|
|
|
|
Both flags support the value `all`, so if the
|
2014-07-10 19:50:45 -04:00
|
|
|
|
operator wants to have all capabilities but `MKNOD` they could use:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --cap-add=ALL --cap-drop=MKNOD ...
|
2014-07-10 19:50:45 -04:00
|
|
|
|
|
|
|
|
|
For interacting with the network stack, instead of using `--privileged` they
|
|
|
|
|
should use `--cap-add=NET_ADMIN` to modify the network interfaces.
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -t -i --rm ubuntu:14.04 ip link add dummy0 type dummy
|
2014-12-05 01:30:47 -05:00
|
|
|
|
RTNETLINK answers: Operation not permitted
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -t -i --rm --cap-add=NET_ADMIN ubuntu:14.04 ip link add dummy0 type dummy
|
2014-12-05 01:30:47 -05:00
|
|
|
|
|
2014-12-05 01:41:18 -05:00
|
|
|
|
To mount a FUSE based filesystem, you need to combine both `--cap-add` and
|
|
|
|
|
`--device`:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --rm -it --cap-add SYS_ADMIN sshfs sshfs sven@10.10.10.20:/home/sven /mnt
|
2014-12-05 01:41:18 -05:00
|
|
|
|
fuse: failed to open /dev/fuse: Operation not permitted
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --rm -it --device /dev/fuse sshfs sshfs sven@10.10.10.20:/home/sven /mnt
|
2014-12-05 01:41:18 -05:00
|
|
|
|
fusermount: mount failed: Operation not permitted
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs
|
2014-12-05 01:41:18 -05:00
|
|
|
|
# sshfs sven@10.10.10.20:/home/sven /mnt
|
|
|
|
|
The authenticity of host '10.10.10.20 (10.10.10.20)' can't be established.
|
|
|
|
|
ECDSA key fingerprint is 25:34:85:75:25:b0:17:46:05:19:04:93:b5:dd:5f:c6.
|
|
|
|
|
Are you sure you want to continue connecting (yes/no)? yes
|
|
|
|
|
sven@10.10.10.20's password:
|
|
|
|
|
root@30aa0cfaf1b5:/# ls -la /mnt/src/docker
|
|
|
|
|
total 1516
|
|
|
|
|
drwxrwxr-x 1 1000 1000 4096 Dec 4 06:08 .
|
|
|
|
|
drwxrwxr-x 1 1000 1000 4096 Dec 4 11:46 ..
|
|
|
|
|
-rw-rw-r-- 1 1000 1000 16 Oct 8 00:09 .dockerignore
|
|
|
|
|
-rwxrwxr-x 1 1000 1000 464 Oct 8 00:09 .drone.yml
|
|
|
|
|
drwxrwxr-x 1 1000 1000 4096 Dec 4 06:11 .git
|
|
|
|
|
-rw-rw-r-- 1 1000 1000 461 Dec 4 06:08 .gitignore
|
|
|
|
|
....
|
|
|
|
|
|
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
|
If the Docker daemon was started using the `lxc` exec-driver
|
2015-08-10 08:48:08 -04:00
|
|
|
|
(`docker daemon --exec-driver=lxc`) then the operator can also specify LXC options
|
2014-04-23 16:48:28 -04:00
|
|
|
|
using one or more `--lxc-conf` parameters. These can be new parameters or
|
|
|
|
|
override existing parameters from the [lxc-template.go](
|
2014-07-24 18:19:50 -04:00
|
|
|
|
https://github.com/docker/docker/blob/master/daemon/execdriver/lxc/lxc_template.go).
|
2014-04-23 16:48:28 -04:00
|
|
|
|
Note that in the future, a given host's docker daemon may not use LXC, so this
|
|
|
|
|
is an implementation-specific configuration meant for operators already
|
|
|
|
|
familiar with using LXC directly.
|
|
|
|
|
|
2014-12-03 20:49:06 -05:00
|
|
|
|
> **Note:**
|
|
|
|
|
> If you use `--lxc-conf` to modify a container's configuration which is also
|
|
|
|
|
> managed by the Docker daemon, then the Docker daemon will not know about this
|
|
|
|
|
> modification, and you will need to manage any conflicts yourself. For example,
|
|
|
|
|
> you can use `--lxc-conf` to set a container's IP address, but this will not be
|
|
|
|
|
> reflected in the `/etc/hosts` file.
|
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
## Logging drivers (--log-driver)
|
2015-06-25 08:00:49 -04:00
|
|
|
|
|
|
|
|
|
The container can have a different logging driver than the Docker daemon. Use
|
|
|
|
|
the `--log-driver=VALUE` with the `docker run` command to configure the
|
|
|
|
|
container's logging driver. The following options are supported:
|
|
|
|
|
|
|
|
|
|
| `none` | Disables any logging for the container. `docker logs` won't be available with this driver. |
|
|
|
|
|
|-------------|-------------------------------------------------------------------------------------------------------------------------------|
|
|
|
|
|
| `json-file` | Default logging driver for Docker. Writes JSON messages to file. No logging options are supported for this driver. |
|
|
|
|
|
| `syslog` | Syslog logging driver for Docker. Writes log messages to syslog. |
|
|
|
|
|
| `journald` | Journald logging driver for Docker. Writes log messages to `journald`. |
|
|
|
|
|
| `gelf` | Graylog Extended Log Format (GELF) logging driver for Docker. Writes log messages to a GELF endpoint likeGraylog or Logstash. |
|
|
|
|
|
| `fluentd` | Fluentd logging driver for Docker. Writes log messages to `fluentd` (forward input). |
|
|
|
|
|
|
|
|
|
|
The `docker logs`command is available only for the `json-file` logging
|
|
|
|
|
driver. For detailed information on working with logging drivers, see
|
2015-08-12 19:55:21 -04:00
|
|
|
|
[Configure a logging driver](/reference/logging/overview/).
|
2015-06-20 00:07:50 -04:00
|
|
|
|
|
|
|
|
|
|
2014-10-20 20:48:58 -04:00
|
|
|
|
## Overriding Dockerfile image defaults
|
2014-04-23 16:48:28 -04:00
|
|
|
|
|
2014-12-15 23:25:37 -05:00
|
|
|
|
When a developer builds an image from a [*Dockerfile*](/reference/builder)
|
2014-04-23 16:48:28 -04:00
|
|
|
|
or when she commits it, the developer can set a number of default parameters
|
|
|
|
|
that take effect when the image starts up as a container.
|
|
|
|
|
|
|
|
|
|
Four of the Dockerfile commands cannot be overridden at runtime: `FROM`,
|
|
|
|
|
`MAINTAINER`, `RUN`, and `ADD`. Everything else has a corresponding override
|
|
|
|
|
in `docker run`. We'll go through what the developer might have set in each
|
|
|
|
|
Dockerfile instruction and how the operator can override that setting.
|
|
|
|
|
|
|
|
|
|
- [CMD (Default Command or Options)](#cmd-default-command-or-options)
|
2014-05-02 20:20:59 -04:00
|
|
|
|
- [ENTRYPOINT (Default Command to Execute at Runtime)](
|
2014-04-23 16:48:28 -04:00
|
|
|
|
#entrypoint-default-command-to-execute-at-runtime)
|
|
|
|
|
- [EXPOSE (Incoming Ports)](#expose-incoming-ports)
|
|
|
|
|
- [ENV (Environment Variables)](#env-environment-variables)
|
|
|
|
|
- [VOLUME (Shared Filesystems)](#volume-shared-filesystems)
|
|
|
|
|
- [USER](#user)
|
|
|
|
|
- [WORKDIR](#workdir)
|
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### CMD (default command or options)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
Recall the optional `COMMAND` in the Docker
|
|
|
|
|
commandline:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
This command is optional because the person who created the `IMAGE` may
|
|
|
|
|
have already provided a default `COMMAND` using the Dockerfile `CMD`
|
|
|
|
|
instruction. As the operator (the person running a container from the
|
|
|
|
|
image), you can override that `CMD` instruction just by specifying a new
|
|
|
|
|
`COMMAND`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
If the image also specifies an `ENTRYPOINT` then the `CMD` or `COMMAND`
|
|
|
|
|
get appended as arguments to the `ENTRYPOINT`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### ENTRYPOINT (default command to execute at runtime)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
--entrypoint="": Overwrite the default entrypoint set by the image
|
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
The `ENTRYPOINT` of an image is similar to a `COMMAND` because it
|
2014-04-17 18:55:24 -04:00
|
|
|
|
specifies what executable to run when the container starts, but it is
|
|
|
|
|
(purposely) more difficult to override. The `ENTRYPOINT` gives a
|
|
|
|
|
container its default nature or behavior, so that when you set an
|
|
|
|
|
`ENTRYPOINT` you can run the container *as if it were that binary*,
|
|
|
|
|
complete with default options, and you can pass in more options via the
|
|
|
|
|
`COMMAND`. But, sometimes an operator may want to run something else
|
|
|
|
|
inside the container, so you can override the default `ENTRYPOINT` at
|
|
|
|
|
runtime by using a string to specify the new `ENTRYPOINT`. Here is an
|
|
|
|
|
example of how to run a shell in a container that has been set up to
|
|
|
|
|
automatically run something else (like `/usr/bin/redis-server`):
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -i -t --entrypoint /bin/bash example/redis
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
or two examples of how to pass more parameters to that ENTRYPOINT:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -i -t --entrypoint /bin/bash example/redis -c ls -l
|
|
|
|
|
$ docker run -i -t --entrypoint /usr/bin/redis-cli example/redis --help
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### EXPOSE (incoming ports)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-07-14 09:51:32 -04:00
|
|
|
|
The following `run` command options work with container networking:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-08-17 18:16:11 -04:00
|
|
|
|
--expose=[]: Expose a port or a range of ports inside the container.
|
|
|
|
|
These are additional to those exposed by the `EXPOSE` instruction
|
2014-04-15 20:53:12 -04:00
|
|
|
|
-P=false : Publish all exposed ports to the host interfaces
|
2015-07-13 09:46:39 -04:00
|
|
|
|
-p=[] : Publish a container᾿s port or a range of ports to the host
|
2014-11-03 13:15:55 -05:00
|
|
|
|
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
|
2015-08-17 18:16:11 -04:00
|
|
|
|
Both hostPort and containerPort can be specified as a
|
|
|
|
|
range of ports. When specifying ranges for both, the
|
|
|
|
|
number of container ports in the range must match the
|
|
|
|
|
number of host ports in the range, for example:
|
|
|
|
|
-p 1234-1236:1234-1236/tcp
|
|
|
|
|
|
|
|
|
|
When specifying a range for hostPort only, the
|
|
|
|
|
containerPort must not be a range. In this case the
|
|
|
|
|
container port is published somewhere within the
|
|
|
|
|
specified hostPort range. (e.g., `-p 1234-1236:1234/tcp`)
|
|
|
|
|
|
2014-11-03 13:15:55 -05:00
|
|
|
|
(use 'docker port' to see the actual mapping)
|
2015-08-17 18:16:11 -04:00
|
|
|
|
|
2015-05-07 16:02:14 -04:00
|
|
|
|
--link="" : Add link to another container (<name or id>:alias or <name or id>)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-08-17 18:16:11 -04:00
|
|
|
|
With the exception of the `EXPOSE` directive, an image developer hasn't
|
|
|
|
|
got much control over networking. The `EXPOSE` instruction defines the
|
|
|
|
|
initial incoming ports that provide services. These ports are available
|
|
|
|
|
to processes inside the container. An operator can use the `--expose`
|
|
|
|
|
option to add to the exposed ports.
|
|
|
|
|
|
|
|
|
|
To expose a container's internal port, an operator can start the
|
|
|
|
|
container with the `-P` or `-p` flag. The exposed port is accessible on
|
|
|
|
|
the host and the ports are available to any client that can reach the
|
|
|
|
|
host.
|
|
|
|
|
|
|
|
|
|
The `-P` option publishes all the ports to the host interfaces. Docker
|
|
|
|
|
binds each exposed port to a random port on the host. The range of
|
|
|
|
|
ports are within an *ephemeral port range* defined by
|
|
|
|
|
`/proc/sys/net/ipv4/ip_local_port_range`. Use the `-p` flag to
|
|
|
|
|
explicitly map a single port or range of ports.
|
|
|
|
|
|
|
|
|
|
The port number inside the container (where the service listens) does
|
|
|
|
|
not need to match the port number exposed on the outside of the
|
|
|
|
|
container (where clients connect). For example, inside the container an
|
|
|
|
|
HTTP service is listening on port 80 (and so the image developer
|
|
|
|
|
specifies `EXPOSE 80` in the Dockerfile). At runtime, the port might be
|
|
|
|
|
bound to 42800 on the host. To find the mapping between the host ports
|
|
|
|
|
and the exposed ports, use `docker port`.
|
|
|
|
|
|
|
|
|
|
If the operator uses `--link` when starting a new client container,
|
2014-06-29 22:25:12 -04:00
|
|
|
|
then the client container can access the exposed port via a private
|
2015-08-17 18:16:11 -04:00
|
|
|
|
networking interface. Docker will set some environment variables in the
|
|
|
|
|
client container to help indicate which interface and port to use. For
|
|
|
|
|
more information on linking, see [the guide on linking container
|
|
|
|
|
together](/userguide/dockerlinks/)
|
2014-04-23 16:48:28 -04:00
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### ENV (environment variables)
|
2014-04-23 16:48:28 -04:00
|
|
|
|
|
2014-10-10 18:40:52 -04:00
|
|
|
|
When a new container is created, Docker will set the following environment
|
|
|
|
|
variables automatically:
|
|
|
|
|
|
2015-03-01 15:13:33 -05:00
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Variable</th>
|
|
|
|
|
<th>Value</th>
|
2014-10-10 18:40:52 -04:00
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
2015-03-01 15:13:33 -05:00
|
|
|
|
<td><code>HOME</code></td>
|
|
|
|
|
<td>
|
2014-10-10 18:40:52 -04:00
|
|
|
|
Set based on the value of <code>USER</code>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
2015-03-01 15:13:33 -05:00
|
|
|
|
<tr>
|
|
|
|
|
<td><code>HOSTNAME</code></td>
|
2015-07-13 09:46:39 -04:00
|
|
|
|
<td>
|
2014-10-10 18:40:52 -04:00
|
|
|
|
The hostname associated with the container
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
2015-03-01 15:13:33 -05:00
|
|
|
|
<td><code>PATH</code></td>
|
2015-07-13 09:46:39 -04:00
|
|
|
|
<td>
|
2014-10-10 18:40:52 -04:00
|
|
|
|
Includes popular directories, such as :<br>
|
|
|
|
|
<code>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</code>
|
|
|
|
|
</td>
|
2015-03-01 15:13:33 -05:00
|
|
|
|
<tr>
|
|
|
|
|
<td><code>TERM</code></td>
|
2015-06-10 07:22:27 -04:00
|
|
|
|
<td><code>xterm</code> if the container is allocated a pseudo-TTY</td>
|
2014-10-10 18:40:52 -04:00
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
The container may also include environment variables defined
|
|
|
|
|
as a result of the container being linked with another container. See
|
2015-08-18 15:25:59 -04:00
|
|
|
|
the [*Container Links*](/userguide/dockerlinks/#connect-with-the-linking-system)
|
2014-10-10 18:40:52 -04:00
|
|
|
|
section for more details.
|
|
|
|
|
|
2015-07-13 09:46:39 -04:00
|
|
|
|
Additionally, the operator can **set any environment variable** in the
|
|
|
|
|
container by using one or more `-e` flags, even overriding those mentioned
|
2014-10-10 18:40:52 -04:00
|
|
|
|
above, or already defined by the developer with a Dockerfile `ENV`:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -e "deep=purple" --rm ubuntu /bin/bash -c export
|
2014-04-15 20:53:12 -04:00
|
|
|
|
declare -x HOME="/"
|
|
|
|
|
declare -x HOSTNAME="85bc26a0e200"
|
|
|
|
|
declare -x OLDPWD
|
|
|
|
|
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
|
declare -x PWD="/"
|
|
|
|
|
declare -x SHLVL="1"
|
|
|
|
|
declare -x container="lxc"
|
|
|
|
|
declare -x deep="purple"
|
|
|
|
|
|
2014-04-17 18:55:24 -04:00
|
|
|
|
Similarly the operator can set the **hostname** with `-h`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-01-18 20:57:44 -05:00
|
|
|
|
`--link <name or id>:alias` also sets environment variables, using the *alias* string to
|
2014-04-23 16:48:28 -04:00
|
|
|
|
define environment variables within the container that give the IP and PORT
|
|
|
|
|
information for connecting to the service container. Let's imagine we have a
|
|
|
|
|
container running Redis:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
# Start the service container, named redis-name
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -d --name redis-name dockerfiles/redis
|
2014-04-15 20:53:12 -04:00
|
|
|
|
4241164edf6f5aca5b0e9e4c9eccd899b0b8080c64c0cd26efe02166c73208f3
|
|
|
|
|
|
|
|
|
|
# The redis-name container exposed port 6379
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker ps
|
2015-03-01 15:13:33 -05:00
|
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
2014-05-01 10:13:34 -04:00
|
|
|
|
4241164edf6f $ dockerfiles/redis:latest /redis-stable/src/re 5 seconds ago Up 4 seconds 6379/tcp redis-name
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-18 20:35:45 -04:00
|
|
|
|
# Note that there are no public ports exposed since we didn᾿t use -p or -P
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker port 4241164edf6f 6379
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014/01/25 00:55:38 Error: No public port '6379' published for 4241164edf6f
|
|
|
|
|
|
2014-07-10 00:14:06 -04:00
|
|
|
|
Yet we can get information about the Redis container's exposed ports
|
2014-04-15 20:53:12 -04:00
|
|
|
|
with `--link`. Choose an alias that will form a
|
|
|
|
|
valid environment variable!
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c export
|
2014-04-15 20:53:12 -04:00
|
|
|
|
declare -x HOME="/"
|
|
|
|
|
declare -x HOSTNAME="acda7f7b1cdc"
|
|
|
|
|
declare -x OLDPWD
|
|
|
|
|
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
|
declare -x PWD="/"
|
|
|
|
|
declare -x REDIS_ALIAS_NAME="/distracted_wright/redis"
|
|
|
|
|
declare -x REDIS_ALIAS_PORT="tcp://172.17.0.32:6379"
|
|
|
|
|
declare -x REDIS_ALIAS_PORT_6379_TCP="tcp://172.17.0.32:6379"
|
|
|
|
|
declare -x REDIS_ALIAS_PORT_6379_TCP_ADDR="172.17.0.32"
|
|
|
|
|
declare -x REDIS_ALIAS_PORT_6379_TCP_PORT="6379"
|
|
|
|
|
declare -x REDIS_ALIAS_PORT_6379_TCP_PROTO="tcp"
|
|
|
|
|
declare -x SHLVL="1"
|
|
|
|
|
declare -x container="lxc"
|
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
|
And we can use that information to connect from another container as a client:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -i -t --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c '/redis-stable/src/redis-cli -h $REDIS_ALIAS_PORT_6379_TCP_ADDR -p $REDIS_ALIAS_PORT_6379_TCP_PORT'
|
2014-04-15 20:53:12 -04:00
|
|
|
|
172.17.0.32:6379>
|
|
|
|
|
|
2014-04-07 14:34:07 -04:00
|
|
|
|
Docker will also map the private IP address to the alias of a linked
|
|
|
|
|
container by inserting an entry into `/etc/hosts`. You can use this
|
|
|
|
|
mechanism to communicate with a linked container by its alias:
|
|
|
|
|
|
2015-03-25 10:48:35 -04:00
|
|
|
|
$ docker run -d --name servicename busybox sleep 30
|
|
|
|
|
$ docker run -i -t --link servicename:servicealias busybox ping -c 1 servicealias
|
2014-04-07 14:34:07 -04:00
|
|
|
|
|
2014-07-14 19:19:37 -04:00
|
|
|
|
If you restart the source container (`servicename` in this case), the recipient
|
|
|
|
|
container's `/etc/hosts` entry will be automatically updated.
|
|
|
|
|
|
2015-01-18 18:23:57 -05:00
|
|
|
|
> **Note**:
|
2015-03-08 12:42:14 -04:00
|
|
|
|
> Unlike host entries in the `/etc/hosts` file, IP addresses stored in the
|
2015-01-18 18:23:57 -05:00
|
|
|
|
> environment variables are not automatically updated if the source container is
|
|
|
|
|
> restarted. We recommend using the host entries in `/etc/hosts` to resolve the
|
|
|
|
|
> IP address of linked containers.
|
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### VOLUME (shared filesystems)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-06-05 15:42:48 -04:00
|
|
|
|
-v=[]: Create a bind mount with: [host-dir:]container-dir[:rw|ro].
|
|
|
|
|
If 'host-dir' is missing, then docker creates a new volume.
|
|
|
|
|
If neither 'rw' or 'ro' is specified then the volume is mounted
|
|
|
|
|
in read-write mode.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
--volumes-from="": Mount all volumes from the given container(s)
|
|
|
|
|
|
2014-06-29 22:25:12 -04:00
|
|
|
|
The volumes commands are complex enough to have their own documentation
|
2015-07-13 09:46:39 -04:00
|
|
|
|
in section [*Managing data in
|
2014-12-15 23:25:37 -05:00
|
|
|
|
containers*](/userguide/dockervolumes). A developer can define
|
2014-06-29 22:25:12 -04:00
|
|
|
|
one or more `VOLUME`'s associated with an image, but only the operator
|
|
|
|
|
can give access from one container to another (or from a container to a
|
2014-04-23 16:48:28 -04:00
|
|
|
|
volume mounted on the host).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### USER
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2015-08-18 02:56:56 -04:00
|
|
|
|
`root` (id = 0) is the default user within a container. The image developer can
|
|
|
|
|
create additional users. Those users are accessible by name. When passing a numeric
|
|
|
|
|
ID, the user does not have to exist in the container.
|
2015-08-13 00:34:57 -04:00
|
|
|
|
|
|
|
|
|
The developer can set a default user to run the first process with the
|
2015-08-18 02:56:56 -04:00
|
|
|
|
Dockerfile `USER` instruction. When starting a container, the operator can override
|
|
|
|
|
the `USER` instruction by passing the `-u` option.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
-u="": Username or UID
|
|
|
|
|
|
2015-08-18 02:56:56 -04:00
|
|
|
|
> **Note:** if you pass a numeric uid, it must be in the range of 0-2147483647.
|
2014-05-17 14:43:31 -04:00
|
|
|
|
|
2015-07-28 17:02:57 -04:00
|
|
|
|
### WORKDIR
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
|
The default working directory for running binaries within a container is the
|
|
|
|
|
root directory (`/`), but the developer can set a different default with the
|
|
|
|
|
Dockerfile `WORKDIR` command. The operator can override this with:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
|
|
-w="": Working directory inside the container
|