Docker 实战操作之运行命令docker run详解(三)
docker run 官方说明如下:
[root@localhost opt]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes 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 mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int 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)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
1、 -a
-a, --attach=[] Attach to STDIN, STDOUT or STDERR
如果在执行 run 命令时没有指定 -a,那么 docker 默认会挂载所有标准数据流,包括输入输出和错误。你可以特别指定挂载哪个标准流。
docker run -a stdin -a stdout -i -t centos:8.2.2004 /bin/bash
只挂载标准输入输出
2、--add-host
--add-host=[] Add a custom host-to-IP mapping (host:ip)
添加 host-ip 到容器的 hosts 文件
# 添加一个host-ip
[root@localhost opt]# docker run -it --name=centos82 --add-host db:10.0.0.17 --hostname=centos8.2 centos:8.2.2004 /bin/bash
[root@centos8 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.0.0.17 db # 这里自动添加启动容器时--add-host设置的主机地址
172.17.0.6 centos82
# 添加多个host-ip
[root@localhost opt]# docker run -it --name=centos8.3 --add-host=web1:10.0.0.16 --add-host=web2:10.0.0.17 --add-host=web3:10.0.0.18 --hostname=centos83 centos:8.2.2004 /bin/bash
[root@centos8 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.0.0.16 web1
10.0.0.17 web2
10.0.0.18 web3
172.17.0.6 centos83
3、--blkio-weight
--blkio-weight=0 Block IO (relative weight), between 10 and 1000
相对于 CPU 和 内存 的配额控制,docker 对磁盘 IO 的控制相对不成熟,大多数都必须在有宿主机设备的情况下使用。主要包括以下参数:
- device-read-bps:限制此设备上的读速度(bytes per second),单位可以是 kb、mb 或者 gb.
- device-read-iops:通过每秒读 IO 次数来限制指定设备的读速度。
- device-write-bps :限制此设备上的写速度(bytes per second),单位可以是kb、mb或者gb。
- device-write-iops:通过每秒写 IO 次数来限制指定设备的写速度。
- blkio-weight:容器默认磁盘 IO 的加权值,有效值范围为10-100。
- blkio-weight-device: 针对特定设备的 IO 加权控制。其格式为 DEVICE_NAME:WEIGHT 存储配额控制的相关参数,可以参考Red Hat 文档中 blkio 这一章,了解它们的详细作用。
3.1、磁盘IO配额控制示例
blkio-weight
要使 –blkio-weight 生效,需要保证 IO 的调度算法为 CFQ。可以使用下面的方式查看:
root@localhost:~# cat /sys/block/sda/queue/scheduler
noop [deadline] cfq
使用下面的命令创建两个 –blkio-weight 值不同的容器:
docker run -ti –rm –blkio-weight 100 centos:7.9.2009
docker run -ti –rm –blkio-weight 1000 centos:7.9.2009
在容器中同时执行下面的 dd 命令,进行测试:
time dd if=/dev/zero of=test.out bs=1M count=1024 oflag=direct
device-write-bps
使用下面的命令创建容器,并执行命令验证写速度的限制
docker run -tid --name disk1 –device-write-bps /dev/sda:1mb centos:7.9.2009
3.2、容器资源限制
在Docker的默认配置中是没有对容器做资源限制的,在容器发生异常的状况下有可能耗光宿主机所有资源导致OOM,OOM一旦发生任何进程都有可能被系统杀掉。所以在启动Docker容器时最好是指定CPU、内存、硬盘大小等硬件配额。Docker通过cgroup来控制这些资源配额,也就是说下面讲到的命令其实都是在配置cgroup。比如调整了CPU的限制后,在/sys/fs/cgroup/cpu/docker/container_id/可以看到设置的值。cgroup分配的结果取决于当时主机和其他容器的运行状况,如果A容器很空闲,那么即使给它分配了更多份额,也会被其他忙碌的容器抢夺资源,是一种动态分配.
3.2.1 Docker内存限制
-m | --memory:设置容器可用内存大小,支持单位有k、m、g
--memory-swap:设置内存+swap的总大小,配合-m一起使用,该选项不指定的话默认是-m的2倍大小
--oom-kill-disable:不允许容器因为OOM被系统杀掉,建议配合-m选项使用,避免宿主内存被耗光
# 容器使用的内存上限为1G,虚拟内存500M,超过该内存限制就会停止容器
docker run -itd -m 1024m --memoery-swap=1524m --oom-kill-disable centos:7.9.2009
3.2.2 Docker硬盘I/O限制
--device-read-bps:限制读某个设备的bps
--device-write-bps:限制写某个设备的bps
--device-read-iops:限制读某个设备的iops
--device-write-iops:限制写某个设备的iops
# 限制最高写速度为30M/S
docker run -it --name iotest --device-write-bps /dev/sda1:30M docker centos:7.9.2009
3.2.3 Docker CPU配额设置
-c | --cpu-share:CPU权重设置,Docker会把CPU资源分成1024份,如果对一个容器设置了1024意味它独占所有CPU资源,如果多个容器同时进行了设置,那么每个容器最后会通过各自占有的百分比来分配CPU资源
--cpus:限制CPU核数
--cpuset-cpus:设置CPU亲和,让容器绑定在指定的CPU上
docker run -it --name test1 --cpu-shares 1024 centos:7.9.2009 /bin/bash
docker run -it --name test2 --cpu-shares 512 centos:7.9.2009 /bin/bash
CPU亲和设置,比如物理机有16个核心,创建的容器只能用0、1、2这三个内核
docker run -itd --name testcpu --cpuset-cpus 0-2 centos:7.9.2009
cat /sys/fs/cgroup/cpuset/docker/dockerid/cpuset.cpus # 注意dockerid需要替换成容器id
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23cdb49b1e85 centos:7.9.2009 "/bin/bash" 3 minutes ago Up 3 minutes testcpu
[root@localhost opt]# cat /sys/fs/cgroup/cpuset/docker/23cdb49b1e854c3c35d9709cb5d83a9eef4cd0cbc0772753293f15dfe165cda9/cpuset.cpus
0-2
3.2.4 容器空间大小限制
在 docker 使用 devicemapper 作为存储驱动时,默认每个容器和镜像的最大大小为 10 G。如果需要调整,可以在 daemon 启动参数中,使用 dm.basesize 来指定,但需要注意的是,修改这个值,不仅仅需要重启 docker daemon 服务,还会导致宿主机上的所有本地镜像和容器都被清理掉。 使用 aufs 或者 overlay 等其他存储驱动时,没有这个限制。
4、--cidfile=
--cidfile= Write the container ID to the file
将 container ID 保存到 cid_file, 保存的格式为长 UUID
docker run -it -d --cidfile=cid_file centos:7.9.2009 /bin/bash
#cat cid_file
3a9d08b718536379395b6ff66e5b42c7a20db9a0afd4337e4103230fd0a859ab
5、--cpu-shares
--cpu-shares=0 CPU shares (relative weight)
默认情况下,使用 -c 或者 --cpu-shares 参数值为0,可以赋予当前活动 container 1024个 cpu 共享周期。这个0值可以针对活动的 container 进行修改来调整不同的 cpu 循环周期。 比如,我们使用 -c 或者 --cpu-shares=0 启动了 C0,C1,C2 三个c ontainer,使用 -c/–cpu-shares=512 启动了C3.这时,C0,C1,C2 可以 100%的使用 CPU 资源(1024),但 C3 只能使用 50%的 CPU 资源(512)。如果这个 host 的 OS 是时序调度类型的,每个 CPU 时间片是 100 微秒,那么C0,C1,C2将完全使用掉这 100 微秒,而 C3 只能使用 50 微秒
6、--cpu-period, --cpu-quota
--cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota=0 Limit CPU CFS (Completely Fair Scheduler) quota
–cpu-period 和 --cpu-quota,这两个参数是相互配合的,–cpu-period 和 --cpu-quota 的这种配置叫 Ceiling Enforcement Tunable Parameters,–cpu-shares 的这种配置叫 Relative Shares Tunable Parameters。–cpu-period是用来指定容器对CPU的使用要在多长时间内做一次重新分配,而 --cpu-quota 是用来指定在这个周期内,最多可以有多少时间用来跑这个容器。跟 --cpu-shares 不同的是这种配置是指定一个绝对值,而且没有弹性在里面,容器对CPU资源的使用绝对不会超过配置的值。
比如说 A 容器配置的 --cpu-period=100000 --cpu-quota=50000,那么 A 容器就可以最多使用 50% 个 CPU 资源,如果配置的–cpu-quota=200000,那就可以使用 200% 个 CPU 资源。
那么有什么样的应用场景呢?简单举个例子,加入对外提供 A 和 B 两个服务,但是 A 的优先级比 B 要高,假如只用 --cpu-shares来配置,B 服务占用资源太高时是会对 A 有一定的影响的,但是如果通过 --cpu-period 和 --cpu-quota 来配置,就能起到绝对的控制,做到无论B怎么样,都不会影响到 A。
cpu-period 和 cpu-quota 的单位为微秒(μs)。cpu-period的最小值为1000微秒,最大值为1秒(10^6 μs),默认值为 0.1 秒(100000 μs)。cpu-quota 的值默认为 -1,表示不做控制。
7、--cpuset-cpus, --cpuset-mems
--cpuset-cpus= CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems= MEMs in which to allow execution (0-3, 0,1)
对多核 CPU 的服务器,docker 还可以控制容器运行限定使用哪些 cpu 内核和内存节点,即使用 –cpuset-cpus 和 –cpuset-mems参数。对具有 NUMA 拓扑(具有多 CPU、多内存节点)的服务器尤其有用,可以对需要高性能计算的容器进行性能最优的配置。如果服务器只有一个内存节点,则 –cpuset-mems 的配置基本上不会有明显效果。
使用示例
#表示创建的容器只能用 0、1、2 这三个内核。最终生成的 cgroup 的cpu
docker run -tid --name cpu1 --cpuset-cpus 0-2 centos:8.2.2004
内核配置如下:
# cat /sys/fs/cgroup/cpuset/docker/<容器的完整长ID>/cpuset.cpus
0-2
通过 docker exec <容器ID> taskset -c -p 1(容器内部第一个进程编号一般为1),可以看到容器中进程与 CPU 内核的绑定关系,可以认为达到了绑定 CPU 内核的目的。
8、-d, --detach
-d, --detach=false Run container in background and print container ID
如果在 docker run 后面追加 -d=true 或者 -d,则 containter 将会运行在后台模式(Detached mode)。此时所有 I/O 数据只能通过网络资源或者共享卷组来进行交互。因为 container 不再监听你执行 docker run 的这个终端命令行窗口。但你可以通过执行docker attach 来重新挂载这个container 里面。需要注意的时,如果你选择执行 -d 使 container 进入后台模式,那么将无法配合"–rm"参数。
9、--device=
--device=[] Add a host device to the container
10、--disable-content-trust
--disable-content-trust=true Skip image verification
跳过镜像验证
11、--dns
--dns=[] Set custom DNS servers
自定义DNS:
[root@localhost opt]# docker run -it --dns=8.8.8.8 --rm --hostname=dnstest centos:8.2.2004 /bin/bash
[root@dnstest /]# cat /etc/resolv.conf
search localdomain
nameserver 8.8.8.8
12、--dns-opt
--dns-opt=[] Set DNS options
13、--dns-search
--dns-search=[] Set custom DNS search domains
14、-e, --env
-e, --env=[] Set environment variables
自这义环境变量
15、--entrypoint
--entrypoint= Overwrite the default ENTRYPOINT of the image
字面意思是进入点,而它的功能也恰如其意。
An ENTRYPOINT allows you to configure a container that will run as an executable. 它可以让你的容器功能表现得像一个可执行程序一样。
15.1、示例一
使用下面的 ENTRYPOINT 构造镜像
ENTRYPOINT ["/bin/echo"]
那么 docker build 出来的镜像以后的容器功能就像一个 /bin/echo 程序: 比如我 build 出来的镜像名称叫 imageecho:v1,那么我可以这样用它:
docker run -it imageecho:v1 “this is a test”
这里就会输出”this is a test”这串字符,而这个 imageecho 镜像对应的容器表现出来的功能就像一个 echo 程序一样。 你添加的参数“this is a test”会添加到 ENTRYPOINT 后面,就成了这样 /bin/echo “this is a test” 。
15.2、示例二
ENTRYPOINT ["/bin/cat"]
构造出来的镜像你可以这样运行(假设名为imagecat:v1):
docker run -it imagecat:v1 /etc/hosts
这样相当: /bin/cat /etc/hosts 这个命令的作用。运行之后就输出 /etc/hosts里的内容。
16、--env-file
--env-file=[] Read in a file of environment variables
读取设置环境变量的文件
17、--expose
--expose=[] Expose a port or a range of ports
告诉 Docker 服务端容器暴露的端口号,供互联系统使用
docker run -it --expose=22 --rm centos:7.9.2009 /bin/bash
18、--group-add
--group-add=[] Add additional groups to join
19、-h, --hostname
-h, --hostname= Container host name
设置容器主机名
[root@localhost opt]# docker run -it --hostname=web --rm centos:7.9.2009 /bin/bash
[root@web /]# 进入容器后
20、-i, --interactive=false
-i, --interactive=false Keep STDIN open even if not attached
保持标准输入,常同 -t 一起使用来申请一个控制台进行数据交互
21、--ipc
--ipc= IPC namespace to use
IPC(POSIX/SysV IPC) 命名空间提供了相互隔离的命名共享内存,信号灯变量和消息队列。 共享内存可以提高进程数据交互速度。共享内存一般用在 database 和高性能应用(C/OpenMPI, C++/using boost libraries)上或者金融服务上。如果需要容器里面部署上述类型的应用,那么就应该在多个容器直接采取共享内存了。
22、--kernel-memory
--kernel-memory= Kernel memory limit
内核内存,不会被交换到 swap 上。一般情况下,不建议修改,可以直接参考 docker 的官方文档
23、-l, --label
-l, --label=[] Set meta data on a container
--label-file=[] Read in a line delimited file of labels
24、--link
--link=[] Add link to another container
用于连接两个容器
24.1、示例:连接两个容器
启动容器1:web1
docker run -it --name web1 --hostname=web1 -p 22 -p 80 -d centos:8.2.2004
启动容器2:web2连接到web1,并命名为webs
docker run -it --name web2 --hostname=web2 --link=web1:webs -p 22 -p 80 -d centos:8.2.2004
# 进入容器web2
[root@localhost opt]# docker exec -it web2 bash
[root@web2 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 webs web1
172.17.0.3 web2
[root@web2 /]# exit
[root@localhost opt]# docker exec -it web2 bash
[root@web2 /]# ping -c3 web1 # web2能与web1通过域名通信
PING webs (172.17.0.2) 56(84) bytes of data.
64 bytes from webs (172.17.0.2): icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from webs (172.17.0.2): icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from webs (172.17.0.2): icmp_seq=3 ttl=64 time=0.042 ms
--- webs ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 64ms
rtt min/avg/max/mdev = 0.040/0.059/0.097/0.027 ms
25、--log-driver
--log-driver= Logging driver for container
--log-opt=[] Log driver options
Docker 增加了对 json-file 型(默认)log driver 的 rotate 功能,我们可通过 max-size 和 max-file 两个 –log-opt 来配置。
比如:我们启动一个 nginx 容器,采用 json-file日志引擎,每个 log 文件限制最大为 1 k,轮转的日志个数为 5 个:
docker run --name logtest --log-driver=json-file --log-opt max-size=1k --log-opt max-file=5 -p 80:80 -d nginx:1.21.0
有了 rotate,我们就不必担心某个 container 的日志暴涨而将同 host 的其他 container 拖死了。
26、--mac-address
--mac-address= Container MAC address
(e.g. 92:d0:c6:0a:29:33)
设置容器的 mac 地址
27、-m, --memory
-m, --memory= Memory limit
设置容器使用的最大内存上限。默认单位为 byte,可以使用 K、G、M 等带单位的字符串。 默认情况下,容器可以使用主机上的所有空闲内存。
设置容器的内存上限,参考命令如下所示:
docker run -tid --name mem1 --memory 128m centos:8.2.2004 /bin/bash
默认情况下,除了 --memory 指定的内存大小以外,docker 还为容器分配了同样大小的 swap 分区,也就是说,上面的命令创建出的容器实际上最多可以使用 256 MB内存,而不是 128 MB内存。如果需要自定义 swap 分区大小,则可以通过联合使用 --memory–swap 参数来实现控制。 对上面的命令创建的容器,可以查看到在 cgroups 的配置文件中,查看到容器的内存大小为 128 MB (128×1024×1024=134217728B),内存和 swap 加起来大小为 256MB (256×1024×1024=268435456B)。
#cat /sys/fs/cgroup/memory/docker/<容器的完整ID>/memory.limit_in_bytes
[root@localhost opt]# cat /sys/fs/cgroup/memory/docker/68c95c81b56163298f44c11cd9ba7b13a5f7055302258c5465323bc32979d651/memory.limit_in_bytes
134217728
#cat /sys/fs/cgroup/memory/docker/<容器的完整ID>/memory.memsw.limit_in_bytes
[root@localhost opt]# cat /sys/fs/cgroup/memory/docker/68c95c81b56163298f44c11cd9ba7b13a5f7055302258c5465323bc32979d651/memory.memsw.limit_in_bytes
268435456
注意:
执行上述命令时,命令行可能会输出下面的警告: WARNING: Your kernel does not support swap limit capabilities, memory limited without swap. 这是因为主机上默认不启用 cgroup 来控制 swap 分区,可以参考 docker 官方的相应文档,修改 grub 启动参数。
28、--memory-reservation
--memory-reservation= Memory soft limit
启用弹性的内存共享,当宿主机资源充足时,允许容器尽量多地使用内存,当检测到内存竞争或者低内存时,强制将容器的内存降低到 memory-reservation 所指定的内存大小。按照官方说法,不设置此选项时,有可能出现某些容器长时间占用大量内存,导致性能上的损失。
29、--memory-swap
--memory-swap= Total memory (memory + swap), '-1' to disable swap
等于内存和 swap 分区大小的总和,设置为 -1 时,表示 swap 分区的大小是无限的。默认单位为 byte,可以使用 K、G、M 等带单位的字符串。如果 –memory-swap 的设置值小于 –memory 的值,则使用默认值,为 –memory-swap 值的两倍
30、--memory-swappiness
--memory-swappiness=-1 Tuning container memory swappiness (0 to 100)
控制进程将物理内存交换到 swap 分区的倾向,默认系数为 60。系数越小,就越倾向于使用物理内存。值范围为 0-100。当值为100 时,表示尽量使用 swap 分区;当值为 0 时,表示禁用容器 swap 功能(这点不同于宿主机,宿主机 swappiness 设置为 0 也不保证 swap 不会被使用
31、--name
--name= Assign a name to the container
为容器指定一个名字
docker run -it --name=nginx-test nginx:1.21.0 /bin/bash
32、--net
--net=default Set the Network for the container
以下是网络设置中常用的参数:
-
none 关闭 container 内的网络连接: 将网络模式设置为 none 时,这个 container 将不允许访问任何外部 router。这个 container 内部只会有一个 loopback 接口,而且不存在任何可以访问外部网络的 outer。
-
bridge 通过veth接口来连接 contianer 默认选项: Docker 默认是将 container 设置为 bridge 模式。此时在 host 上面讲存在一个 docker0 的网络接口,同时会针对 container 创建一对 veth 接口。其中一个 veth 接口是在 host 充当网卡桥接作用,另外一个 veth 接口存在于 container 的命名空间中,并且指向 container 的 loopback。Docker 会自动给这个 container 分配一个IP,并且将 container 内的数据通过桥接转发到外部。
-
host 允许 container 使用 host 的网络堆栈信息: 当网络模式设置为 host 时,这个 container 将完全共享 host 的网络堆栈。host 所有的网络接口将完全对 container 开放。container 的主机名也会存在于 host 的 hostname 中。这时,container 所有对外暴露的 port 和对其它 container 的 link,将完全失效。
-
Container: 当网络模式设置为 Container 时,这个 container 将完全复用另外一个 container 的网络堆栈。同时使用时这个 container 的名称必须要符合下面的格式:–net container:. 比如当前有一个绑定了本地地址 localhost 的 redis container。如果另外一个 container 需要复用这个网络堆栈,则需要如下操作:
docker run -d --name redis redis:6.2.1 --bind 127.0.0.1
# use the redis container's network stack to access localhost
docker run --rm -ti --net container:redis redis:6.2.1 redis-cli -h 127.0.0.1
33、-P, --publish-all
-P, --publish-all=false Publish all exposed ports to random ports
对外映射所有端口
34、-p, --publish
-p, --publish=[] Publish a container's port(s) to the host
对外映射指定端口,如不指定映射后的端口将随机指定
docker run -it -p 2222:22 -p 80:80 -d centos:8.2.2004
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5a5d0e235b5 centos:8.2.2004 "/bin/bash" 27 seconds ago Up 26 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:2222->22/tcp, :::2222->22/tcp hardcore_chatterjee
使用 docker run 来启动我们创建的容器。-d让容器以后台方式运行。使用多个-p来映射多个端口,将容器的22端口映射为本地的22,80映射为80。
35、--pid
--pid= PID namespace to use
设置容器的 PID 模式。两种:
host: use the host's PID namespace inside the container.
Note: the host mode gives the container full access to local PID and is therefore considered insecure.
36、--privileged
--privileged=false Give extended privileges to this container
默认情况下container是不能访问任何其他设备的。但是通过"privileged",container就拥有了访问任何其他设备的权限。 当操作者执行docker run --privileged时,Docker将拥有访问host所有设备的权限
docker run -it --rm --privileged centos:8.2.2004 /bin/bash
37、--read-only
--read-only=false Mount the container's root filesystem as read only
启用后,容器的文件系统将为只读
[root@localhost opt]# docker run -it --hostname=readonlytest --rm --read-only centos:8.2.2004 /bin/bash
[root@readonlytest /]# touch a.txt
touch: cannot touch 'a.txt': Read-only file system
38 --restart
--restart string Restart policy to apply when a container exits (default "no")
- no,默认策略,在容器退出时不重启容器
- on-failure,在容器非正常退出时(退出状态非 0),才会重启容器
- on-failure:3,在容器非正常退出时重启容器,最多重启 3 次
- always,在容器退出时总是重启容器,当操作系统或 docker 服务重启时,该容器总能随系统启动
- unless-stopped,在容器退出时总是重启容器,但是不考虑在 Docker 守护进程启动时就已经停止了的容器
docker run -it --name=restart-test --hostname=restart --restart=always centos:8.2.2004 /bin/bash
39、--rm
--rm=false Automatically remove the container when it exits
当容器退出时如果容器关闭,则自动清除所有该容器的信息
[root@localhost opt]# docker run -it --name mycentos --hostname=testrm --rm centos:8.2.2004 /bin/bash
[root@testrm /]# exit
[root@localhost opt]# docker ps -a # 容器退出时关闭,则自动删除容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40、-t, --tty
-t, --tty=false Allocate a pseudo-TTY
分配一个模拟终端,常和 -i 一块使用
41、-v, --volume
-v, --volume=[] Bind mount a volume
可以使用带有 -v 参数的 docker run 命令给容器添加一个数据卷.
- 添加数据卷/data,会自动创建目录
docker run -it --name nginx-web --hostname=nginx-web -v /data nginx:1.21.0 /bin/bash
[root@localhost opt]# docker run -it --name nginx-web --hostname=nginx-web -v /data nginx:1.21.0 /bin/bash
root@nginx-web:/# cd /data
root@nginx-web:/data# df -lh
Filesystem Size Used Avail Use% Mounted on
overlay 38G 5.6G 33G 15% /
tmpfs 64M 0 64M 0% /dev
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
shm 64M 0 64M 0% /dev/shm
/dev/mapper/centos-root 38G 5.6G 33G 15% /data # 自动创建/data目录
tmpfs 2.0G 0 2.0G 0% /proc/asound
tmpfs 2.0G 0 2.0G 0% /proc/acpi
tmpfs 2.0G 0 2.0G 0% /proc/scsi
tmpfs 2.0G 0 2.0G 0% /sys/firmware
- 将宿主机的目录添加到容器 将宿主机的 /data_web 加载为容器 /data 目录【若宿主机或者容器不存在相应的目录,则自动创建】
# 挂载一个目录
docker run -it --name testvolume --hostname=testvolume -v /data_web:/data centos:8.2.2004 /bin/bash