docker run

命令

docker run

描述

docker进程运行与独立的容器中,而容器是宿主机的一个进程。宿主机可以使本地的也可以是远程的。每当执行docker run之后,容器进程式独立运行的,他有自己的文件系统、网络以及与主机隔离的进程。

帮助

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
      --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
      --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
      --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 string Connect a container to a network (default "default")
      --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
      --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

https://www.jianshu.com/p/f83f809d7b6f https://docs.docker.com/v18.09/engine/reference/commandline/stop/

## run用于指定镜像创建容器

$ docker run [选项] <镜像名称, id> [命令] [参数]


# 选项
-d, --detach=false 指定容器运行于前台还是后台,默认为false。
-i, --interactive=false 打开标准输入用于控制台交互
-t, --tty=false 分配tty设备用来支持终端登录,默认为false。
-u, --user="" 指定容器的用户
-a, --attach=[] 登录容器,必须是以docker run -d启动的容器。
-w, --workdir="" 设置容器的工作目录
-c, --cpu-shares=0 设置容器CPU权重,在CPU共享场景下使用。
-e, --env=[] 设置环境变量,容器中可使用该环境变量。
-m, --memory="" 设置容器的内存上限
-p, --public=[] 设置容器暴露的端口
-h, --hostname="" 设置容器的主机名
-v, --volume=[] 设置容器挂载的存储卷,也就是挂载到容器的某个目录。
--volumn-from=[] 给容器挂载其他容器上的卷,也就是挂载到容器的某个目录。
--cap-add=[] 添加权限
--cap-drop=[] 删除权限
--cidfile="" 运行容器后在指定文件中写入容器PID值,这是典型的监控系统的用法。
--cpuset="" 设置容器可以使用那些CPU,此参数用来设置容器独占CPU。
--device=[] 添加主机设备给容器,相当于设备直通。
--dns=[] 设置容器的DNS服务器
--dns-search=[] 设置容器的DNS搜索域名,写入到容器的/etc/resolv.conf文件。
--env-file=[] 设置环境变量文件,文件格式为每行一个环境变量。
--expose=[] 设置容器暴露的端口,即修改镜像的暴露端口。
--link=[] 设置容器之间的关联关系,使用其他容器的IP、env等信息。
--lxc-conf=[] 设置容器的配置文件,只有在指定--exe-driver=lxc时使用。
--name="" 设置容器的名称,可通过名字进行容器管理,links特性需要使用名字。
--net="bridge" 容器网络设置
--privileged=false 设置容器是否为特权容器,特权容器拥有所有的capabilities。
--restart="no" 设置让其停止后的重启策略
--rm=false 设置容器停止后自动删除容器,不支持以docker run -d启动的容器。
--sig-proxy=true 设置由代理接收并处理信号,但SIGCHLD、SIGSTOP、SIGKILL不能被代理。

# 命令
-d, --detach Detach模式,默认为守护进程模式,即容器以后台方式运行。
--rm=false 若容器内的进程终止则自动删除容器,禁止和-d选项一起使用。
--sig-proxy=true 将所有信号传递给进程,非TTY模式也一样,但不能传递SIGCHLD、SIGKILL、SIGSTOP信号。

# 运行一个在后台执行的容器,同时使用控制台管理。
$ docker run -i -t -d ubuntu:latest

# 运行一个带命令且在后台不断执行的容器,不直接展示容器内部信息。
$ docker run -d ubuntu:latest ping www.docker.com

# 运行一个在后台不断执行的容器,同时带有命令,程序被终止后还能重启继续跑,可用控制台管理。
$ docker run -d --restart=always ubuntu:latest ping www.docker.com

# 为容器指定一个名字
$ docker run -d --name=ubuntu_server ubuntu:latest

# 容器暴露80端口并指定宿主机81端口与其通信(宿主机端口:容器端口)
$ docker run -d --name=ubuntu -p 81:80 ubuntu:latest

# 指定容器内目录与宿主机目录共享(宿主机目录:容器目录)
$ docker run -d --name=ubuntu_server -v /home/www:/var/www ubuntu:latest

# 设置宿主机与docker的共享目录
$ docker run -d -i -t -p 80:80 -v /share/swoft:/var/www/swoft --name swoft swoft/swoft /bin/bash
使用docker run启动容器,docker在后台的标准操作流程:

检查本地是否存在指定的镜像,若不存在则从公有仓库下载。
使用镜像创建并启动容器
分配一个文件系统,并在只读的镜像层外面挂载一层可读可写层。
从宿主机配置的网桥接口中桥接一个虚拟机接口到容器中去
从地址池分配一个IP地址给容器
执行用户指定的应用程序
执行完毕后容器被终止

docker run 常见参数


-i, –interactive=false

容器如果没有被attach,就保持开放到标准输入
Keep STDIN open even if not attached

-t, –tty=false

分配一个虚拟TTY终端 Allocate a pseudo-TTY

-h, –hostname=

内部容器的主机名,常见(localhost) Container host name

-d, –detach=false

在后台运行容器,返回新建容器ID Run container in background and print container ID

–name=

给容器分配一个名字,以便外部管理 Assign a name to the container

–net=default

设置容器网络 none: container: bridge: Set the Network for the container

-m, –memory=

内存限制,单位k,m,g,t Memory limit

–dns=[]

设置DNS服务器 Set custom DNS servers

–add-host=[]

添加一条DNS,主机到IP映射 Add a custom host-to-IP mapping (host:ip)

–expose=[]

开放一个端口或一个范围端口 Expose a port or a range of ports

–rm=false

当容器退出时自动删除该容器 Automatically remove the container when it exits

## -w, –workdir=

Working directory inside the container

docker run -itd --name="vinny" -h="vm-localhost" --expose=22 --dns="8.8.8.8" --net="bridge" -m="20M" host /bin/bash

##常见错误

Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.

强烈鼓励在生产环境中使用使用回环地址设备。两者选择其一,使用`--storage-opt dm.thinpooldev`或者使用`--storage-opt dm.no_warn_on_loop_devices=true` 终止这个告警。

终止这个告警方法

修改配置文件然后重启docker

[root@localhost dockerfile]# cat /etc/sysconfig/docker-storage
# This file may be automatically generated by an installation program.
# Please DO NOT edit this file directly. Instead edit
# /etc/sysconfig/docker-storage-setup and/or refer to
# "man docker-storage-setup".

# By default, Docker uses a loopback-mounted sparse file in
# /var/lib/docker.  The loopback makes it slower, and there are some
# restrictive defaults, such as 100GB max storage.

DOCKER_STORAGE_OPTIONS= --storage-opt dm.no_warn_on_loop_devices=true