Docker run 指令詳解

本文翻譯自docker官網(wǎng):https://github.com/docker/cli/blob/master/docs/reference/commandline/run.md

run

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host value                Add a custom host-to-IP mapping (host:ip) (default [])
  -a, --attach value                  Attach to STDIN, STDOUT or STDERR (default [])
      --blkio-weight value            Block IO (relative weight), between 10 and 1000
      --blkio-weight-device value     Block IO weight (relative device weight) (default [])
      --cap-add value                 Add Linux capabilities (default [])
      --cap-drop value                Drop Linux capabilities (default [])
      --cgroupns string               Cgroup namespace to use
                                      'host':    Run the container in the Docker host's cgroup namespace
                                      'private': Run the container in its own private cgroup namespace
                                      '':        Use the default Docker daemon cgroup namespace specified by the `--default-cgroupns-mode` option
      --cgroup-parent string          Optional parent cgroup for the container
      --cidfile string                Write the container ID to the file
      --cpu-count int                 The number of CPUs available for execution by the container.
                                      Windows daemon only. On Windows Server containers, this is
                                      approximated as a percentage of total CPU usage.
      --cpu-percent int               Limit percentage of CPU available for execution
                                      by the container. Windows daemon only.
                                      The processor resource controls are mutually
                                      exclusive, the order of precedence is CPUCount
                                      first, then CPUShares, and CPUPercent last.
      --cpu-period int                Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                 Limit CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int                CPU shares (relative weight)
      --cpus NanoCPUs                 Number of CPUs (default 0.000)
      --cpu-rt-period int             Limit the CPU real-time period in microseconds
      --cpu-rt-runtime int            Limit the CPU real-time runtime in microseconds
      --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
      --detach-keys string            Override the key sequence for detaching a container
      --device value                  Add a host device to the container (default [])
      --device-cgroup-rule value      Add a rule to the cgroup allowed devices list
      --device-read-bps value         Limit read rate (bytes per second) from a device (default [])
      --device-read-iops value        Limit read rate (IO per second) from a device (default [])
      --device-write-bps value        Limit write rate (bytes per second) to a device (default [])
      --device-write-iops value       Limit write rate (IO per second) to a device (default [])
      --disable-content-trust         Skip image verification (default true)
      --dns value                     Set custom DNS servers (default [])
      --dns-option value              Set DNS options (default [])
      --dns-search value              Set custom DNS search domains (default [])
      --domainname string             Container NIS domain name
      --entrypoint string             Overwrite the default ENTRYPOINT of the image
  -e, --env value                     Set environment variables (default [])
      --env-file value                Read in a file of environment variables (default [])
      --expose value                  Expose a port or a range of ports (default [])
      --group-add value               Add additional groups to join (default [])
      --health-cmd string             Command to run to check health
      --health-interval duration      Time between running the check (ns|us|ms|s|m|h) (default 0s)
      --health-retries int            Consecutive failures needed to report unhealthy
      --health-timeout duration       Maximum time to allow one check to run (ns|us|ms|s|m|h) (default 0s)
      --health-start-period duration  Start period for the container to initialize before counting retries towards unstable (ns|us|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 string        Maximum IO bandwidth limit for the system drive (Windows only)
                                      (Windows only). The format is `<number><unit>`.
                                      Unit is optional and can be `b` (bytes per second),
                                      `k` (kilobytes per second), `m` (megabytes per second),
                                      or `g` (gigabytes per second). If you omit the unit,
                                      the system uses bytes per second.
                                      --io-maxbandwidth and --io-maxiops are mutually exclusive options.
      --io-maxiops uint               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 namespace to use
      --isolation string              Container isolation technology
      --kernel-memory string          Kernel memory limit
  -l, --label value                   Set meta data on a container (default [])
      --label-file value              Read in a line delimited file of labels (default [])
      --link value                    Add link to another container (default [])
      --link-local-ip value           Container IPv4/IPv6 link-local addresses (default [])
      --log-driver string             Logging driver for the container
      --log-opt value                 Log driver options (default [])
      --mac-address string            Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory string                 Memory limit
      --memory-reservation string     Memory soft limit
      --memory-swap string            Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int         Tune container memory swappiness (0 to 100) (default -1)
      --mount value                   Attach a filesystem mount to the container (default [])
      --name string                   Assign a name to the container
      --network-alias value           Add network-scoped alias for the container (default [])
      --network string                Connect a container to a network
                                      'bridge': create a network stack on the default Docker bridge
                                      'none': no networking
                                      'container:<name|id>': reuse another container's network stack
                                      'host': use the Docker host network stack
                                      '<network-name>|<network-id>': connect to a user-defined network
      --no-healthcheck                Disable any container-specified HEALTHCHECK
      --oom-kill-disable              Disable OOM Killer
      --oom-score-adj int             Tune host's OOM preferences (-1000 to 1000)
      --pid string                    PID namespace to use
      --pids-limit int                Tune container pids limit (set -1 for unlimited)
      --privileged                    Give extended privileges to this container
  -p, --publish value                 Publish a container's port(s) to the host (default [])
  -P, --publish-all                   Publish all exposed ports to random ports
      --read-only                     Mount the container's root filesystem as read only
      --restart string                Restart policy to apply when a container exits (default "no")
                                      Possible values are : no, on-failure[:max-retry], always, unless-stopped
      --rm                            Automatically remove the container when it exits
      --runtime string                Runtime to use for this container
      --security-opt value            Security Options (default [])
      --shm-size bytes                Size of /dev/shm
                                      The format is `<number><unit>`. `number` must be greater than `0`.
                                      Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes),
                                      or `g` (gigabytes). If you omit the unit, the system uses bytes.
      --sig-proxy                     Proxy received signals to the process (default true)
      --stop-signal string            Signal to stop a container (default "SIGTERM")
      --stop-timeout=10               Timeout (in seconds) to stop a container
      --storage-opt value             Storage driver options for the container (default [])
      --sysctl value                  Sysctl options (default map[])
      --tmpfs value                   Mount a tmpfs directory (default [])
  -t, --tty                           Allocate a pseudo-TTY
      --ulimit value                  Ulimit options (default [])
  -u, --user string                   Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                 User namespace to use
                                      'host': Use the Docker host user namespace
                                      '': Use the Docker daemon user namespace specified by `--userns-remap` option.
      --uts string                    UTS namespace to use
  -v, --volume value                  Bind mount a volume (default []). The format
                                      is `[host-src:]container-dest[:<options>]`.
                                      The comma-delimited `options` are [rw|ro],
                                      [z|Z], [[r]shared|[r]slave|[r]private],
                                      [delegated|cached|consistent], and
                                      [nocopy]. The 'host-src' is an absolute path
                                      or a name value.
      --volume-driver string          Optional volume driver for the container
      --volumes-from value            Mount volumes from the specified container(s) (default [])
  -w, --workdir string                Working directory inside the container

