docker常用命令-dokcer create

docker create

语义
  • 创建一个新容器,但是不启动该容器
语法

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

参数说明
  • –add-host list : 在容器内hosts文件中添加主机名到ip的映射关系,映射模式为 host:IP
    docker create --add-host ${host}:${IP} ${image}

  • -a : --attach list 是否绑定到标准输入,输出和错误
    docker create -a stdout ${image}

  • –blkio-weight uinit16 : 容器读写块设备的IO性能权重,默认为0,取值范围10~1000
    docker create --blkio-weight 100 ${image}

  • –blkio-weight-device weighted-device : 指定设备的IO性能权重
    docker create --blkio-weight-device ${deviceName}:${weight} ${image}

  • –cap-add list : 增加容器的linux指定安全能力
    docker create --cap-add ${cap} ${image}

  • –cap-drop list : 移除容器的liunux指定安全能力
    docker create --cap-drop ${cap} ${image}

  • –cgroup-parent string : 配置容器的控制组,继承该控制组的资源限制模式
    docker create --cgroup-parent ${cgroup} ${image}

  • –cidfile string : 将该容器的pid输出到某一文件中
    docker create --cidfile ${path} ${image}

  • –cpu-count int : 设置容器cpu数量仅限Windows版本
    docker create --cpu-count ${count} ${image}

  • –cpu-percent int : 设置容器CPU使用百分比仅限Windows版本
    docker create --cpu-percent ${percent} ${image}

  • –cpu-period int : 与参数–cpu-quota配合使用,用于设定cpu从新分配资源的时间周期,时间周期结束后,会对cpu进行重新分配,主要作用用Linux的CFS(公平调度算法)模式
    docker create --cpu-percent ${percent} ${image}

  • –cpu-quota int : 与参数–cpu-period配合使用,用于设定该容器在资源分配周期内占用cpu的时间,若容器设定–cpu-quota=1000000 --cpu-period=500000,则该容器在这个时间周期内权重为50%,这两个参数主要是提升宿主机内某一容器的权重比,可以用来解决宿主机内若干容器的资源抢占导致重要容器cpu性能不足的场景。该模式应用于Linux 的CFS(公平调度算法)模式
    docker create --cpu-quota ${count} ${image}

  • –cpu-rt-period int : 实时调度策略的时间周期,该处时间的单位为毫秒
    docker create --cpu-rt-period ${count} ${image}

  • –cpu-rt-runtime int : 实时调度策略中的每个时间周期中的最长调用时间,该处时间的单位为毫秒
    docker create --cpu-rt-runtime ${count} ${image}

  • -c : --cpu-shares int : CPU资源的使用权重,是相对权重,设置为一个正整数,代表相对CPU资源占比,默认情况下所有容器的权重相同,只有当两个或多个容器去抢占同一个CPU计算资源的时候,–cpu-shares 才会生效
    docker create -c ${count} ${image}

  • –cpus decimal : 容器的CPU占宿主机的CPU的比例,默认为0.000
    docker create --cpus 0.524 ${image}

  • –cpuset-cpus string : 指定允许使用的CPU序号,从0开始,默认使用所有的CPU
    docker create --cpuset-cpus=0-2 ${image}

  • –cpuset-mems string : 指定允许使用的内存节点
    docker create --cpuset-mems=0,2

  • –credentialspec string : 托管服务账户的凭证(仅对Windows系统有效)
    docker create --credentialspec ${account} ${image}

  • –device list : 映射设备到容器中,默认[]
    docker create --device /dev:/tmp ${image}

  • –device-read-bps throttled-device : 限制从某个设备或多个设备进行读取的每秒的字节速率,单位可以是KB、MB、GB
    docker create --device-read-bps /dev/sda:10M ${image}

  • –device-read-iops throttled-device : 限制从某个设备或多个设备进行IO读取速率
    docker create --device-read-iops /dev/sda:200 ${image}

  • –device-write-bps throttled-device : 限制从某个设备或多个设备进行写入的每秒的字节速率,单位可以是KB、MB、GB
    docker create --device-write-bps /dev/sda:20K ${image}

  • –device-write-iops throttled-device : 限制从某个设备或多个设备进行IO写入速率
    docker create --device-write-iops /dev/sda:40 ${image}

  • –disable-content-trust : 跳过镜像认证,默认为 true
    docker create --disable-content-trust:false ${image}

  • –dns list : 设定容器的DNS地址,在容器的 /etc/resolv.conf 文件中可查看。注意:如果不设置, Docker 会默认用主机上的 /etc/resolv.conf 来配置容器
    docker create --dns 8.8.8.8 ${image}

  • –dns-option list : 设置容器中的dns配置,配置的文件在容器中的/etc/resolv.conf文件下可以查看
    docker create --dns-option test-option ${image}

  • –dns-search list : 设定容器的搜索域,当设定搜索域为 test.com 时,在搜索一个名为 host 的主机时,DNS 不仅搜索host,还会搜索 host.test.com 。 注意:如果不设置, Docker 会默认用主机上的 /etc/resolv.conf 来配置容器
    docker create --dns-search test.com ${image}

  • –entrypoint string : 重写image默认的ENTRYPOINT

  • -e : --env list : 设置容器实例的环境变量
    docker create -e path=/tmp/test.txt ${image}

  • –env-file list : 读入一个配置文件
    docker create --env-file /usr/conf.env ${image}

  • –expose list : 开放一个或一组端口
    docker create --expose 80 --expose 9001 ${image}

  • –group-add list : 指定加入容器的用户组

  • –health-cmd string : 运行安全检查的命令
    docker create --health-cmd check ${image}

  • –health-interval duration : 运行安全检查间隔时间,单位有ns(纳秒)|us(微秒)|ms(毫秒)|s(秒)|m(分钟)|h(小时),默认为0秒
    docker create --health-interval 10m ${image}

  • –health-retries int : 运行安全检查后的失败重试次数
    docker create --health-retries 5 ${image}

  • –health-timeout duration : 允许单次运行检查的最大时间间隔,单位可以是 ns(纳秒)us(微秒)ms(毫秒) s(秒) m(分) h(小时)
    docker create --health-timeout 100ns ${image}

  • -h : --hostname string : 设置容器的hostname,通过 cat /etc/hostname 命令可以查看该命令
    docker create --hostname haha ${image}

  • –init : 在容器中运行一个初始化操作在信号量和起始进程之前
    docker create --init ${image}

  • –init-path string : 指向进行docker初始化的二进制文件
    docker create --init-path /bin/init ${image}

  • -i : --interactive : 保持一个输入流打开状态即使没有任何依赖
    docker create -i ${image}

  • –io-maxbandwidth string : 系统驱动允许的最大IO带宽限制(只限Windows系统)

  • –io-maxiops uint : 系统驱动允许的最大IO存储限制(只限Windows系统)

  • –ip string : 指定容器IPv4的地址
    docker create --ip 192.168.30.10 ${image}

  • –ip6 string : 指定容器IPv6的地址
    docker create --ip6 2001:db8::33 ${image}

  • –ipc string : 使用的IPC命名空间

  • –isolation string : 配置容器隔离机制

  • –kernel-memory string : 容器内核内存限制,即容器的系统内核最多可以使用多少内存
    docker create --kernel-memory 10m ${image}

  • -l : --label list : 为生成的镜像设置meta data
    docker create -l test=myLabel ${image}

  • –label-file list : 通过文件来设置容器的label,以行作为分隔
    docker create --label-file /tmp/label.txt
