Linxu环境安装Docker
(示范环境CentOS 7.6, 为了快速入门所以只介绍其中一种便捷的安装方法)
安装依赖包
yum install -y device-mapper-persistent-data lvm2 yum-utils
device-mapper-persistent-data和lvm2是devicemapper存储驱动所需,yum-utils是下面yum-config-manager所需
添加Docker软件源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装Dcoker
yum install -y docker-ce
启动确认Docker
systemctl start docker
systemctl status docker
设置Dcoker开机自启
systemctl enable docker
查看Docker信息
docker info
配置Docker加速器
Docker Hub提供数以万计的镜像,在国内需配加速器才稳定和速度
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://4a1df5ef.m.daocloud.io
或者加到docker配置文件/etc/docker/daemon.json
然后重启docker:systemctl restart docker
[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://4a1df5ef.m.daocloud.io
docker version >= 1.12
{
"registry-mirrors": ["http://4a1df5ef.m.daocloud.io"],
"storage-driver": "devicemapper"
}
Success.
You need to restart docker to take effect: sudo systemctl restart docker
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://4a1df5ef.m.daocloud.io"],
"storage-driver": "devicemapper"
}
[root@localhost ~]# systemctl restart docker
[root@localhost ~]#
容器管理
Docker生命周期管理命令粗略图
创建容器
docker (container) run [OPTIONS] IMAGE [COMMAND] [ARG...]
下面列出部分常用参数(更详细的查看docker run --help):
参数 | 说明 |
-i,--interactive | 交互模式 |
-t,--tty | 分配伪终端 |
-d,--detach | 在后台运行容器并打印容器ID,不设置默认false |
-e,-env list) | 设环境变量 |
-p,--publish list | 将容器的端口发布到主机 |
-P,--publish-all | 将容器所有公开的端口发布到主机随机端口 |
--name string | 分配容器名,可用容器名进行管理 |
-h,--hostname="" | 设容器主机名 |
-m,--memory bytes | 设置容器可用内存上限 |
--memory-swap bytes | 设置容器可用的swap分区内存量("-1"无限制交换) |
--memory-swappiness int | 设置容器可用的swap分区内存量百分比(0-100, 默认-1) |
--oom-kill-disable | 禁OOM Killer |
-c, --cpu-shares int | CPU份额(相对权重) |
--cpus decimal | 容器可使用cpu数 |
--cpuset-cpus string | 设容器可用哪些CPU核心(0-3或0,1), 设容器独占CPU |
-u,--user string | 指定容器用户名或UID(格式:< name| UID >[: |
--mount | 将文件系统挂载附加到容器 |
-v,--volume list | 绑定挂载卷 |
--volume-driver string | 选择容器的卷驱动 |
--reatart | 容器退出时重启策略,默认no |
-w, --workdir string | 容器内的工作目录 |
--net="" | bridge Docker启动后创建docker0网桥,不指定就加入这个网桥 (默认) |
host 共用宿主机网络,不单独分配网络空间 | |
container:CONTAINER/ID 使用指定容器网络空间 | |
none 分配一个网络空间,但不配置,内部自带DNS发现,同一网络容器可以相互通信 |
创建一个Nginx容器在后台运行
docker (container) run -d nginx
[root@localhost ~]# docker run -d nginx #-d 后台运行
Unable to find image 'nginx:latest' locally #第一次run会先下载到本地
latest: Pulling from library/nginx
743f2d6c1f65: Pull complete
6bfc4ec4420a: Pull complete
688a776db95f: Pull complete
Digest: sha256:e770165fef9e36b990882a4083d8ccf5e29e469a8609bb6b2e3b47d9510e2c8d
Status: Downloaded newer image for nginx:latest
727f48e44dabd794d08914fe3e41899923cd1751f53af0327b01b4cfc7b09211
[root@localhost ~]# docker images #查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 53f3fd8007f7 2 days ago 109MB
[root@localhost ~]# docker container ls #查看运行中容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
727f48e44dab nginx "nginx -g 'daemon of鈥 6 minutes ago Up 6 minutes 80/tcp compassionate_panini
[root@localhost ~]# docker ps #查看运行中容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
727f48e44dab nginx "nginx -g 'daemon of鈥 6 minutes ago Up 6 minutes 80/tcp compassionate_panini
显示运行中的容器
docker ps 或 docker container ls
[root@localhost project]# docker ps
#随机生成的容器ID 基于哪个镜像创建 启动容器时运行的命令 创建时间 运行状态 端口映射 容器名
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2420149b614 php:v2 "php-fpm" About an hour ago Up About an hour lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 25 hours ago Up 24 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
c9cb6d179649 busybox "sh" 4 days ago Up 4 days busybox
e89b0a8d88ff busybox "sh" 9 days ago Up 9 days bs2
7832b11a081c busybox "sh" 9 days ago Up 9 days bs1
9e700ee368ca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
9796a2251876 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
[root@localhost project]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2420149b614 php:v2 "php-fpm" About an hour ago Up About an hour lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 25 hours ago Up 24 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
c9cb6d179649 busybox "sh" 4 days ago Up 4 days busybox
e89b0a8d88ff busybox "sh" 9 days ago Up 9 days bs2
7832b11a081c busybox "sh" 9 days ago Up 9 days bs1
9e700ee368ca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
9796a2251876 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
显示所有容器,包括未运行的
docker ps -a 或 docker container ls -a
[root@localhost project]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2420149b614 php:v2 "php-fpm" About an hour ago Up About an hour lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 25 hours ago Up 24 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
49e2ca3fa526 centos:7 "/bin/bash" 3 days ago Exited (137) 2 days ago 7-base
c9cb6d179649 busybox "sh" 4 days ago Up 4 days busybox
e89b0a8d88ff busybox "sh" 9 days ago Up 9 days bs2
7832b11a081c busybox "sh" 9 days ago Up 9 days bs1
9e700ee368ca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
073f8cb49877 nginx "nginx -g 'daemon of鈥 9 days ago Exited (0) 25 hours ago nginx02
0eabd05a20ac nginx "nginx02 -p 88:80 ng鈥 9 days ago Created 80/tcp nifty_mendeleev
9796a2251876 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
[root@localhost project]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2420149b614 php:v2 "php-fpm" About an hour ago Up About an hour lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 25 hours ago Up 24 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
49e2ca3fa526 centos:7 "/bin/bash" 3 days ago Exited (137) 2 days ago 7-base
c9cb6d179649 busybox "sh" 4 days ago Up 4 days busybox
e89b0a8d88ff busybox "sh" 9 days ago Up 9 days bs2
7832b11a081c busybox "sh" 9 days ago Up 9 days bs1
9e700ee368ca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
073f8cb49877 nginx "nginx -g 'daemon of鈥 9 days ago Exited (0) 25 hours ago nginx02
0eabd05a20ac nginx "nginx02 -p 88:80 ng鈥 9 days ago Created 80/tcp nifty_mendeleev
9796a2251876 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 nginx "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
创建一个后台运行的容器,并设容器名/端口映射/主机名/环境变量
docker run -d --name nginx-1 -p 86:80 -h nginx-1 -e test=1234 nginx
[root@localhost ~]# docker run -d --name nginx-1 -p 86:80 -h nginx-1 -e test=1234 nginx #宿主机的86端口映射到容器的80端口
87510e62003567d3bef26c0ec28fa29ee3f7807930c0b3789058b6ab1a9db3b0
[root@localhost ~]# docker ps #查看运行中容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87510e620035 nginx "nginx -g 'daemon of鈥 9 seconds ago Up 4 seconds 0.0.0.0:86->80/tcp nginx-1
[root@localhost ~]# docker exec -it nginx-1 bash #进入容器并确认设置是否生效
root@nginx-1:/# hostname
nginx-1
root@nginx-1:/# echo $test
1234
root@nginx-1:/# exit #退出当前容器
exit
[root@localhost ~]# netstat -tlnp #宿主机可以看到设置容器的端口86
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6607/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6701/master
tcp6 0 0 :::86 :::* LISTEN 11905/docker-proxy
tcp6 0 0 :::22 :::* LISTEN 6607/sshd
tcp6 0 0 ::1:25 :::* LISTEN 6701/master
[root@localhost ~]# curl http://127.0.0.1:86 #在宿主机访问映射的端口看是否生效
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@localhost ~]#
创建一个容器,并限制CPU/内存
docker run -d --name nginx-2 --cpus="0.5" --memory="512M" --oom-kill-disable nginx
[root@localhost ~]# docker run -d --name nginx-2 --cpus="0.5" --memory="512M" --oom-kill-disable nginx #限制的CPU/内存不能超过宿主机的上限
c974ca01646cc39dafcf8065ad380fd088d75bf800b456a947c1fb1e38a6edda
[root@localhost ~]# docker stats --no-stream nginx-2 #查看容器资源使用及限制
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
c974ca01646c nginx-2 0.00% 1.352MiB / 512MiB 0.26% 656B / 0B 6.9MB / 0B 2
[root@localhost ~]#
从宿主机进入运行中的容器
docker exec -it #交互进入容器;exec也可以在宿主机上远程执行运行中容器里命令
docker attach #进入容器,但[Ctrl]+d或exit退出时候容器也会停止
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8acbd766d3c6 busybox "sh" 3 minutes ago Up 3 minutes busybox
[root@localhost ~]# docker exec -it busybox sh
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
[root@localhost ~]# docker attach busybox
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8acbd766d3c6 busybox "sh" 5 minutes ago Exited (0) 8 seconds ago busybox
[root@localhost ~]# docker start busybox
busybox
[root@localhost ~]# docker exec busybox ls /
bin
dev
etc
home
proc
root
sys
tmp
usr
var
宿主机和容器相互拷贝文件
docker cp "宿主机目录或文件" “容器名字或ID”:"容器路径" #从宿主机拷贝到容器
docker cp “容器名字或ID”:"容器路径" "宿主机目录或文件" #从容器拷贝到宿主机
[root@localhost ~]# docker run -itd --name busybox busybox
ff9766324a82187ebd78b2fc38d2d78311fbb434ffa7d0344c86ced9b43431c1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff9766324a82 busybox "sh" 3 seconds ago Up 3 seconds busybox
[root@localhost ~]# touch 123.txt
[root@localhost ~]# docker cp 123.txt busybox:/tmp/
[root@localhost ~]# docker exec busybox ls /tmp/
123.txt
[root@localhost ~]# docker exec -d busybox touch /tmp/456.txt
[root@localhost ~]# docker exec busybox ls /tmp/
123.txt
456.txt
[root@localhost ~]# docker cp busybox:/tmp/456.txt .
[root@localhost ~]# ls
123.txt 456.txt anaconda-ks.cfg
[root@localhost ~]# mkdir www
[root@localhost ~]# docker cp www busybox:/wwwroot #拷贝目录到容器并重命名
[root@localhost ~]# docker exec busybox ls /
bin
dev
etc
home
proc
root
sys
tmp
usr
var
wwwroot
[root@localhost ~]#
容器和宿主机共享文件
容器里面的数据随容器删除而删除,要保存/共享数据就挂载文件系统到容器(volume、bind mount、tmpfs mount)
1)volume
多容器间可挂载同样的卷,共享数据
参数 | 说明 |
docker volume create | 创建卷 |
docker volume inspect | 显示一个或多个卷的详细情况 |
docker volume ls | 列出卷表 |
docker volume rm | 删除一个或多个卷 |
docker volume prune | 删除所有未使用的本地卷 |
[root@localhost ~]# docker volume ls #查看卷
DRIVER VOLUME NAME
[root@localhost ~]# docker volume create test-vol #创建卷
test-vol
[root@localhost ~]# docker volume ls #查看卷
DRIVER VOLUME NAME
local test-vol
[root@localhost ~]# docker volume inspect test-vol #查看卷详情
[
{
"CreatedAt": "2019-05-15T09:24:37+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/test-vol/_data", #卷的保存路径
"Name": "test-vol",
"Options": {},
"Scope": "local"
}
]
[root@localhost ~]# docker run -itd --name busybox-vol -v test-vol:/mnt busybox #运行一个使用上面卷的容器
5dde5eac0341d90795045907a9fb980a88d97a91da6d8dd3abef70e2a5402d03
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5dde5eac0341 busybox "sh" 4 seconds ago Up 2 seconds busybox-vol
[root@localhost ~]# docker exec -d busybox-vol touch /mnt/test.txt #在宿主机用exec远程运行容器命令touch创建一个文件
[root@localhost ~]# docker exec busybox-vol ls /mnt/test.txt #在宿主机用exec远程运行容器命令ls看刚才创建的文件
/mnt/test.txt
[root@localhost ~]# ls /var/lib/docker/volumes/test-vol/_data/ #卷默认存储路径
test.txt
local test-vol
[root@localhost ~]# docker run -itd --name busybox01 --mount src=test-vol,dst=/mnt busybox #除了-v, 还可以用--mount (docker 17CE后才加入的--mount)
fb9e882b99dcda7430fc6f2f28bf93bf32d4f7145e76a0ade807fcdb8c02cf73
[root@localhost ~]# docker exec busybox01 ls /mnt
test.txt
[root@localhost tmp]# docker volume create --driver local --opt type=nfs --opt o=addr=192.168.101.128,rw --opt device=:/mnt vol-nfs #使用外部存储作为卷
vol-nfs
[root@localhost tmp]# docker volume ls
DRIVER VOLUME NAME
local test-vol
local vol-nfs
[root@localhost tmp]# docker volume inspect vol-nfs
[
{
"CreatedAt": "2019-05-16T09:43:45+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/vol-nfs/_data",
"Name": "vol-nfs",
"Options": {
"device": ":/mnt",
"o": "addr=192.168.101.128,rw",
"type": "nfs"
},
"Scope": "local"
}
]
[root@localhost tmp]#
2、bind mount
从宿主机共享文件到容器
[root@localhost tmp]# mkdir /tmp/www #运行前挂载的宿主机目录要建好
[root@localhost tmp]# docker run -d --name nginx-2 -v /tmp/www:/usr/local/nginx/html nginx #-v 宿主机路径:容器路径。用--mount挂载也OK
d01c794ada8a8b5064c607717df0030497f4f482f204ed736263701d7a2412fd
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d01c794ada8a nginx "nginx -g 'daemon of鈥 9 seconds ago Up 7 seconds 80/tcp nginx-2
[root@localhost tmp]# docker inspect nginx-2 #用inpect确认下容器挂载信息
......
"Mounts": [
{
"Type": "bind",
"Source": "/tmp/www",
"Destination": "/usr/local/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
......
[root@localhost tmp]# echo > /tmp/www/test.html
[root@localhost tmp]# docker exec nginx-2 ls /usr/local/nginx/html
test.html
[root@localhost tmp]#
3、tmpfs
挂载到内存中,不写入文件系统。适合数据不需要保存,而且提高性能
[root@localhost tmp]# docker volume create --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=100m,uid=1000 vol-tmpfs #大小为100m,uid为1000
vol-tmpfs
[root@localhost tmp]# docker volume ls
DRIVER VOLUME NAME
local test-vol
local vol-nfs
local vol-tmpfs
[root@localhost tmp]# docker volume inspect vol-tmpfs
[
{
"CreatedAt": "2019-05-16T09:55:20+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/vol-tmpfs/_data",
"Name": "vol-tmpfs",
"Options": {
"device": "tmpfs",
"o": "size=100m,uid=1000",
"type": "tmpfs"
},
"Scope": "local"
}
]
[root@localhost tmp]#
查看容器日志和进程
[root@localhost tmp]# docker run -d --name nginx -p 86:80 nginx
70b50aab941cd791032ccf30f379e97bc5c8f6341148b96ea59709e3ba7731aa
[root@localhost tmp]# curl -l http://127.0.0.1:86
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@localhost tmp]# docker logs nginx #查看容器日志
172.17.0.1 - - [16/May/2019:02:36:49 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@localhost tmp]# docker top nginx #查看容器进程
UID PID PPID C STIME TTY TIME CMD
root 76819 76803 0 10:36 ? 00:00:00 nginx: master process nginx -g daemon off;
101 76858 76819 0 10:36 ? 00:00:00 nginx: worker process
[root@localhost tmp]#
容器网络配置
--net="" | bridge Docker启动后创建docker0网桥,不指定就加入这个网桥 (默认) |
host 共用宿主机网络,不单独分配网络空间 | |
container:CONTAINER/ID 使用指定容器网络空间 | |
none 分配一个网络空间,但不配置,内部自带DNS发现,同一网络容器可以相互通信 |
none,自定义网络
命令 | 说明 |
docker network create | 创建一个网络 空间 |
docker network ls | 查看网络列表 |
docker network inspect | 查看一个或多个网络详情 |
docker network rm | 删除一个或多个网络 |
docker network prune | 删除所有未使用的网络 |
docker network connect | 连接容器网络 |
docker network disconnect | 断开容器网络 |
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
828a106004fe bridge bridge local
6b33d0ae2e5d host host local
7360975b9dcb none null local
[root@localhost ~]# docker network create test #创建一个命名为test的网络
1fed7e2c0662e47989d2c5d1b9bfebf7efe496f09e45fc5ef606a99e0c3e01f1
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
828a106004fe bridge bridge local
6b33d0ae2e5d host host local
7360975b9dcb none null local
1fed7e2c0662 test bridge local
[root@localhost ~]# docker container run -itd --name busybox1 --net=test busybox
f7a7de3b06dba85ad0aabf0544a4b4877bb21de285d1b6741cd18d6b332ff98b
[root@localhost ~]# docker container run -itd --name busybox2 --net=test busybox
3a4a2d6b16b16c0ee92fa063adb60e360416e2e20d6e9b35052a4d5f7a37d055
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a4a2d6b16b1 busybox "sh" 15 seconds ago Up 14 seconds busybox2
f7a7de3b06db busybox "sh" 21 seconds ago Up 20 seconds busybox1
70b50aab941c nginx "nginx -g 'daemon of鈥 2 hours ago Up 2 hours 0.0.0.0:86->80/tcp nginx
[root@localhost ~]# docker exec -it busybox1 ip a #查看busybox1的ip
1: lo: mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
74: eth0@if75: mtu 1500 qdisc noqueue
link/ether 02:42:ac:13:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.19.0.2/16 brd 172.19.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@localhost ~]# docker exec -it busybox2 ip a #查看busybox2的ip
1: lo: mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
76: eth0@if77: mtu 1500 qdisc noqueue
link/ether 02:42:ac:13:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.19.0.3/16 brd 172.19.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@localhost ~]# docker exec -it busybox1 ping 172.19.0.3 -c 2 #测试通过busybox1丢包到busybox2
PING 172.19.0.3 (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.072 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.149 ms
--- 172.19.0.3 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.072/0.110/0.149 ms
从镜像仓库拉取镜像到本地
docker pull nginx
[root@localhost project]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
743f2d6c1f65: Pull complete
6bfc4ec4420a: Pull complete
688a776db95f: Pull complete
Digest: sha256:0e409e180983aea7972c92b0a8ae538d1d3c802fe3a8f795ad8049951894590a
Status: Downloaded newer image for nginx:latest
列出本地镜像
docker images 或 docker image ls
[root@localhost project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f About an hour ago 527MB
php-56 v2 83996a2a5a02 2 hours ago 527MB
nginx v1 aa9d402c7fca 25 hours ago 371MB
nginx-112 latest 5a078b28d864 45 hours ago 371MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
nginx latest 27a188018e18 3 weeks ago 109MB
busybox latest af2f74c517aa 5 weeks ago 1.2MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost project]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f About an hour ago 527MB
php-56 v2 83996a2a5a02 2 hours ago 527MB
nginx v1 aa9d402c7fca 25 hours ago 371MB
nginx-112 latest 5a078b28d864 45 hours ago 371MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
nginx latest 27a188018e18 3 weeks ago 109MB
busybox latest af2f74c517aa 5 weeks ago 1.2MB
centos 7 9f38484d220f 8 weeks ago 202MB
列出包括中间层镜像的本地镜像
docker image ls -a
注:这里看到的镜像大小不是实际占用磁盘的大小,Docker镜像是多层存储结构,并且可以继承复用的,所以不同镜像可复用相同的基础镜像。下面无标签的镜像是中间层镜像,不能删除,否则会导致引用的镜像因为依赖丢失出错
[root@localhost project]# docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f About an hour ago 527MB
ced621504315 About an hour ago 527MB
php-56 v2 83996a2a5a02 2 hours ago 527MB
d6e0b78cd99f 2 hours ago 527MB
a721014faef0 2 hours ago 527MB
b43aec20c2ae 2 hours ago 527MB
96d124d288ea 2 hours ago 527MB
f1746dbc5baa 2 hours ago 404MB
cad90b1023e0 2 hours ago 202MB
nginx v1 aa9d402c7fca 25 hours ago 371MB
nginx-112 latest 5a078b28d864 45 hours ago 371MB
2bf655d88a00 45 hours ago 371MB
addff8f76cf9 45 hours ago 371MB
371cff142ac2 45 hours ago 371MB
d6ee785bb86c 45 hours ago 371MB
7f38493054a5 45 hours ago 364MB
php v1 157ea6156082 2 days ago 527MB
482b33cdbb22 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
eb355c91c0c3 2 days ago 527MB
72362a3403b5 2 days ago 527MB
88f5357500e4 2 days ago 527MB
c1d4b84ccbc5 2 days ago 527MB
04add163a0f6 2 days ago 404MB
6a1cc8607179 2 days ago 202MB
nginx latest 27a188018e18 3 weeks ago 109MB
busybox latest af2f74c517aa 5 weeks ago 1.2MB
centos 7 9f38484d220f 8 weeks ago 202MB
查看某个镜像中各层情况
docker history “名字或者ID”
[root@localhost project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f 2 hours ago 527MB
php-56 v2 83996a2a5a02 2 hours ago 527MB
nginx v1 aa9d402c7fca 26 hours ago 371MB
nginx-112 latest 5a078b28d864 46 hours ago 371MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
nginx latest 27a188018e18 3 weeks ago 109MB
busybox latest af2f74c517aa 5 weeks ago 1.2MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost project]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
27a188018e18 3 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon鈥 0B
3 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
3 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
3 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx鈥 0B
3 weeks ago /bin/sh -c set -x && apt-get update && apt鈥 54.1MB
3 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.12.0鈥 0B
3 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.12鈥 0B
6 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do鈥 0B
6 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
6 weeks ago /bin/sh -c #(nop) ADD file:4fc310c0cb879c876鈥 55.3MB
[root@localhost project]# docker history 27a188018e18
IMAGE CREATED CREATED BY SIZE COMMENT
27a188018e18 3 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon鈥 0B
3 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
3 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
3 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx鈥 0B
3 weeks ago /bin/sh -c set -x && apt-get update && apt鈥 54.1MB
3 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.12.0鈥 0B
3 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.12鈥 0B
6 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do鈥 0B
6 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
6 weeks ago /bin/sh -c #(nop) ADD file:4fc310c0cb879c876鈥 55.3MB
查看镜像详细情况
docker inspect "name(:tag) | ID" 或 docker image inspect "name(:tag) | ID"
[root@localhost project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f 3 hours ago 527MB
php-56 v2 83996a2a5a02 3 hours ago 527MB
nginx v1 aa9d402c7fca 27 hours ago 371MB
nginx-112 latest 5a078b28d864 46 hours ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
busybox latest af2f74c517aa 5 weeks ago 1.2MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost project]# docker inspect nginx:v1
[
{
"Id": "sha256:aa9d402c7fca8fc81d014c0cffc3d2b7ebd25b772f04dc9384c3b06e96a62904",
"RepoTags": [
"nginx:v1"
],
"RepoDigests": [],
"Parent": "sha256:5a078b28d864f2281109d16d69439648982733d0fd9bd5c482ad221cd6f088c8",
"Comment": "",
"Created": "2019-05-09T02:42:10.753920783Z",
"Container": "",
"ContainerConfig": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/nginx/sbin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) COPY file:0da0825bc547dd6839d0a1a98c9b77784ef5e76890405da3149009b2c5da4e8b in /usr/local/nginx/conf/ "
],
"ArgsEscaped": true,
"Image": "sha256:5a078b28d864f2281109d16d69439648982733d0fd9bd5c482ad221cd6f088c8",
"Volumes": null,
"WorkingDir": "/usr/local/nginx",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20190305",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "18.09.5",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/nginx/sbin"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "sha256:5a078b28d864f2281109d16d69439648982733d0fd9bd5c482ad221cd6f088c8",
"Volumes": null,
"WorkingDir": "/usr/local/nginx",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20190305",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 370946800,
"VirtualSize": 370946800,
"GraphDriver": {
"Data": {
"DeviceId": "168",
"DeviceName": "docker-8:3-16821793-f35c2d0e33f00ff56fb8cab15c0e0c72cb898dfb179558fbaabc753c38c6c50e",
"DeviceSize": "10737418240"
},
"Name": "devicemapper"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854",
"sha256:9790e7f584c58ed832d87ab6e4ce7aee9cb9040d94ff954f29d9779547f35a80",
"sha256:af4f89d1758bb97fc3fc5e86a1bb3bfea2963fd9b4a83e913f4be1b3807fccc9",
"sha256:cfde45d2e1eb1611e7840943fc263cf631e4ff88727de88ded7725da4a5218eb"
]
},
"Metadata": {
"LastTagTime": "2019-05-09T10:42:11.172308504+08:00"
}
}
]
[root@localhost project]#
删除镜像
docker rmi "name(:tag) | ID" 或 docker image rm "name(:tag) | ID"
注:删除镜像之前需要停止并删除正在运行这个镜像的相关容器
[root@localhost project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f 3 hours ago 527MB
php-56 v2 83996a2a5a02 3 hours ago 527MB
nginx v1 aa9d402c7fca 27 hours ago 371MB
nginx-112 latest 5a078b28d864 46 hours ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
busybox latest af2f74c517aa 5 weeks ago 1.2MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost project]# docker rmi busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container 7832b11a081c is using its referenced image af2f74c517aa
[root@localhost project]# docker stop 7832b11a081c
7832b11a081c
[root@localhost project]# docker rmi busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container e89b0a8d88ff is using its referenced image af2f74c517aa
[root@localhost project]# docker stop e89b0a8d88ff
e89b0a8d88ff
[root@localhost project]# docker rmi busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container e89b0a8d88ff is using its referenced image af2f74c517aa
[root@localhost project]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2420149b614 php:v2 "php-fpm" 3 hours ago Up 3 hours lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 26 hours ago Up 26 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
49e2ca3fa526 centos:7 "/bin/bash" 3 days ago Exited (137) 2 days ago 7-base
c9cb6d179649 busybox "sh" 4 days ago Up 4 days busybox
e89b0a8d88ff busybox "sh" 9 days ago Exited (137) 40 seconds ago bs2
7832b11a081c busybox "sh" 9 days ago Exited (137) About a minute ago bs1
9e700ee368ca 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
073f8cb49877 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Exited (0) 27 hours ago nginx02
0eabd05a20ac 27a188018e18 "nginx02 -p 88:80 ng鈥 9 days ago Created 80/tcp nifty_mendeleev
9796a2251876 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
[root@localhost project]# docker rm bs1 bs2
bs1
bs2
[root@localhost project]# docker rmi busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container c9cb6d179649 is using its referenced image af2f74c517aa
[root@localhost project]# docker stop c9cb6d179649
c9cb6d179649
[root@localhost project]# docker rmi busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container c9cb6d179649 is using its referenced image af2f74c517aa
[root@localhost project]# docker rm c9cb6d179649
c9cb6d179649
[root@localhost project]# docker rmi busybox
Untagged: busybox:latest
Untagged: busybox@sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4
Deleted: sha256:af2f74c517aac1d26793a6ed05ff45b299a037e1a9eefeae5eacda133e70a825
Deleted: sha256:0b97b1c81a3200e9eeb87f17a5d25a50791a16fa08fc41eb94ad15f26516ccea
[root@localhost project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f 3 hours ago 527MB
php-56 v2 83996a2a5a02 3 hours ago 527MB
nginx v1 aa9d402c7fca 27 hours ago 371MB
nginx-112 latest 5a078b28d864 46 hours ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 2 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost project]#
暂停、删除所有运行中的容器,删除本地镜像
docker kill $(docker ps -q) ; docker rm $(docker ps -aq) ; docker rmi $(docker images -qa)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5c1eb77fff1 busybox "sh" About an hour ago Up About an hour buxybox
727f48e44dab nginx "nginx -g 'daemon of鈥 2 days ago Up 2 days 80/tcp compassionate_panini
[root@localhost ~]# docker ps -q
f5c1eb77fff1
727f48e44dab
[root@localhost ~]# docker kill $(docker ps -q)
f5c1eb77fff1
727f48e44dab
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5c1eb77fff1 busybox "sh" About an hour ago Exited (137) 2 minutes ago buxybox
727f48e44dab nginx "nginx -g 'daemon of鈥 2 days ago Exited (137) 2 minutes ago compassionate_panini
[root@localhost ~]# docker ps -aq
f5c1eb77fff1
727f48e44dab
[root@localhost ~]# docker rm $(docker ps -aq)
f5c1eb77fff1
727f48e44dab
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 64f5d945efcc 3 days ago 1.2MB
nginx latest 53f3fd8007f7 4 days ago 109MB
[root@localhost ~]# docker images -qa
64f5d945efcc
53f3fd8007f7
[root@localhost ~]# docker rmi $(docker images -qa)
Untagged: busybox:latest
Untagged: busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d
Deleted: sha256:64f5d945efcc0f39ab11b3cd4ba403cc9fefe1fa3613123ca016cf3708e8cafb
Deleted: sha256:d1156b98822dccbb924b4e5fe16465a7ecac8bfc81d726177bed403a8e70c972
Untagged: nginx:latest
Untagged: nginx@sha256:e770165fef9e36b990882a4083d8ccf5e29e469a8609bb6b2e3b47d9510e2c8d
Deleted: sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf
Deleted: sha256:50183b8336fcc9552a55c86895cdfdfb6f1bb349a951da638f22f645ce235926
Deleted: sha256:093a0ead7cedbef266292a1b08e478489ed6584170f0d82127c5ac9a10fd8303
Deleted: sha256:6270adb5794c6987109e54af00ab456977c5d5cc6f1bc52c1ce58d32ec0f15f4
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]#
Docker镜像导出导入
导出容器为文件
docker export "name(:tag) | ID" > container.tar
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68014318583f busybox "sh" 24 minutes ago Up 24 minutes busybox
e2420149b614 php:v2 "php-fpm" 4 hours ago Up 4 hours lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 28 hours ago Up 27 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
9e700ee368ca 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
9796a2251876 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
[root@localhost ~]# docker export busybox > busybox.tar
[root@localhost ~]# ll
total 1392
-rw-------. 1 root root 1379 Apr 29 18:57 anaconda-ks.cfg
-rw-r--r-- 1 root root 1420800 May 10 14:50 busybox.tar
drwxr-xr-x 4 root root 33 May 7 09:30 lnmp
[root@localhost ~]# docker export 68014318583f > busybox01.tar
[root@localhost ~]# ll
total 2780
-rw-------. 1 root root 1379 Apr 29 18:57 anaconda-ks.cfg
-rw-r--r-- 1 root root 1420800 May 10 14:53 busybox01.tar
-rw-r--r-- 1 root root 1420800 May 10 14:50 busybox.tar
drwxr-xr-x 4 root root 33 May 7 09:30 lnmp
用import导入export出来的文件为镜像
docker import "CONTAINER:ID"
docker import - "REPOSITORY:TAG" < container.tar 或 cat container.tar | docker import - "REPOSITORY:TAG" #指定镜像名和标签
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v2 0033622fde2f 4 hours ago 527MB
php-56 v2 83996a2a5a02 4 hours ago 527MB
busybox latest 64f5d945efcc 10 hours ago 1.2MB
nginx v1 aa9d402c7fca 28 hours ago 371MB
nginx-112 latest 5a078b28d864 2 days ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 3 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost ~]# docker import busybox.tar
sha256:0c804436c47a89cff5f836776beb759f3ef6583f49a399e4913508f7c3a3cbd0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
0c804436c47a 10 seconds ago 1.2MB
php v2 0033622fde2f 4 hours ago 527MB
php-56 v2 83996a2a5a02 4 hours ago 527MB
busybox latest 64f5d945efcc 10 hours ago 1.2MB
nginx v1 aa9d402c7fca 28 hours ago 371MB
nginx-112 latest 5a078b28d864 2 days ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 3 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost ~]# docker import - busybox:v1 < busybox.tar
sha256:3a8fed956b0d249f92c6ca18535ac2dd7a1f67b3a289a8aa8536e67b03af9dfe
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v1 3a8fed956b0d 3 seconds ago 1.2MB
0c804436c47a 2 minutes ago 1.2MB
php v2 0033622fde2f 4 hours ago 527MB
php-56 v2 83996a2a5a02 4 hours ago 527MB
busybox latest 64f5d945efcc 10 hours ago 1.2MB
nginx v1 aa9d402c7fca 28 hours ago 371MB
nginx-112 latest 5a078b28d864 2 days ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 3 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost ~]# ^C
[root@localhost ~]# cat busybox.tar | docker import - busybox:v2
sha256:43298b31d867db420d54dd5f64f5d0c9b7907d3eb6fa3c6e4199cd1d0c42a68b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v2 43298b31d867 6 seconds ago 1.2MB
busybox v1 3a8fed956b0d 2 minutes ago 1.2MB
0c804436c47a 4 minutes ago 1.2MB
php v2 0033622fde2f 4 hours ago 527MB
php-56 v2 83996a2a5a02 4 hours ago 527MB
busybox latest 64f5d945efcc 10 hours ago 1.2MB
nginx v1 aa9d402c7fca 28 hours ago 371MB
nginx-112 latest 5a078b28d864 2 days ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 3 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
centos 7 9f38484d220f 8 weeks ago 202MB
镜像保存加载
docker save “镜像名:标签 | ID” > image.tar #不指定标签就会把相同名字的都保存出来了,看下面例子的images.tar大小
docker load < image.tar 或 docker load --i image.tar
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v2 43298b31d867 22 minutes ago 1.2MB
busybox v1 3a8fed956b0d 24 minutes ago 1.2MB
0c804436c47a 27 minutes ago 1.2MB
php v2 0033622fde2f 5 hours ago 527MB
php-56 v2 83996a2a5a02 5 hours ago 527MB
busybox latest 64f5d945efcc 10 hours ago 1.2MB
nginx v1 aa9d402c7fca 29 hours ago 371MB
nginx-112 latest 5a078b28d864 2 days ago 371MB
nginx latest 53f3fd8007f7 2 days ago 109MB
php v1 157ea6156082 2 days ago 527MB
php-56 latest 12d6c7e75b6a 3 days ago 527MB
nginx 27a188018e18 3 weeks ago 109MB
centos 7 9f38484d220f 8 weeks ago 202MB
[root@localhost ~]# docker save busybox > image.tar
[root@localhost ~]# ll
total 5572
-rw-------. 1 root root 1379 Apr 29 18:57 anaconda-ks.cfg
-rw-r--r-- 1 root root 1420800 May 10 14:53 busybox01.tar
-rw-r--r-- 1 root root 1420800 May 10 14:50 busybox.tar
-rw-r--r-- 1 root root 2856448 May 10 15:22 image.tar
drwxr-xr-x 4 root root 33 May 7 09:30 lnmp
[root@localhost ~]# docker save busybox:v2 > image.tar
[root@localhost ~]# ll
total 4176
-rw-------. 1 root root 1379 Apr 29 18:57 anaconda-ks.cfg
-rw-r--r-- 1 root root 1420800 May 10 14:53 busybox01.tar
-rw-r--r-- 1 root root 1420800 May 10 14:50 busybox.tar
-rw-r--r-- 1 root root 1428992 May 10 15:23 image.tar
drwxr-xr-x 4 root root 33 May 7 09:30 lnmp
[root@localhost ~]# docker load < image.tar
Loaded image: busybox:v2
[root@localhost ~]# docker load < image.tar
Loaded image: busybox:v2
[root@localhost ~]# docker save busybox:v1 > image.tar
[root@localhost ~]# docker load < image.tar
Loaded image: busybox:v1
[root@localhost ~]# docker load -i image.tar
Loaded image: busybox:v1
注:export导出的镜像会比save保存出来的小,export直接拿容器的,会丢失镜像的历史,也无法回滚之前的镜像层
保存容器为镜像
docker commit "容器ID" "镜像名:ID"
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68014318583f busybox "sh" About an hour ago Up About an hour busybox
e2420149b614 php:v2 "php-fpm" 5 hours ago Up 5 hours lnmp_php
799783479e81 nginx:v1 "nginx -g 'daemon of鈥 29 hours ago Up 28 hours 0.0.0.0:88->80/tcp lnmp_nginx
9b199a9b5836 centos:7 "/bin/bash" 2 days ago Up 2 days centos7
9e700ee368ca 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx05
85b964626fca 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx04
80a2a5e35751 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx03
9796a2251876 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp nginx01
3172a2e970f1 27a188018e18 "nginx -g 'daemon of鈥 9 days ago Up 9 days 80/tcp cocky_chaum
[root@localhost ~]# docker commit 68014318583f busybox:v3
sha256:3874ab49e818daea7861d87c76f7c0533ab4b0f4ea3e8ed5c37f7cd447ffa057
[root@localhost ~]# docker images busybox:v3
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v3 3874ab49e818 2 minutes ago 1.2MB