Description

The docker run command first creates a writeable container layer over the
specified image, and then starts it using the specified command. That is,
docker run is equivalent to the API /containers/create then
/containers/(id)/start. A stopped container can be restarted with all its
previous changes intact using docker start. See docker ps -a to view a list
of all containers.

docker run指令首先會在指定的鏡像上創(chuàng)建一個可寫的容器層狰挡,然后使用指定指令啟動它蓝纲。
docker run相當(dāng)于API /containers/create 然后/containers/(id)/start宜狐。
一個停止的容器可以使用docker start重新啟動桥滨,并且之前的所有更改都完好無損。

The docker run command can be used in combination with docker commit to
change the command that a container runs. There is additional detailed information about docker run in the Docker run reference.

docker run指令可以和docker commit指令結(jié)合使用,以更改容器運行的指令硕盹。
docker run的其他詳細信息在 Docker run reference.

For information on connecting a container to a network, see the "Docker network overview".

有關(guān)將容器網(wǎng)絡(luò)的信息,請參考 "Docker network overview"

Examples

Assign name and allocate pseudo-TTY (--name, -it)

指定名稱并分配偽TTY(使用 --name, -it)

$ 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

This example runs a container named test using the debian:latest
image. The -it instructs Docker to allocate a pseudo-TTY connected to
the container's stdin; creating an interactive bash shell in the container.
In the example, the bash shell is quit by entering
exit 13. This exit code is passed on to the caller of
docker run, and is recorded in the test container's metadata.

此例子使用debian:latest鏡像運行了一個容器叨咖,并命名為test瘩例。
-it指示Docker分配一個鏈接到容器stdin的偽TTY;在容器中創(chuàng)建交互式bashshell。
在該例子中甸各,通過輸入exit 13來退出bashshell垛贤。
這個推出代碼被傳遞給docker run的調(diào)用者,并記錄在測試容器的元數(shù)據(jù)中趣倾。

Capture container ID (--cidfile)

捕獲容器id(--cidfile)

$ docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"

This will create a container and print test to the console. The cidfile
flag makes Docker attempt to create a new file and write the container ID to it.
If the file exists already, Docker will return an error. Docker will close this
file when docker run exits.

這將創(chuàng)建一個容器并打印test到控制臺聘惦。
cidfile標(biāo)志使Docker嘗試創(chuàng)建一個新文件并將容器 ID寫入其中。
如果該文件已經(jīng)存在儒恋,Dokcer將返回一個錯誤善绎。當(dāng)docker run退出時Docker將關(guān)閉該文件。

Full container capabilities (--privileged)

完整的容器功能(--privileged 特權(quán))

$ docker run -t -i --rm ubuntu bash
root@bc338942ef20:/# mount -t tmpfs none /mnt
mount: permission denied

This will not work, because by default, most potentially dangerous kernel
capabilities are dropped; including cap_sys_admin (which is required to mount
filesystems). However, the --privileged flag will allow it to run:

這將行不通诫尽,因為默認情況下禀酱,大多數(shù)潛在危險內(nèi)核功能都會被丟棄;包括cap_sys_admin(裝載文件系統(tǒng)需要)
但是 --privileged標(biāo)識將允許它執(zhí)行:

$ 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

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.

--privileged 標(biāo)志給了容器所有的功能牧嫉,它還解除了設(shè)備cgroup控制器強制執(zhí)行的所有權(quán)限剂跟。
換句話說,容器可以主機可以執(zhí)行的幾乎所有操作驹止。
此標(biāo)志存在是為了允許特殊的用力,像是Docker中運行Docker(套娃)观蜗。

Set working directory (-w)

設(shè)置工作目錄

$ docker  run -w /path/to/dir/ -i -t  ubuntu pwd

The -w lets the command being executed inside directory given, here
/path/to/dir/. If the path does not exist it is created inside the container.

-w 讓指令在給定的目錄中執(zhí)行臊恋,這里/path/to/dir
如果該路徑不存在墓捻,則在容器內(nèi)不創(chuàng)建該路徑抖仅。

Set storage driver options per container

設(shè)置每個容器存儲驅(qū)動程序選項

$ docker run -it --storage-opt size=120G fedora /bin/bash

This (size) will allow to set the container rootfs size to 120G at creation time.
This option is only available for the devicemapper, btrfs, overlay2,
windowsfilter and zfs graph drivers.
For the devicemapper, btrfs, windowsfilter and zfs graph drivers,
user cannot pass a size less than the Default BaseFS Size.
For the overlay2 storage driver, the size option is only available if the
backing fs is xfs and mounted with the pquota mount option.
Under these conditions, user can pass any size less than the backing fs size.

