docker run -d nginx
创建了三个容器,每个容器都独立运行,拥有着自己的硬件资源[root@localhost docker]# docker run -d nginx
ec5306ec7efec2ddbdd4e538721e3bbdc8ce29b48e6a82383f002547739421e7
[root@localhost docker]# docker run -d nginx
58065b181111e9db49f421893d028d89244764f07ce791639b14d7b0ae05f072
[root@localhost docker]# docker run -d nginx
2a9769bc8ed99191b81a2478853d9f4603b9c6305004ba970bd3bfade1c4bdfc
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a9769bc8ed9 nginx "/docker-entrypoint.…" 14 seconds ago Up 13 seconds 80/tcp affectionate_cerf
58065b181111 nginx "/docker-entrypoint.…" 15 seconds ago Up 15 seconds 80/tcp bold_solomon
ec5306ec7efe nginx "/docker-entrypoint.…" 18 seconds ago Up 17 seconds 80/tcp xenodochial_satoshi
e72612d340ec nginx "/docker-entrypoint.…" About a minute ago Exited (0) About a minute ago silly_herschel
docker stop ec
停止其中一个容器,其他容器仍能正常运行docker run -d nginx
中的nginx是创建容器的镜像,相当于是应用被打包后的一个文件[root@localhost docker]# docker stop ec
ec
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a9769bc8ed9 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp affectionate_cerf
58065b181111 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp bold_solomon
ec5306ec7efe nginx "/docker-entrypoint.…" About a minute ago Exited (0) 1 second ago xenodochial_satoshi
e72612d340ec nginx "/docker-entrypoint.…" 2 minutes ago Exited (0) 2 minutes ago silly_herschel
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 298ec0e28760 14 months ago 133MB
容器 | 虚拟机 | |
---|---|---|
启动速度 | 秒甚至毫秒启动 | 数秒至数十秒 |
系统内核 | 共享内核 | 不共享内核 |
实现技术 | 利用Linux内核技术Namespace/Cgroup等实现。 | 依赖虚拟化技术实现,由Hypervisor层实现对资源的隔离 |
隔离效果 | 进程级别的隔离 | 系统资源级别的隔离 |
资源消耗(性能) | 容器中的应用只是宿主机上的一个普通进程 | 使用虚拟化技术,就会有额外的资源消耗和占用 |
资源调用(敏捷性) | 应用进程直接由宿主机OS管理 | 应用进程需经过Hypervisor的拦截和处理,才能调用系统资源 |
运行数量 | 一台服务器上能启动1000+容器 | 一台服务器上一般不超过100台虚拟机 |
应用 | DevOps、微服务等 | 用于硬件资源划分 |
镜像 | 分层镜像 | 非分层镜像 |
有了容器后,传统的应用部署方式发生了变化。以前用户需要先申请一个虚拟机或者安装一台物理机,然后使用脚本或者手动的方式部署应用。由于容器镜像将应用及其依赖进行了打包,部署时,仅需要将镜像解压即可
名称 | 内容 |
---|---|
bundle.md(Filesystem Bundle ) | bundle中包含了运行容器所需要的所有信息,主要是config.json文件和rootfs。Runtime根据bundle启动容器 |
config.md | 包含对容器实施标准操作所必需的元数据,存放于config.json文件中。如oci版本、rootfs路径、mount目录、process、platform、容器hostname等 |
config-linux.md | Linux平台上对config.md的扩展,内容也包含在config.json文件中。如namespace、devices、CgroupPath、resources、sysctl、readonlyPaths等 |
runtime.md | 定义了3部分内容:容器状态(如status、pid等)、容器相关操作(如create、kill等)、容器生命周期 |
runtime-linux.md | 是Linux平台上对runtime.md的补充 |
容器可以被看成是一种轻量级虚拟化技术,利用namespace和Cgroup等隔离技术,将一个或者一组进程运行在一个相对独立的资源空间内
[root@localhost ~]# docker run -it centos /bin/bash
[root@24b87937f13d /]# ps axf
PID TTY STAT TIME COMMAND
1 pts/0 Ss 0:00 /bin/bash
14 pts/0 R+ 0:00 ps axf
[root@localhost ~]# docker inspect 24b87937f13d | grep Pid
"Pid": 96745,
"PidMode": "",
"PidsLimit": 0,
容器是为任务而生的,容器中的任务终止了,容器也就终止了
docker create 容器镜像
docker create nginx
Created
[root@localhost ~]# docker create nginx
ad33d31adf5279d49ae6d0a9466a00965790c59067d06d16e5bcce15960baeb6
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad33d31adf52 nginx "/docker-entrypoint.…" 10 seconds ago Created sweet_hoover
docker start 容器ID
docker start a
Up
[root@localhost ~]# docker start a
a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad33d31adf52 nginx "/docker-entrypoint.…" 18 minutes ago Up 4 minutes 80/tcp sweet_hoover
docker run 容器镜像
docker run nginx
[root@localhost ~]# docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
-d
容器启动时放入后台运行,例如:docker run -d nginx
[root@localhost ~]# docker run -d nginx
86b0789d0a3b7b240e5a8f7ddfd477c9560f252e5330bc6edd61bb63167c9f3f
-i
交互式执行-t
为容器分配一个终端,在使用该参数时需要为容器分配一个shell,但不是所有的镜像中安装了shell-i
和-t
一般配合使用,可以写作-it
[root@localhost ~]# docker run -d -it nginx /bin/bash
f01db67272d10a433f1e0df46fb5d1a6a693f6ea6403cd2e1a20a01d0b8a0380
docker attach
命令,可以进入容器[root@localhost ~]# docker attach f0
root@f01db67272d1:/#
-p
配置宿主机和容器的端口映射,例如:docker run -d -p 8081:80 nginx
,其中8081为宿主机的端口,80为容器的端口[root@localhost ~]# docker run -d -p 8081:80 nginx
53a133352e4b9f08f0425e9c217cd2824f6dac7cdf36dd29c0c5fdb41802c372
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53a133352e4b nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8081->80/tcp, :::8081->80/tcp quizzical_roentgen
docker run -d -p 8081:80/udp nginx
[root@localhost ~]# docker run -d -p 8081:80/udp nginx
cc12f8ccabb74d115f69b723e133b168d342fec0f8aba36bfbcce1353c50890a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc12f8ccabb7 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp, 0.0.0.0:8081->80/udp, :::8081->80/udp competent_napier
docker run -d -p 8081:80 -p 5000:5000 nginx
[root@localhost ~]# docker run -d -p 8081:80 -p 5000:5000 nginx
321f2a5e7e76298575d7a3f7c10e73820920c7bf001007b461bbac3ff9efb864
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
321f2a5e7e76 nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp, 0.0.0.0:8081->80/tcp, :::8081>80/tcp
-e
在容器启动时为其配置环境变量,例如:docker run -d -it -e A=100 nginx /bin/bash
,在启动容器时,将A设置为100[root@localhost ~]# docker run -d -it -e A=100 nginx /bin/bash
7ed7285fa56dec468e1db2cf3c20ed410a5878b45d9b27bf878b24580cd09e7e
[root@localhost ~]# docker attach 7e
root@7ed7285fa56d:/# echo $A
100
docker run -d -it -e A=100 -e B=200 nginx /bin/bash
[root@localhost ~]# docker run -d -it -e A=100 -e B=200 nginx /bin/bash
d1a3b533380181168ab9642d5479da2671ed8af45bae925ca67eeea2b3ec4213
[root@localhost ~]# docker attach d1
root@d1a3b5333801:/# echo $A
100
root@d1a3b5333801:/# echo $B
200
--workdir
指定容器内部的工作目录,例如:docker run -d -it --workdir /tmp nginx /bin/bash
,将工作目录修改为/tmp[root@localhost ~]# docker run -d -it --workdir /tmp nginx /bin/bash
c7d7afdc2d0b9c52c988079c9ccb88cf9637ca32c731bf93448b5e39c38e2a62
[root@localhost ~]# docker attach c7
root@c7d7afdc2d0b:/tmp# pwd
/tmp
--name
用于创建和启动容器时,为容器指定一个名称,例如:docker run -d --name test nginx
,将容器名称指定为test[root@localhost ~]# docker run -d --name test nginx
4f830a51df2c72ec342610ca621d609cbe65103c0cd815cd4e6d87ff7a620cbe
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f830a51df2c nginx "/docker-entrypoint.…" 12 seconds ago Up 12 seconds 80/tcp test
--dns
为容器指定DNS,例如:docker run -d -it --dns 192.168.38.2 busybox /bin/sh
,将容器DNS指定为192.168.38.2-c
可以用来限制容器cpu资源,例如:docker run -d --cpus 0.2 progrium/stress --cpu 1
中的“–cpus 0.2”表示占用了宿主机20%的cpu资源[root@localhost Dockerfile]# docker run -d --cpus 0.2 progrium/stress --cpu 1
A7a28c2e3954728fcbe94a68d6f74fd25a982818ff5d5b59805082d269a134f5
[root@localhost Dockerfile]# top
top - 06:56:30 up 2:55, 2 users, load average: 0.87, 0.57, 0.28
Tasks: 119 total, 2 running, 117 sleeping, 0 stopped, 0 zombie
%Cpu(s): 9.7 us, 0.2 sy, 0.0 ni, 90.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861300 total, 2343544 free, 445236 used, 1072520 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3168692 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5351 root 20 0 7304 96 0 R 19.9 0.0 0:20.84 stress
662 root 20 0 273008 4752 3656 S 0.3 0.1 0:25.42 vmtoolsd
1071 root 20 0 1089408 45668 14616 S 0.3 1.2 0:07.91 containerd
progrium/stress
是一个用来进行压力测试的容器镜像--cpu 1
表示在容器中产生1个进程,用来不停的计算随机数的平方,这样认为把容器所有的计算资源全部占用掉了--cpu-period
和--cpu-quota
配合使用也可以用来限制容器cpu资源,例如:docker run -d --cpu-period 10000 --cpu-quota 3000 progrium/stress --cpu 1
中的“–cpu-period 10000 --cpu-quota 3000”相当于“–cpus 0.3”[root@localhost Dockerfile]# docker run -d --cpu-period 10000 --cpu-quota 3000 progrium/stress --cpu 1
Fc060bff9b4502021cbb01f262cff05861c0a129eaa04d2d5170c673791ea2c9
[root@localhost Dockerfile]# top
top - 07:11:57 up 3:11, 2 users, load average: 0.00, 0.04, 0.14
Tasks: 120 total, 2 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 16.0 us, 0.0 sy, 0.0 ni, 84.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861300 total, 2341900 free, 446876 used, 1072524 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3167064 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5747 root 20 0 7304 96 0 R 29.7 0.0 0:04.10 stress
662 root 20 0 273008 4752 3656 S 0.8 0.1 0:27.20 vmtoolsd
1 root 20 0 125504 3936 2572 S 0.0 0.1 0:02.49 systemd
--cpu-period
和--cpu-quota
分别指在10000个总时间单位中,分配给该容器3000个时间单位--memory
或-m
可以用来限制容器的内存资源,例如:docker run -d -m 2000M progrium/stress --vm 1 --vm-bytes 2000M
中的“-m”表示为该容器分配了2000 M内存资源[root@localhost Dockerfile]# free -m
total used free shared buff/cache available
Mem: 3770 424 2299 11 1047 1480
Swap: 0 0 0
[root@localhost Dockerfile]# docker run -d -m 2000M progrium/stress --vm 1 --vm-bytes 2000M
Ad4647f1657f6ac382d400e12e66aaa23b0ecb428001dbc389078e7bcd8b312f
[root@localhost Dockerfile]# top
top - 07:25:24 up 3:24, 2 users, load average: 1.14, 0.55, 0.28
Tasks: 123 total, 2 running, 121 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 19.2 sy, 0.0 ni, 80.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861300 total, 327704 free, 2461040 used, 1072556 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 1152924 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6073 root 20 0 2055308 1.9g 244 R 100.0 52.1 1:03.88 stress
attach
:进入容器,然后执行命令,因此需要在运行时容器为其提前创建好终端。执行完命令使用exit
可退出容器[root@localhost ~]# docker run -d -it nginx /bin/bash
792497bf8801f008fd9372ca8d44680192447148951f2fcb85bde4a8fd158dfd
[root@localhost ~]# docker attach 79
root@792497bf8801:/# echo "this is a test"
this is a test
root@792497bf8801:/# exit
exit
[root@localhost ~]#
[root@localhost ~]# docker run -d nginx
995989fa9dd51d7587a82fa7085059eec78858a7afa0cdcfd5cca2b9f5789533
[root@localhost ~]# docker exec 99 echo "this is a test"
this is a test
[root@localhost ~]# docker exec -it 79 /bin/bash
root@792497bf8801:/#
docker pause
可暂停一个运行中的容器[root@localhost ~]# docker pause 79
79
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
792497bf8801 nginx "/docker-entrypoint.…" 26 minutes ago Up 23 minutes (Paused) 80/tcp dazzling_keller
docker unpause
可将暂停的容器恢复到运行中[root@localhost ~]# docker unpause 79
79
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
792497bf8801 nginx "/docker-entrypoint.…" 28 minutes ago Up 25 minutes 80/tcp dazzling_keller
docker ps
可以列出当前容器的概要信息
-a
可列出所有的容器-q
列出容器室时仅显示容器ID-n
列出最近创建的n个容器(包括所有状态)docker ps -aq
,可列出所有的容器[root@localhost ~]# docker ps -aq
5ed66058457a
634f4c4e9b45
4e0f8a174dad
c545b3d90797
792497bf8801
docker ps -n 2
,可列出最近创建的两个容器[root@localhost ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ed66058457a nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) 4 seconds ago elated_neumann
634f4c4e9b45 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp tender_shtern
docker inspect
可查看容器的详细信息,默认情况下,该命令的输出是以go语言的语法显示,如果需要使用其他格式输出,可添--format
选项docker inspect --format=”{{json .NetworkSettings}}“ pedantic_burnell | python -m json.tool
可以以json格式输出容器的网络信息使用命令docker logs 容器ID
可查看容器的日志,例如docker logs 06
即可查看到该容器的日志
[root@localhost ~]# docker logs 06
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
docker restart 容器ID
可以重启容器[root@localhost ~]# docker restart 06
06
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0634a706db21 nginx "/docker-entrypoint.…" 5 hours ago Up 39 seconds 80/tcp pedantic_burnell
--restart
可以设置容器的自动重启策略【如:--restart=always
】
no
在容器退出时不重启容器,默认使用该策略on-failure
在容器非正常退出时重启容器,后面可以添加尝试重启的次数always
容器退出时总是重启容器unless-stopped
在容器退出时总是重启容器,但是不考虑在docker守护进程启动时就已经停止了的容器docker rm 容器ID
删除容器,该命令只能删除非运行状态的容器,使用-f
选项可以强制删除所有状态的容器[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0634a706db21 nginx "/docker-entrypoint.…" 5 hours ago Up 7 minutes 80/tcp pedantic_burnell
[root@localhost ~]# docker rm 06
Error response from daemon: You cannot remove a running container 0634a706db21f8f5637143b770554c595709a17553677e62a5fe8cdb27dffafc. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm -f 06
06
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
--rm
选项可以在容器停止后自动删除谨慎操作。
docker ps -aq | xargs docker rm -f
看这篇文章