#label.txt
tagOne=test
tagTwo=labelAge
tagThree=t

  • –link list : 添加一个链接到其他容器

  • –link-local-ip list : 连接宿主机IP

  • –log-driver string : 容器日志驱动地址

  • –log-opt list : 日志渠道相关配置信息

  • –mac-address string : 设置容器MAC地址

  • -m : --memory string : 容器内存使用限制,数字需要使用整数,对应的单位是b, k, m, g中的一个。最小取值是4M,在默认情况下,容器可以占用无限量的内存,直至主机内存资源耗尽。
    注意,在实际容器使用场景中,如果不对容器使用内存量加以限制的话,可能导致一个容器会耗尽整个主机内存,从而导致系统不稳定。所以在使用容器时务必对容器内存加以限制
    docker create -m 512M ${image}

  • –memory-reservation string : 内存软限制。 数字需要使用正整数,对应的单位是b, k, m, g中的一个。
    通常情况下,容器能够使用的内存量仅仅由-m/–memory选项限定。如果设置了–memory-reservation选项,当内存使用量超过–memory-reservation选项所设定的值时,系统会强制容器执行回收内存的操作,使得容器内存消耗不会长时间超过–memory-reservation的限定值。这个限制并不会阻止进程使用超过限额的内存,只是在系统内存不足时,会回收部分内存,使内存使用量向限定值靠拢。

  • –memory-swap string : 总内存使用限制 (物理内存 + 交换分区,数字需要使用整数,对应的单位是b, k, m, g中的一个。这里是物理内存+交换分区的总和,并非仅是交换分区。

  • –memory-swappiness int : 调节容器内存使用交换分区的选项,取值为0和100之间的整数(含0和100)。0表示容器不使用交换分区,100表示容器尽可能多的使用交换分区。

  • –name string : 容器名称
    docker create --name n-2 nginx:latest

  • –network string : 设置连接容器的网络模式

  • –network-alias list : 添加一个网络域别名为该容器

  • –no-healthcheck : 禁用容器的健康检查

  • –oom-kill-disable : 容器内存耗尽是是否杀掉容器进程,默认杀掉容器进程

  • –oom-score-adj int : 调优容器的OOM配置

  • –pid string : 使用的PID命名空间

  • –pids-limit int : 调优容器的pid限制

  • –privileged : 赋予该容器可拓展权限,通过privileged ,container就拥有了访问任何其他设备的权限
    docker create --privileged ${image}

  • -p : --publish list : 指定容器与宿主机的端口映射
    docker create -p 80:30001 ${image}

  • -P : --publish-all : 映射容器所有的端口
    docker create -P

  • –read-only : 挂载到容器的root文件系统设置为只读

  • –restart string : 当容器退出是是否自动重启,共三个可选值:no、no-failure、always;no为默认值,表示容器退出时,docker不自动重启容器;on-failure表示,若容器的退出状态非0,则docker自动重启容器,还可以指定重启次数,若超过指定次数未能启动容器则放弃;always表示,只要容器退出,则docker将自动重启容器
    docker create --restart on-failure:4 ${image}

  • –rm : 当容器存在时,自动移除容器

  • –runtime string : 容器使用当前的运行时

  • –security-opt list : 配置安全配置信息

  • –shm-size string : 配置/dev/shm的大小,默认64M

  • –stop-signal string : 配置容器停止信号量,默认是SIGTERM

  • –stop-timeout int : 停止一个容器的最长超时时间,单位为秒

  • –storage-opt list : 容器存储渠道配置信息设置

  • –sysctl map : 系统控制文件配置参数

  • –tmpfs list : 挂载一个tmpfs文件目录

  • -t : --tty : 分配一个伪终端
    docker create -t ${image}

  • –ulimit ulimit : 资源控制配置信息

  • -u : --user string 通过某个特定用户的用户名或者用户ID在容器中运行
    docker create -u #{username}/#{userId} #{image}

  • –userns string : 启动userns-remap时配置用户命名空间的模式

  • –uts string : 容器的UTS命名空间

  • -v, --volume list : 在容器中挂载一个卷
    docker create -v /tmp:/tmp #{image}

  • –volume-driver string : 配置特定驱动来运行卷
    设置该驱动需要下载相应的插件,待volume命令中再做进一步运行

  • –volumes-from list : 挂载其他容器挂载的目录到当前的容器下,可同时挂载多个容器的多个目录,不同容器的相同目录同时挂载会发生覆盖
    docker create --volumes-from #{containerName}/#{containerId} #{image}

  • -w : --workdir string 容器中的工作目录
    docker create -w #{path} #{image}

实例
  • 创建容器n-1并且指定容器中的进程ID写入到宿主机/tmp/10.cid文件中
docker create --cidfile /tmp/10.cid --name n-1 nginx:latest 

  • 创建容器n-1并且添加www.jiguang.cn到127.0.0.1的映射
    docker create --add-host www.jiguang.cn:127.0.0.1 --name n-1 nginx:latest

  • 在容器n-1中挂载宿主机的/tmp到容器的/tmp目录下

# 创建nginx容器,生成容器ID,挂载宿主机/tmp目录到容器中的/tmp目录下
docker create -v /tmp:/tmp --name n-1 nginx:latest

# 启动创建好的nginx容器
docker start n-1

# 查看宿主机的tmp目录下的内容
ls /tmp

# 进入容器内bash虚拟终端
docker exec -it n-1 bash

# 查看容器中的/tmp目录下的内容与宿主机是否相同
ls /tmp

# 在容器的/tmp目录下创建1.txt文件
touch 1.txt

# 退出容器
exit

# 查看宿主机中的/tmp目录中是否存在1.txt文件
ls /tmp
  • 在容器n-3中挂载容器n-1挂载的卷和容器n-2挂载的卷
# 创建容器n-1,挂载本地的/tmp目录到容器的/tmp目录下
docker create -v /tmp:/tmp --name n-1 nginx:latest

# 创建容器n-2,挂载 /my/test卷到容器中
docker create -v /my/test --name n-2 nginx:latest

# 启动容器 n-1和 n-2
docker start n-1 n-2

# 进入n-1容器的虚拟终端中
docker exec -it n-1 bash

# 在 n-1 容器中的 /tmp 目录下创建10.txt文件
touch 10.txt

# 退出n-1容器
exit

# 查看本地/tmp目录下是否存在10.txt文件

# 进入n-2容器的虚拟终端中
docker exec -it n-2 bash

# 在 n-2 容器中的 /my/test 目录下创建1.txt文件
touch /my/test/1.txt

# 退出n-2容器
exit

# 创建容器n-3,共享n-1和n-2挂载的目录
docker create --volumes-from n-1 --volumes-from n-2 --name n-3 nginx:latest

# 启动容器 n-3
docker start n-3

# 进入n-3容器虚拟终端
docker exec -it n-3 bash

# 查看容器 n-3 中目录挂载情况,如果不出意外的话, 容器n-3中的/tmp 目录 和容器n-2中的/my/test目录应该已经都挂载到了容器n-3中
ls /tmp/ /my/test/
  • 设置一个容器的工作目录为/app
# 创建容器,生成容器ID,设置工作目录为/app
docker create -w /app nginx:latest
# 通过容器ID启动容器
docker start #{containerID}
# 进入容器查看工作目录
docker exec -it #{containerId} bash
  • 添加宿主机的/dev文件夹下的设备到n-1容器中的/tmp文件夹下
docker create --device /dev:/tmp --name n-1 nginx:latest
  • 在n-1容器中添加添加8.8.8.8这个域名解析地址
docker create --dns 8.8.8.8 --name n-1 nginx:latest
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有梦想的攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值