這個(size)允許在創(chuàng)建容器時將容器rootfs大小設(shè)置為120G坊夫。
此選項僅適用于devicemapperbtrfs撤卢、overlay2环凿、 windowsfilterzfs圖形驅(qū)動。
對于devicemapper放吩、 btrfs智听、overlay2windowsfilterzfs圖形驅(qū)動渡紫,用戶不能傳遞小于默認BaseFs大小的size到推。
對于overlay2驅(qū)動程序,只有在backing fs是xfs并且使用pquota安裝選項安裝的情況下size選項才有用惕澎。
在這些條件下莉测,用戶可以傳遞任何小于backing fs大小的size。

Mount tmpfs (--tmpfs)

安裝臨時文件系統(tǒng)

$ docker run -d --tmpfs /run:rw,noexec,nosuid,size=65536k my_image

The --tmpfs flag mounts an empty tmpfs into the container with the rw,
noexec, nosuid, size=65536k options.

--tempfs 標(biāo)志將空的tempfs裝在到容器中唧喉,使用rw,noexec, nosuid, size=65536k選項

Mount volume (-v, --read-only)

$ docker  run  -v `pwd`:`pwd` -w `pwd` -i -t  ubuntu pwd

The -v flag mounts the current working directory into the container. The -w
lets the command being executed inside the current working directory, by
changing into the directory to the value returned by pwd. So this
combination executes the command using the container, but inside the
current working directory.

v標(biāo)志系那個當(dāng)前工作目錄裝載到容器中捣卤。-w允許在當(dāng)前工作目錄中執(zhí)行指令,通過將pwd返回的值更改到目錄中八孝。
所以這個組合使用容器執(zhí)行命令董朝,但是在當(dāng)前工作目錄中。

$ docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash

When the host directory of a bind-mounted volume doesn't exist, Docker
will automatically create this directory on the host for you. In the
example above, Docker will create the /doesnt/exist
folder before starting your container.

當(dāng)綁定裝載卷的主機目錄不存在時唆阿,docker將自動為你在主機上創(chuàng)建該目錄益涧。
在上面的例子中,Docker在啟動容器前將創(chuàng)建/doesnt/exist文件夾驯鳖。

$ docker run --read-only -v /icanwrite busybox touch /icanwrite/here

Volumes can be used in combination with --read-only to control where
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.

卷可以和--read-only結(jié)合使用闲询,以控制容器寫文件。
--read-only標(biāo)志將容器的根文件系統(tǒng)裝載為只讀浅辙,禁止寫入容器的指定卷以外的位置

$ docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v /path/to/static-docker-binary:/usr/bin/docker busybox sh

By bind-mounting the docker unix socket and statically linked docker
binary (refer to get the linux binary),
you give the container the full access to create and manipulate the host's
Docker daemon.

通過綁定掛載docker unix套接字和靜態(tài)鏈接的docker二進制文件,
你授予容器創(chuàng)建和操作主機Docker守護進程的完全訪問權(quán)扭弧。

On Windows, the paths must be specified using Windows-style semantics.

在Windos系統(tǒng)中,必須使用Windows樣式的語義指定路徑

PS C:\> docker run -v c:\foo:c:\dest microsoft/nanoserver cmd /s /c type c:\dest\somefile.txt
Contents of file

PS C:\> docker run -v c:\foo:d: microsoft/nanoserver cmd /s /c type d:\somefile.txt
Contents of file

The following examples will fail when using Windows-based containers, as the
destination of a volume or bind mount inside the container must be one of:
a non-existing or empty directory; or a drive other than C:. Further, the source
of a bind mount must be a local directory, not a file.

當(dāng)使用基于windows的容器時记舆,以下示例將失效鸽捻,
因為容器中的卷或綁定裝載的目標(biāo)必須是以下目錄之一:不存在或空目錄;或C:以外的驅(qū)動器泽腮。
此外御蒲,綁定裝載的源必須是本地目錄,而不是文件

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 ...

For in-depth information about volumes, refer to manage data in containers

有關(guān)卷的詳細信息诊赊,請參考manage data in containers

Add bind mounts or volumes using the --mount flag

使用--mount標(biāo)志添加綁定 mounts 或 volumes

The --mount flag allows you to mount volumes, host-directories and tmpfs
mounts in a container.

--mount標(biāo)志允許您在容器中裝載卷厚满、主機目錄和tmpfs裝載。

The --mount flag supports most options that are supported by the -v or the
--volume flag, but uses a different syntax. For in-depth information on the
--mount flag, and a comparison between --volume and --mount, refer to
the service create command reference.

--mount標(biāo)志支持-v--volume標(biāo)志支持的大多數(shù)選項碧磅,但使用不同的語法碘箍。
有關(guān)--mount標(biāo)志的詳細信息遵馆,以及--volume--mount之間的比較,請參閱service create command reference

Even though there is no plan to deprecate --volume, usage of --mount is recommended.

即使沒有計劃否決--volume丰榴,也建議使用--mount货邓。

Examples:

$ docker run --read-only --mount type=volume,target=/icanwrite busybox touch /icanwrite/here
$ docker run -t -i --mount type=bind,src=/data,dst=/data busybox sh

Publish or expose port (-p, --expose)

公開或暴露端口

$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

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.
The Docker User Guide
explains in detail how to manipulate ports in Docker.

這會容器的8080端口綁定到主機127.0.0.1的TCP端口80上。你還可以指定udpsctp端口四濒。
Docker User Guide 中詳細說明如何在Docker中操作端口换况。

Note that ports which are not bound to the host (i.e., -p 80:80 instead of
-p 127.0.0.1:80:80) will be accessible from the outside. This also applies if
you configured UFW to block this specific port, as Docker manages his
own iptables rules. Read more

請注意,可以從外部訪問未綁定到主機的端口(即-p 80:80而不是-p 127.0.0.1:80:80)峻黍。
如果你將UFW配置為阻止此特定端口复隆,這也適用,因為Docker管理自己的iptables規(guī)則。

$ docker run --expose 80 ubuntu bash

This exposes port 80 of the container without publishing the port to the host
system's interfaces.

