1、创建容器常用选项

[root@linux-node1 ~]# docker container diff 47cb511b8faf        #查看当前容器与镜像有哪些变化(diff)
C /root
A /root/.bash_history
C /run
A /run/nginx.pid
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp


[root@linux-node1 ~]# docker container run --help
[root@linux-node1 ~]# docker run --help
选项                      描述
-i, –interactive         交互式
-t, –tty                分配一个伪终端
-d, –detach                运行容器到后台
-e, –env                设置环境变量
-p, –publish list        发布容器端口到主机
-P, –publish-all        发布容器所有EXPOSE的端口到宿主机随机端口
–name string            指定容器名称
-h, –hostname            设置容器主机名
–ip string                指定容器IP,只能用于自定义网络
–network                连接容器到一个网络
–mount mount            将文件系统附加到容器
-v, –volume list        绑定挂载一个卷
–restart string            容器退出时重启策略,默认no,可选值:[always|on-failure]

1.1 启动一个centos容器以后台运行的方式

[root@linux-node1 ~]# docker run -itd centos
4b40859bff9f76b1aca8f35579d68e79c33cef71a771e0ce5bea66768077a71a

1.2 查看最新启动的容器

[root@linux-node1 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4b40859bff9f        centos              "/bin/bash"         8 seconds ago       Up 7 seconds                            vibrant_nash

1.3 查看centos运行进程

[root@linux-node1 ~]# docker top 4b40859bff9f
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                25840               25823               0                   11:27               pts/0               00:00:00            /bin/bash

1.4 进入centos容器

[root@linux-node1 ~]# docker exec -it 4b40859bff9f bash        
[root@4b40859bff9f /]#

1.5 返回宿主机

[root@4b40859bff9f /]# exit
exit

1.6 centos依然运行

[root@linux-node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4b40859bff9f        centos              "/bin/bash"              10 minutes ago      Up 10 minutes                           vibrant_nash
47cb511b8faf        nginx               "nginx -g 'daemon of…"   4 days ago          Up 4 days           80/tcp              friendly_jennings

1.7 创建一个容器后台运行,名字为web,设置环境变量‘test=123456’ 转发容器80端口至宿主机的88端口

[root@linux-node1 ~]# docker container run -d --name web -e test=123456 -p 88:80 -h web nginx
5a0b1adc0e83842c01d4c5b39dba6a01ca6a169b09215624e9bf7ac6f9ec822e
[root@linux-node1 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
5a0b1adc0e83        nginx               "nginx -g 'daemon of…"   23 seconds ago      Up 22 seconds       0.0.0.0:88->80/tcp   web

1.8 查看nginx日志

[root@linux-node1 ~]# docker logs web
192.168.5.57 - - [02/Jan/2019:05:16:10 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
192.168.5.57 - - [02/Jan/2019:05:16:11 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.6.240:88/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
2019/01/02 05:16:11 [error] 8#8: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.5.57, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.6.240:88", referrer: "http://192.168.6.240:88/"

1.9 进入web容器查看主机名机变量

[root@linux-node1 ~]# docker exec -it web bash
root@web:/# 
root@web:/# echo ${test}
123456

1.10 查看docker file里的声明

[root@linux-node1 ~]# docker history nginx:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
02256cfb0e4b        6 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           6 days ago          /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           6 days ago          /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           6 days ago          /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           6 days ago          /bin/sh -c set -x  && apt-get update  && apt…   53.9MB              
<missing>           6 days ago          /bin/sh -c #(nop)  ENV NJS_VERSION=1.15.8.0.…   0B                  
<missing>           6 days ago          /bin/sh -c #(nop)  ENV NGINX_VERSION=1.15.8-…   0B                  
<missing>           6 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           6 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           6 weeks ago         /bin/sh -c #(nop) ADD file:dab9baf938799c515…   55.3MB

1.11 大P创建随机端口

[root@linux-node1 ~]# docker container run -d --name web1 -P -h web1 nginx
cb2afe9bd1aa6cb545af8655ace279a352155068444c19c6ba9714ec4bbd6add

1.12 设置容器退出重启策略

[root@linux-node1 ~]# docker container run -d --name web2 -P -h web2 --restart always nginx  #开机启动
[root@linux-node1 ~]# docker container run -d --name web3 -P -h web3 --restart on-failure:3 nginx  #在容器非正常退出时重启容器,最多重启3次

2、容器资源限制

[root@linux-node1 ~]# docker container run --help|grep memory
      --kernel-memory bytes            Kernel memory limit
  -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)
[root@linux-node1 ~]# docker container run --help|grep cpu
      --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)


选项                           描述
-m,–memory                    容器可以使用的最大内存量
–memory-swap                  允许交换到磁盘的内存量
–memory-swappiness=<0-100>    容器使用SWAP分区交换的百分比(0-100,默认为-1)
–oom-kill-disable            禁用OOM Killer
–cpus                        可以使用的CPU数量
–cpuset-cpus                限制容器使用特定的CPU核心,如(0-3, 0,1)
–cpu-shares                    CPU共享(相对权重)

示例:
内存限额:
允许容器最多使用500M内存和100M的Swap,并禁用OOM Killer:

docker run -d --name nginx03 --memory="500m" --memory-swap=“600m" --oom-kill-disable nginx

[root@linux-node1 ~]# docker run -d --name web04 --memory="500m" --memory-swap="600m" --oom-kill-disable nginx
[root@linux-node1 ~]# docker stats --no-stream web04        #查看设置的内存
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
8a09581b6980        web04               0.00%               2.867MiB / 500MiB   0.57%               648B / 0B           0B / 0B             2

CPU限额:
允许容器最多使用一个半的CPU:

docker run -d --name nginx04 --cpus="1.5" nginx

允许容器最多使用50%的CPU:

docker run -d --name nginx05 --cpus=".5" nginx

[root@linux-node1 ~]# docker run -d –name web05 –cpus=”2” nginx
[root@linux-node1 ~]# docker run -d –name web06 –cpus=”.5” nginx

3、管理容器常用命令

选项              描述
ls                列出容器
inspect            查看一个或多个容器详细信息
exec            在运行容器中执行命令
commit            创建一个新镜像来自一个容器
cp                拷贝文件/文件夹到一个容器
logs            获取一个容器日志
port            列出或指定容器端口映射
top                显示一个容器运行的进程
stats            显示容器资源使用统计
stop/start        停止/启动一个或多个容器
rm                删除一个或多个容器
[root@linux-node1 ~]# docker ps            #查看所有运行的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0132eeb8daf6        nginx               "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        80/tcp 

[root@linux-node1 ~]# docker ps -l        #查看最近启动的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0132eeb8daf6        nginx               "nginx -g 'daemon of…"   6 minutes ago       Up 6 minutes        80/tcp              web06

[root@linux-node1 ~]# docker ps -a        #查看所有容器包括停止的,挂起的,运行的
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                  NAMES
0132eeb8daf6        nginx               "nginx -g 'daemon of…"   7 minutes ago       Up 7 minutes            80/tcp                 web06
8a09581b6980        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes           80/tcp                 web04
13542bd7e816        nginx               "nginx -g 'daemon of…"   About an hour ago   Up About an hour        0.0.0.0:4003->80/tcp   web3

74db95d8685d        nginx               "nginx -g 'daemon of…"   5 days ago          Exited (0) 5 days ago   

[root@linux-node1 ~]# docker container inspect web     #查看web容器详细信息
[root@linux-node1 ~]# docker inspect web            #查看web容器详细信息


[root@linux-node1 ~]# docker exec -it web1 bash        #进入web1容器
root@web1:/# 
root@web1:/# exit                #退出容器
exit
[root@linux-node1 ~]# 

#commit 创建一个新的镜像,并部署

[root@linux-node1 ~]# docker exec -it web1 bash
root@web1:/# touch sunrisenan.log
root@web1:/# ls
bin  boot  dev    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sunrisenan.log  sys  tmp  usr  var
root@web1:/# exit
[root@linux-node1 ~]# docker commit web1 nginx:web01                 #commit 创建一个新的镜像
sha256:e8ac56450a1d251e47409456567183ac536c32f630c93ef36684de59d6b6399d
[root@linux-node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web01               e8ac56450a1d        16 seconds ago      109MB
nginx               latest              02256cfb0e4b        6 days ago          109MB
centos              latest              1e1148e4cc2c        3 weeks ago         202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB
[root@linux-node1 ~]# docker run -d --name web01-1 nginx:web01        #用nginx:web01镜像创建一个容器
5f31f0d6e509680a79d3579cff267e4e3133de4bdba9cb3ec301926b6af4ef78
[root@linux-node1 ~]# docker exec -it web01-1 bash
root@5f31f0d6e509:/# ls            #查看sunrisenan.log存在
bin  boot  dev    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sunrisenan.log  sys  tmp  usr  var
root@5f31f0d6e509:/# 

#docker拷贝宿主机文件或目录到容器web中

[root@linux-node1 ~]# docker cp nginx.tar web:/
[root@linux-node1 ~]# docker exec -it web bash
root@web:/# ls
bin   dev  home  lib64    mnt       opt     root  sbin  sys  usr
boot  etc  lib     media    nginx.tar  proc  run   srv   tmp  var
root@web:~# exit
exit
[root@linux-node1 ~]# docker exec -it web ls /
bin   dev  home  lib64    mnt       opt     root  sbin  sys  usr
boot  etc  lib     media    nginx.tar  proc  run   srv   tmp  var
[root@linux-node1 ~]#

#输出容器中控制台的日志

[root@linux-node1 ~]# docker logs web

#查看容器中端口映射

[root@linux-node1 ~]# docker port web
80/tcp -> 0.0.0.0:88

#查看容器中有哪些进程

[root@linux-node1 ~]# docker top web
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                26405               26388               0                   13:11               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 26444               26405               0                   13:11               ?                   00:00:00            nginx: worker process
root                30780               26388               0                   15:49               pts/0               00:00:00            bash
[root@linux-node1 ~]# 

#删除容器

[root@linux-node1 ~]# docker rm $(docker ps -a|grep -i exit|awk '{print $1}')        #删除已停止的容器
[root@linux-node1 ~]# docker rm -f $(docker ps -a|awk '{print $1}')        #删除所有容器



清理退出容器
for i in `docker ps -a|grep -i exit|awk '{print $1}'`;do docker rm -f $i;done

清理所有容器
for i in `docker ps -qa`;do docker rm -f $i;done
文档更新时间: 2021-11-10 09:55   作者:李延召