docker run

https://docs.docker.com/engine/reference/commandline/run/

  • docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

docker run -i -t -d ubuntu:15.10 /bin/bash

  • 各个参数解析:

-i: 以交互模式运行容器,通常与 -t 同时使用
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用
-d: 后台运行容器,并返回容器ID
-e: 设置环境变量
-h: 指定容器的hostname
-m 设置容器使用内存最大值。
-P 随机端口映射,容器内部端口随机映射到主机的端口
-p 指定端口映射,格式为:主机(宿主)端口:容器端口
-v 绑定一个卷

—name:指定容器名称
—dns 指定容器使用的DNS服务器,默认和宿主一致
—dns-search 指定容器DNS搜索域名,默认和宿主一致
—cpuset 绑定容器到指定CPU运行
—env-file 从指定文件读入环境变量
—link 添加链接到另一个容器
—net 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型
—volume 绑定一个卷

  • Options:


     --add-host list                  Add a custom host-to-IP mapping
                                       (host:ip)
    # CPU相关可选参数
     -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
  …… ……
  # 内存限制命令参数,使用格式 --key=value 如:--memory=10g --memory-swap=16G 
  -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

常用方法

docker run -itd --name name1 ubuntu /bin/bash

docker run --name ngx -d nginx:1.21.5-alpine
docker run --name ngx -p 80:80 -p 8080:80 -v e:/home:/data -d nginx:1.21.5-alpine
--restart always