這將公開容器的端口80,而不將端口發(fā)布到主機系統(tǒng)的接口

Set environment variables (-e, --env, --env-file)

設(shè)置環(huán)境變量

$ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash

Use the -e, --env, and --env-file flags to set simple (non-array)
environment variables in the container you're running, or overwrite variables
that are defined in the Dockerfile of the image you're running.

使用-e假消、--env--env-file 標(biāo)志 來設(shè)置正在運行的容器中的簡單(非數(shù)組)環(huán)境變量桨菜,
或者覆蓋正在運行的鏡像的Dockerfile中定義的變量

You can define the variable and its value when running the container:

在運行容器時,你可以定義變量及其值:

$ docker run --env VAR1=value1 --env VAR2=value2 ubuntu env | grep VAR
VAR1=value1
VAR2=value2

You can also use variables that you've exported to your local environment:

你還可以使用導(dǎo)出到本地環(huán)境變量的變量:

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
environment, the variable won't be set in the container.

運行該命令時,Docker CLI客戶端將檢查該變量在本地環(huán)境中的值,并將其傳遞給容器。
如果沒有提供=并且該變量未在本地環(huán)境中導(dǎo)出绒北,則變量將不會設(shè)置在容器中。

You can also load the environment variables from a file. This file should use
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.

你也可以從文件中加載環(huán)境變量察署。
此文件應(yīng)該使用語法<variable>=value(將變量設(shè)置為給定值)闷游,或<variable>(從本地環(huán)境獲取)贴汪,#作為注釋脐往。

$ cat env.list
# This is a comment
VAR1=value1
VAR2=value2
USER

$ docker run --env-file env.list ubuntu env | grep VAR
VAR1=value1
VAR2=value2
USER=denis

Set metadata on container (-l, --label, --label-file)

A label is a key=value pair that applies metadata to a container. To label a container with two labels:

一個標(biāo)簽(label)是將元數(shù)據(jù)據(jù)應(yīng)用于容器的key=value對。用兩個標(biāo)簽來標(biāo)記容器:

$ docker run -l my-label --label com.example.foo=bar ubuntu bash

The my-label key doesn't specify a value so the label defaults to an empty
string (""). To add multiple labels, repeat the label flag (-l or --label).

my-lable這個key沒有指定一個值扳埂,因此該標(biāo)簽?zāi)J空字符串(""")业簿。
要添加多個標(biāo)簽,請重復(fù)標(biāo)簽標(biāo)志(-l or --label

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.

key=value必須是唯一的阳懂,以避免覆蓋標(biāo)簽值梅尤。
如果制定具有相同key但value不同的標(biāo)簽,則每個后續(xù)值都將覆蓋上一個值岩调。
Docker使用你最后提供的key=value巷燥。

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:

使用--label-file標(biāo)志從一個文件加載多個標(biāo)簽。 使用 EOL標(biāo)記來分隔文件中的每個標(biāo)簽号枕。
下面的示例是從當(dāng)前目錄的標(biāo)簽文件來加載標(biāo)簽:

$ docker run --label-file ./labels ubuntu bash

The label-file format is similar to the format for loading environment
variables. (Unlike environment variables, labels are not visible to processes
running inside a container.) The following example illustrates a label-file
format:

標(biāo)簽文件(label-file)的格式類似于加載環(huán)境變量的格式缰揪。(與環(huán)境變量不同的是,標(biāo)簽對于容器內(nèi)的進程是不可以見的)
下面的示例演示了標(biāo)簽文件的格式:

com.example.label1="a label"

# this is a comment
com.example.label2=another\ label
com.example.label3

You can load multiple label-files by supplying multiple --label-file flags.

通過提供多個--label-file標(biāo)志來加載多個標(biāo)簽文件堕澄。

For additional information on working with labels, see Labels - custom
metadata in Docker
in
the Docker User Guide.

有關(guān)標(biāo)簽的更多詳情請參考Labels - custom
metadata in Docker

Connect a container to a network (--network)

將容器連接到一個網(wǎng)絡(luò)(--network)

When you start a container use the --network flag to connect it to a network.
This adds the busybox container to the my-net network.

當(dāng)你啟動容器時邀跃,為了將它鏈接到一個網(wǎng)絡(luò),請使用--network標(biāo)志啟動蛙紫。
這將 busybox容器添加到 my-net網(wǎng)絡(luò)拍屑。

$ docker run -itd --network=my-net busybox

You can also choose the IP addresses for the container with --ip and --ip6
flags when you start the container on a user-defined network.

在用戶自定義的網(wǎng)絡(luò)上啟動一個容器時,你還可以通過--ip--ip6的標(biāo)志來選擇容器的IP坑傅。

$ docker run -itd --network=my-net --ip=10.10.9.75 busybox

If you want to add a running container to a network use the docker network connect subcommand.

如果你想將運行中的容器添加到網(wǎng)絡(luò)中僵驰,請使用docker network connect子命令。

You can connect multiple containers to the same network. Once connected, the
containers can communicate easily need only another container's IP address
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.

你可以將多個容器鏈接到相同的網(wǎng)絡(luò)唁毒。
一旦鏈接山蒜茴,容器只需要另一個容器的IP地址或者名字可以方便的通信了。
對于支持multi-host鏈接的overlay網(wǎng)絡(luò)或自定義插件浆西,鏈接到同一個multi-host的容器粉私,
但是從不同啟動引擎啟動的容器也可以通過這種方式來進行通信。

Note

Service discovery is unavailable on the default bridge network. Containers can
communicate via their IP addresses by default. To communicate by name, they
must be linked.
服務(wù)發(fā)現(xiàn)在默認的網(wǎng)橋(bridge)網(wǎng)絡(luò)上不可用近零。默認情況下容器通過他們的IP地址通信诺核。
如果要通過名字來通信,它們必須連接起來久信。

You can disconnect a container from a network using the docker network disconnect command.

你可以使用docker network disconnect指令來容器和網(wǎng)絡(luò)的鏈接窖杀。

Mount volumes from container (--volumes-from)

從容器掛載卷

$ docker run --volumes-from 777f7dc92da7 --volumes-from ba8c0c54f0f2:ro -i -t ubuntu pwd

The --volumes-from flag mounts all the defined volumes from the referenced
containers. Containers can be specified by repetitions of the --volumes-from
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,
the volumes are mounted in the same mode (read write or read only) as
the reference container.

--volumes-from標(biāo)志從引用容器裝載所有定義的卷。容器可以通過重復(fù)--volumes-from參數(shù)來指定裙士。
容器id可以可選的加上:ro或者:rw后最入客,用來分別以只讀(read-only)、讀寫(read-write)模式轉(zhuǎn)載卷腿椎。
默認情況下桌硫,卷將以和引用容器相同的模式(讀寫模式或者只讀模式)來裝載

Labeling systems like SELinux require that proper labels are placed on volume
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.

像 SELinux這樣的標(biāo)簽系統(tǒng)要求在裝入容器的卷內(nèi)容上放置適當(dāng)?shù)臉?biāo)簽。
如果沒有標(biāo)簽酥诽,安全系統(tǒng)可能會阻止容器內(nèi)運行的進程使用內(nèi)容鞍泉。
默認情況下,Docker不會改變操作系統(tǒng)設(shè)置的標(biāo)簽肮帐。

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.

要改變?nèi)萜魃舷挛闹械臉?biāo)簽咖驮,你可以向卷裝載添加兩個后綴:z or :Z
這些后綴告訴Docker重新標(biāo)記共享卷上的文件對象训枢。
z選項告訴Docker兩個容器共享卷內(nèi)容托修。因此,Docker使用共享標(biāo)簽來標(biāo)記內(nèi)容恒界。共享卷標(biāo)志允許所有容器讀/寫內(nèi)容睦刃。
Z選項告訴Docker使用私有的非共享標(biāo)簽來標(biāo)記內(nèi)容。只有當(dāng)前容器才能使用專用卷十酣。

Attach to STDIN/STDOUT/STDERR (-a)

鏈接到 STDIN/STDOUT/STDERR

The -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.

-a 標(biāo)志告訴 docker run 綁定到容器的 STDIN涩拙、 STDOUTSTDERR际长。
這使得可以根據(jù)需要操縱輸出和輸入。

$ echo "test" | docker run -i -a stdin ubuntu cat -

This pipes data into a container and prints the container's ID by attaching
only to the container's STDIN.

這將數(shù)據(jù)管道化到容器中兴泥,并通過僅連接到容器的STDIN來打印容器的ID工育。

$ docker run -a stderr ubuntu echo test

This isn't going to print anything unless there's an error because we've
only attached to the STDERR of the container. The container's logs
still store what's been written to STDERR and STDOUT.

除非出現(xiàn)錯誤,否則不會打印任何內(nèi)容搓彻,因為我們只鏈接到容器的STDERR如绸。
容器的日志仍然存儲寫入STDERR and STDOUT的內(nèi)容。

$ cat somefile | docker run -i -a stdin mybuilder dobuild

This is how piping a file into a container could be done for a build.
The container's ID will be printed after the build is done and the build
logs could be retrieved using docker logs. This is
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.

這就是將文件管道化到容器的方法旭贬。
容器的ID將在構(gòu)建完成后打印怔接,并且可以使用docker logs檢索構(gòu)建日志。
如果你需要將文件或者其他內(nèi)容通過管道傳輸?shù)饺萜飨」欤⒃谌萜鬟\行完成后檢索容器的ID扼脐,那么這將非常有用。

Add host device to container (--device)

將主機設(shè)備添加到容器奋刽。

$ docker run --device=/dev/sdc:/dev/xvdc \
             --device=/dev/sdd --device=/dev/zero:/dev/nulo \
             -i -t \
             ubuntu ls -l /dev/{xvdc,sdd,nulo}

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
crw-rw-rw- 1 root root 1, 5 Feb  9 16:05 /dev/nulo

It is often necessary to directly expose devices to a container. The --device
option enables that. For example, a specific block storage device or loop
device or audio device can be added to an otherwise unprivileged container
(without the --privileged flag) and have the application directly access it.

通常需要將設(shè)備直接暴露給容器谎势。--device選項可以做到。
例如杨名,可以將特定的存儲塊設(shè)備脏榆、循環(huán)設(shè)備或者音頻設(shè)備添加到一個沒有特權(quán)的容器(沒有 --privileged標(biāo)志),
并讓應(yīng)用程序直接使用它台谍。

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. If the container is running in privileged mode, then the permissions specified
will be ignored.

默認情況下须喂,容器將能夠讀、寫和管理這些設(shè)備趁蕊∥肷可以使用三個:rwm選項集合來覆蓋每個--device標(biāo)志。
如果容器以特權(quán)模式運行掷伙,則指定的權(quán)限將被忽略是己。

$ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk  /dev/xvdc

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.

Command (m for help): q

$ docker run --device=/dev/sda:/dev/xvdc:rw --rm -it ubuntu fdisk  /dev/xvdc

Command (m for help): q

$ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk  /dev/xvdc
fdisk: unable to open /dev/xvdc: Operation not permitted

Note

The --device option cannot be safely used with ephemeral devices. Block devices
that may be removed should not be added to untrusted containers with --device.
--device選項不能安全的用于臨時設(shè)備。
不應(yīng)該適用--device將可能被刪除的塊設(shè)備添加到不受信任的容器中任柜。

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
.
Refer to the table defined in the Windows container
docs

for a list of container-supported device interface class GUIDs.

對于Windows卒废,傳遞給--device選項的字符串格式為--device=<IdType>/<Id>
從Windows Server 2019 到 Windows 10 October 2018 Update,Windows僅支持類的IdType和作為設(shè)備接口類GUID的Id宙地。
有關(guān)容器支持的設(shè)備接口類guid的列表摔认,請參閱Windows容器文檔中定義的表。

If this option is specified for a process-isolated Windows container, all
devices that implement the requested device interface class GUID are made
available in the container. For example, the command below makes all COM
ports on the host visible in the container.

如果為進程隔離的Windows容器指定了此選項宅粥,實現(xiàn)請求的設(shè)備接口類GUID的所有設(shè)備都在容器中可用参袱。
例如,下面的命令使主機上的所有COM端口在容器中可見。

PS C:\> docker run --device=class/86E0D1E0-8089-11D0-9CE4-08003E301F73 mcr.microsoft.com/windows/servercore:ltsc2019

Note

The --device option is only supported on process-isolated Windows containers.
This option fails if the container isolation is hyperv or when running Linux
Containers on Windows (LCOW).

Access an NVIDIA GPU

The --gpus flag allows you to access NVIDIA GPU resources. First you need to
install nvidia-container-runtime.
Visit Specify a container's resources
for more information.

--gpus標(biāo)志允許你訪問 NVIDIA GPU 資源抹蚀。首先需要安裝 nvidia-container-runtime剿牺。
訪問 Specify a container's resources 獲取更多信息。

To use --gpus, specify which GPUs (or all) to use. If no value is provied, all
available GPUs are used. The example below exposes all available GPUs.

使用--gpus 指定哪個GPU(或者所有)使用环壤。如果沒有提供值牢贸,所有可用的GPU都被使用。
下面的示例公開了所有可用的GPU镐捧。

$ docker run -it --rm --gpus all ubuntu nvidia-smi

Use the device option to specify GPUs. The example below exposes a specific
GPU.

使用device選項制定GPU。下面的示例公開了一個特定的GPU臭增。

$ docker run -it --rm --gpus device=GPU-3a23c669-1f69-c64e-cf85-44e9b07e7a2a ubuntu nvidia-smi

The example below exposes the first and third GPUs.

下面的示例公開了第一個和第三個GPU懂酱。

$ docker run -it --rm --gpus device=0,2 nvidia-smi

Restart policies (--restart)

重啟策略

Use Docker's --restart to specify a container's restart policy. A restart
policy controls whether the Docker daemon restarts a container after exit.
Docker supports the following restart policies:

使用 Docker的 --restart來指定容器的重啟策略。
一個重啟策略控制Docker守護進程是否在容器推出后重新啟動它誊抛。
Docker支持一下重啟策略:

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.
unless-stopped Restart the container unless it is 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 will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.

no : 不要在容器退出時重啟它列牺,這是默認值。
on-failure[:max-retries] : 只在容器以非0狀態(tài)退出時才重啟拗窃。(可選)限制Docker守護進程嘗試重啟的次數(shù)瞎领。
unless-stopped : 重新啟動容器,除非容器已顯式停止或者Docker自身已停止或重新啟動随夸。
always : 無論推出狀態(tài)如何九默,始終重啟容器。當(dāng)你指定always宾毒,Docker守護進程將無限期地重啟容器驼修。無論容器的當(dāng)前狀態(tài)如何,容器總在守護進程啟動時啟動诈铛。

$ docker run --restart=always redis

This will run the redis container with a restart policy of always
so that if the container exits, Docker will restart it.

這將以 always 的重啟策略運行redis乙各,所以如果容器推出,Docker將重啟它幢竹。

More detailed information on restart policies can be found in the
Restart Policies (--restart)
section of the Docker run reference page.

在Docker run 參考頁有更多的關(guān)于重啟策略詳情 Restart Policies (--restart)

Add entries to container hosts file (--add-host)

向容器的hosts文件添加條目

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:

你可以通過使用一個或多個--add-host標(biāo)志其他hosts添加到容器的/ect/hosts文件

$ docker run --add-host=docker:10.180.0.1 --rm -it debian

root@f38c87f2a42d:/# ping docker
PING docker (10.180.0.1): 48 data bytes
56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms
56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms
^C--- docker ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms

Sometimes you need to connect to the Docker host from within your
container. To enable this, pass the Docker host's IP address to
the container using the --add-host flag. To find the host's address,
use the ip addr show command.

有時候你需要從容器內(nèi)鏈接Docker主機耳峦。要使用此功能,請使用--add-host標(biāo)志將Docker主機的IP地址傳遞給容器焕毫。
要查詢主機地址蹲坷,請使用ip addr show指令。

The flags you pass to ip addr show depend on whether you are
using IPv4 or IPv6 networking in your containers. Use the following
flags for IPv4 address retrieval for a network device named eth0:

傳遞給ip addr show的標(biāo)志取決于容器中使用的是IPV4還是IPV6網(wǎng)絡(luò)邑飒。
使用以下標(biāo)志來檢索名為eth0d額網(wǎng)絡(luò)設(shè)備的IPV4地址冠句。

$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print $2}' | cut -d / -f 1 | sed -n 1p`
$ docker run  --add-host=docker:${HOSTIP} --rm -it debian

For IPv6 use the -6 flag instead of the -4 flag. For other network
devices, replace eth0 with the correct device name (for example docker0
for the bridge device).

對于IPV6,使用-6 標(biāo)志替代 -4標(biāo)志幸乒。
對于其它網(wǎng)路哦設(shè)備懦底,使用正確的設(shè)備名稱替代eth0(例如 網(wǎng)橋設(shè)備的docker0 )

Set ulimits in container (--ulimit)

在容器中設(shè)置限制

Since setting ulimit settings in a container requires extra privileges not
available in the default container, you can set these using the --ulimit flag.
--ulimit is specified with a soft and hard limit as such:
<type>=<soft limit>[:<hard limit>], for example:

在容器中如果需要默認容器中不可用的額外特權(quán),可以使用--ulimit標(biāo)志來設(shè)置這些特權(quán)。
--ulimit被指定為軟限制和應(yīng)限制 比如:<type>=<soft limit>[:<hard limit>]`, 例如:

$ docker run --ulimit nofile=1024:1024 --rm debian sh -c "ulimit -n"
1024

Note

If you do not provide a hard limit, the soft limit is used
for both values. If no ulimits are set, they are inherited from
the default ulimits set on the daemon. The as option is disabled now.
In other words, the following script is not supported:
如果你不提供hard limit聚唐, 則soft limit 將應(yīng)用于這兩個值丐重。
如果為設(shè)置ulimit,則從守護進程上設(shè)置的默認ulimit繼承杆查。

$ docker run -it --ulimit as=1024 fedora /bin/bash`

The values are sent to the appropriate syscall as they are set.
Docker doesn't perform any byte conversion. Take this into account when setting the values.

這些值在設(shè)置時被發(fā)送到相應(yīng)的syscall扮惦。Dockers不執(zhí)行任何字節(jié)轉(zhuǎn)換。設(shè)置值時要考慮到這一點亲桦。

For nproc usage

Be careful setting nproc with the ulimit flag as nproc is designed by Linux to set the
maximum number of processes available to a user, not to a container. For example, start four
containers with daemon user:

請小心使用ulimit標(biāo)志設(shè)置nproc崖蜜,因為nproc是由Linux設(shè)計的,用于設(shè)置用戶可用的最大進程數(shù)客峭,而不是容器容器可用的最大進程數(shù)豫领。
例如,用daemon用戶啟動四個容器:

$ 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

The 4th container fails and reports "[8] System error: resource temporarily unavailable" error.
This fails because the caller set nproc=3 resulting in the first three containers using up
the three processes quota set for the daemon user.

第四個容器失敗報告"[8] System error: resource temporarily unavailable"錯誤舔琅。
失敗是因為調(diào)用者設(shè)置nproc=3導(dǎo)致前三個容器使用了為daemon用戶設(shè)置的三個進程配額等恐。

Stop container with signal (--stop-signal)

帶信號停止容器

The --stop-signal flag sets the system call signal that will be sent to the container to exit.
This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9,
or a signal name in the format SIGNAME, for instance SIGKILL.

--stop signal標(biāo)志設(shè)置將發(fā)送到容器以退出的系統(tǒng)調(diào)用信號。
這個信號可以是一個有效的無符號數(shù)字备蚓,它與內(nèi)核syscall表中的一個位置相匹配课蔬,例如9,也可以是SIGNAME格式的信號名郊尝,例如SIGKILL二跋。

Optional security options (--security-opt)

可選安全選項

On Windows, this flag can be used to specify the credentialspec option.
The credentialspec must be in the format file://spec.txt or registry://keyname.

在Windos上,此標(biāo)志可以用于指定 credentialspec 選項流昏。
credentialspec 的格式必須是file://spec.txt or registry://keyname同欠。

Stop container with timeout (--stop-timeout)

使用超時來停止容器

The --stop-timeout flag sets the timeout (in seconds) that a pre-defined (see --stop-signal) system call
signal that will be sent to the container to exit. After timeout elapses the container will be killed with SIGKILL.

--stop timeout標(biāo)志設(shè)置將發(fā)送到容器以退出的預(yù)定義(請參閱--stop signal)系統(tǒng)調(diào)用信號的超時(以秒為單位)。
超時過后横缔,容器將被SIGKILL殺死铺遂。

Specify isolation technology for container (--isolation)

指定容器的隔離技術(shù)

This option is useful in situations where you are running Docker containers on
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:

此選項在Windows上運行Docker容器的情況下非常有用。--isolation<value>選項設(shè)置容器的隔離技術(shù)茎刚。
在Linux上襟锐,唯一受支持的是使用Linux namespace的默認選項。這兩個命令在Linux上是等效的:

$ docker run -d busybox top
$ docker run -d --isolation default busybox top

On Windows, --isolation can take one of these values:

在Windows上膛锭,--isolation 可以采用以下值之一

Value Description
default Use the value specified by the Docker daemon's --exec-opt or system default (see below).
process Shared-kernel namespace isolation (not supported on Windows client operating systems older than Windows 10 1809).
hyperv Hyper-V hypervisor partition-based isolation.

default : 使用Docker守護進程的--exec opt或系統(tǒng)默認值指定的值(見下文)粮坞。
process : 共享內(nèi)核(Shared-kernel) namespace隔離(在早于Windows 10 1809的Windows客戶端操作系統(tǒng)上不受支持)。
hyperv : 基于Hyper-V虛擬機監(jiān)控程序分區(qū)的隔離初狰。

The default isolation on Windows server operating systems is process. The default
isolation on Windows client operating systems is hyperv. An attempt to start a container on a client
operating system older than Windows 10 1809 with --isolation process will fail.

Windows服務(wù)器操作系統(tǒng)上的默認隔離是process莫杈。Windows客戶端操作系統(tǒng)上的默認隔離是hyperv
嘗試在早于Windows101809的客戶端操作系統(tǒng)上用--isolation process啟動容器將失敗奢入。

On Windows server, assuming the default configuration, these commands are equivalent
and result in process isolation:

在Windows服務(wù)器上筝闹,假設(shè)默認配置,這些命令導(dǎo)致的process隔離是等效的:

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

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:

如果您已經(jīng)在Docker守護程序上設(shè)置了--exec-opt isolation=hyperv選項,
或者正在針對基于Windows客戶端的守護程序運行关顷,那么這些命令是等效的糊秆,并且會導(dǎo)致hyperv隔離:

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

Specify hard limits on memory available to containers (-m, --memory)

指定容器可用內(nèi)存的硬限制

These parameters always set an upper limit on the memory available to the container. On Linux, this
is set on the cgroup and applications in a container can query it at /sys/fs/cgroup/memory/memory.limit_in_bytes.

這些參數(shù)總是設(shè)置容器可用內(nèi)存的上限。
在Linux上议双,這是在cgroup上設(shè)置的痘番,容器中的應(yīng)用程序可以在/sys/fs/cgroup/memory/memory.limit_in_bytes查詢它。

On Windows, this will affect containers differently depending on what type of isolation is used.

在Windows上平痰,這將根據(jù)所使用的隔離類型對容器產(chǎn)生不同的影響

  • With process isolation, Windows will report the full memory of the host system, not the limit to applications running inside the containe

    使用process隔離汞舱,Windows將報告主機系統(tǒng)的全部內(nèi)存,而不是對容器內(nèi)運行的應(yīng)用程序的限制
    ```powershell
    PS C:> docker run -it -m 2GB --isolation=process microsoft/nanoserver powershell Get-ComputerInfo memory

    CsTotalPhysicalMemory : 17064509440
    CsPhyicallyInstalledMemory : 16777216
    OsTotalVisibleMemorySize : 16664560
    OsFreePhysicalMemory : 14646720
    OsTotalVirtualMemorySize : 19154928
    OsFreeVirtualMemory : 17197440
    OsInUseVirtualMemory : 1957488
    OsMaxProcessMemorySize : 137438953344

    
    
  • With hyperv isolation, Windows will create 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."

    使用hyperv隔離宗雇,Windows將創(chuàng)建一個足夠大的實用虛擬機來容納內(nèi)存限制昂芜,再加上托管容器所需的最小操作系統(tǒng)。
    這個大小被報告為“Total Physical Memory”

    PS C:\> docker run -it -m 2GB --isolation=hyperv microsoft/nanoserver powershell Get-ComputerInfo *memory*
    
    CsTotalPhysicalMemory      : 2683355136
    CsPhyicallyInstalledMemory :
    OsTotalVisibleMemorySize   : 2620464
    OsFreePhysicalMemory       : 2306552
    OsTotalVirtualMemorySize   : 2620464
    OsFreeVirtualMemory        : 2356692
    OsInUseVirtualMemory       : 263772
    OsMaxProcessMemorySize     : 137438953344
    

Configure namespaced kernel parameters (sysctls) at runtime

在運行時配置命名空間內(nèi)核參數(shù)(sysctls)

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:

--sysctl設(shè)置容器中的命名空間內(nèi)核參數(shù)(sysctl)逾礁。
例如,要在容器網(wǎng)絡(luò)名稱空間中啟用IP轉(zhuǎn)發(fā)访惜,請運行以下命令:

$ docker run --sysctl net.ipv4.ip_forward=1 someimage

Note

Not all sysctls are namespaced. Docker does not support changing sysctls
inside of a container that also modify the host system. As the kernel
evolves we expect to see more sysctls become namespaced.
并非所有sysctl都有名稱空間嘹履。Docker不支持在同時修改主機系統(tǒng)的容器內(nèi)更改sysctl。
隨著內(nèi)核的發(fā)展债热,我們期望看到更多的sysctl被命名砾嫉。

Currently supported sysctls

目前支持的sysctls

IPC Namespace:

  • 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.

Network Namespace:

  • Sysctls beginning with net.*
  • If you use the --network=host option using these sysctls are not allowed.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市窒篱,隨后出現(xiàn)的幾起案子焕刮,更是在濱河造成了極大的恐慌,老刑警劉巖墙杯,帶你破解...
    沈念sama閱讀 211,817評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件配并,死亡現(xiàn)場離奇詭異,居然都是意外死亡高镐,警方通過查閱死者的電腦和手機溉旋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,329評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嫉髓,“玉大人观腊,你說我怎么就攤上這事∷阈校” “怎么了梧油?”我有些...
    開封第一講書人閱讀 157,354評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長州邢。 經(jīng)常有香客問我儡陨,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,498評論 1 284
  • 正文 為了忘掉前任迄委,我火速辦了婚禮褐筛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘叙身。我一直安慰自己渔扎,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,600評論 6 386
  • 文/花漫 我一把揭開白布信轿。 她就那樣靜靜地躺著晃痴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪财忽。 梳的紋絲不亂的頭發(fā)上倘核,一...
    開封第一講書人閱讀 49,829評論 1 290
  • 那天,我揣著相機與錄音即彪,去河邊找鬼紧唱。 笑死,一個胖子當(dāng)著我的面吹牛隶校,可吹牛的內(nèi)容都是我干的漏益。 我是一名探鬼主播,決...
    沈念sama閱讀 38,979評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼深胳,長吁一口氣:“原來是場噩夢啊……” “哼绰疤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起舞终,我...
    開封第一講書人閱讀 37,722評論 0 266
  • 序言:老撾萬榮一對情侶失蹤轻庆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后敛劝,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體余爆,經(jīng)...
    沈念sama閱讀 44,189評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,519評論 2 327
  • 正文 我和宋清朗相戀三年夸盟,在試婚紗的時候發(fā)現(xiàn)自己被綠了龙屉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,654評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡满俗,死狀恐怖转捕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情唆垃,我是刑警寧澤五芝,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站辕万,受9級特大地震影響枢步,放射性物質(zhì)發(fā)生泄漏沉删。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,940評論 3 313
  • 文/蒙蒙 一醉途、第九天 我趴在偏房一處隱蔽的房頂上張望矾瑰。 院中可真熱鬧,春花似錦隘擎、人聲如沸殴穴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,762評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽采幌。三九已至,卻和暖如春震桶,著一層夾襖步出監(jiān)牢的瞬間休傍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,993評論 1 266
  • 我被黑心中介騙來泰國打工蹲姐, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留磨取,地道東北人。 一個月前我還...
    沈念sama閱讀 46,382評論 2 360
  • 正文 我出身青樓柴墩,卻偏偏與公主長得像忙厌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子拐邪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,543評論 2 349

推薦閱讀更多精彩內(nèi)容