25.1 docker简介
官网 www.docker.com
github https://github.com/docker/docker.github.io
开源的容器引擎,可以让开发者打包应用以及依赖的库,然后发布到任何流行的linux发行版上,移植很方便
由 go 语言编写,基于 apache2.0 协议发布
基于 linux kernel,要想在 win 下运行需要借助一个 vm(虚拟机)来实现
自 2013 年开始,近些年发展迅猛
docker 从1.13x开始,版本分为社区版 ce 和企业版 ee,并且基于年月的时间线形式,当前最新稳定版为 17.09 参考http://blog.csdn.net/chenhaifeng2016/article/details/6806241
Docker和传统的虚拟化比较:
Docker的优势:
启动非常快,秒级实现
资源利用率高,一台高配置服务器可以跑上千个docker容器
更快的交付和部署,一次创建和配置后,可以在任意地方运行
内核级别的虚拟化,不需要额外的hypevisor支持,会有更高的性能和效率
易迁移,平台依赖性不强
Docker核心概念:
镜像,是一个只读的模板,类似于安装系统用到的那个iso文件,我们通过镜像来完成各种应用的部署。
容器,镜像类似于操作系统,而容器类似于虚拟机本身。它可以被启动、开始、停止、删除等操作,每个容器都是相互隔离的。
仓库,存放镜像的一个场所,仓库分为公开仓库和私有仓库。 最大的公开仓库是Docker hub(hub.docker.com),国内公开仓库(dockerpool.com)
25.2 安装docker
1、下载 yum 源,并安装
[root@arslinux-01 ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo [root@arslinux-01 ~]# yum install -y docker-ce
速度比较慢,也可以直接下载rpm包
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
上传到 linux 下并 yum 安装,可以自动解决依赖 yum install -y docker-ce-xxxx.rpm
2、启动 docker
[root@arslinux-01 ~]# systemctl start docker
25.3 镜像管理
下载 centos 镜像
[root@arslinux-01 ~]# docker pull centos
速度比较慢,可以使用 docker 加速器
配置 docker 加速器
[root@arslinux-01 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"] }
说明:这个url为加速器地址,需要去ali云申请
配置完加速器,重启 docker,再次 docker pull centos 会快很多
docker image 查看本地镜像
docker search xxx 搜索镜像,其中 xxx 为关键词
docker tag 镜像名 标签 给镜像打标签(就是更改镜像名称)
[root@arslinux-01 ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 5493 [OK] ansible/centos7-ansible Ansible on Centos7 122 [OK] jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86… 111 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 96 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 59 imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 57 [OK] tutum/centos Simple CentOS docker image with SSH access 44 centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 39 kinogmt/centos-ssh CentOS with SSH 28 [OK] pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 10 guyton/centos6 From official centos6 container with full up… 9 [OK] drecom/centos-ruby centos ruby 6 [OK] pivotaldata/centos Base centos, freshened up a little with a Do… 3 mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK] darksheer/centos Base Centos Image -- Updated hourly 3 [OK] pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 2 pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 2 miko2u/centos6 CentOS6 日本語環境 2 [OK] ovirtguestagent/centos7-atomic The oVirt Guest Agent for Centos 7 Atomic Ho… 2 indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] mcnaughton/centos-base centos base image 1 [OK] pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0 smartentry/centos centos with smartentry 0 [OK] pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
[root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB centos latest 9f38484d220f 4 months ago 202MB
如果想更改上方 TAG 处信息,可以用 : 将 REPOSITORY : TAG 分开
[root@arslinux-01 ~]# docker tag centos test111:190808 [root@arslinux-01 ~]# docker tag centos test111:1234 [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB centos latest 9f38484d220f 4 months ago 202MB test111 1234 9f38484d220f 4 months ago 202MB test111 190808 9f38484d220f 4 months ago 202MB
这里的 TAG 和命令中的 tag 不是一个概念
docker run -itd 镜像名 把镜像启动为容器
docker ps 查看运行的容器
docker ps -a 查看所有容器(包括未运行的)
[root@arslinux-01 ~]# docker run -itd centos 9d06f43b687172d3cd5ab13529645482f2a9d6fad286c0dd82a5783863db2617 [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9d06f43b6871 centos "/bin/bash" 5 seconds ago Up 3 seconds zealous_mestorf [root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9d06f43b6871 centos "/bin/bash" 3 minutes ago Up 3 minutes zealous_mestorf
说明:-i 表示让容器的标准输入打开,-t 表示分配一个伪终端,-d 表示后台启动,要把 -i -t -d 放到镜像名字前面
docker rmi 镜像名 删除指定镜像
[root@arslinux-01 ~]# docker rmi test111 Error: No such image: test111 [root@arslinux-01 ~]# docker rmi test111:1234 Untagged: test111:1234 [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB centos latest 9f38484d220f 4 months ago 202MB test111 190808 9f38484d220f 4 months ago 202MB
其中后面的参数可以是 tag,如果是 tag 时,实际上是删除该 tag。当后面的参数为镜像 ID 时,则会彻底删除整个镜像,所有标签也会一同删除
不加 TAG 删除镜像,实际 TAG 就是 latest ,docker rmi test111 就是 docker rmi test111:latest
25.4 通过容器创建镜像
1、启动容器
[root@arslinux-01 ~]# docker run -itd centos 77fbe7aeaee649478aae095af386e6b7796e96e865bb95f7d673875fdf6767ec [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 77fbe7aeaee6 centos "/bin/bash" 15 seconds ago Up 12 seconds blissful_yonath
2、进入容器 docker exec -it xxxxx bash
[root@arslinux-01 ~]# docker exec -it 77fbe7 bash [root@77fbe7aeaee6 /]# df -h Filesystem Size Used Avail Use% Mounted on overlay 28G 6.1G 22G 22% / tmpfs 64M 0 64M 0% /dev tmpfs 487M 0 487M 0% /sys/fs/cgroup shm 64M 0 64M 0% /dev/shm /dev/sda3 28G 6.1G 22G 22% /etc/hosts tmpfs 487M 0 487M 0% /proc/asound tmpfs 487M 0 487M 0% /proc/acpi tmpfs 487M 0 487M 0% /proc/scsi tmpfs 487M 0 487M 0% /sys/firmware [root@77fbe7aeaee6 /]# free total used free shared buff/cache available Mem: 995896 654636 73140 2620 268120 133448 Swap: 1999868 169472 1830396
说明:其中xxxxx为容器id,这个id可以用docker ps查看,最后面的bash为进入容器后我们要执行的命令,这样就可以打开一个终端
进入到该容器中,我们做一些变更,比如安装一些东西,然后针对这个容器进行创建新的镜像
3、容器中的操作和机器中操作没有区别
[root@77fbe7aeaee6 /]# yum install -y net-tools [root@77fbe7aeaee6 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 3381 bytes 14925255 (14.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2448 bytes 135606 (132.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@77fbe7aeaee6 /]# exit [root@arslinux-01 ~]# ifconfig |grep docker docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:c8ff:fe99:eec1 prefixlen 64 scopeid 0x20<link> ether 02:42:c8:99:ee:c1 txqueuelen 0 (Ethernet) RX packets 2448 bytes 101334 (98.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3373 bytes 14924599 (14.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
容器中的网卡在宿主机上也出现了
每开启一个容器,都会有一个虚拟网卡让容器和虚拟机通信
[root@arslinux-01 ~]# ifconfig |grep veth veth47c82bd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::5014:d2ff:fe64:e486 prefixlen 64 scopeid 0x20<link> ether 52:14:d2:64:e4:86 txqueuelen 0 (Ethernet) RX packets 2448 bytes 135606 (132.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3381 bytes 14925255 (14.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4、将容器做成镜像 docker commit -m "具体操作" -a "操作人" 容器id 新镜像名称
[root@arslinux-01 ~]# docker commit -m "install net-tools" -a "arslinux" 77fbe7aeaee6 centos_with_net sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6 [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_with_net latest 9c213599fd8f 4 seconds ago 314MB centos latest 8f92b9698cb1 3 minutes ago 314MB ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB test111 190808 9f38484d220f 4 months ago 202MB
说明:-m 加一些改动信息,-a 指定作者相关信息 77fbe7aeaee6 这一串为容器id,再后面为新镜像的名字
5、打开新镜像的容器并进入
[root@arslinux-01 ~]# docker run -itd centos_with_net b1778f203633755d801b022e6db3c07d781f010acf4681cfe5f86b7639b18d6f [root@arslinux-01 ~]# docker exec -it b1778f bash [root@b1778f203633 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 8 bytes 656 (656.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@b1778f203633 /]# ping www.baidu.com PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data. 64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=5 ttl=127 time=6.15 ms 64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=6 ttl=127 time=6.53 ms ^C --- www.a.shifen.com ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 14037ms rtt min/avg/max/mdev = 5.900/6.595/7.355/0.532 ms
25.5 通过模板创建镜像
1、下载模板 http://download.openvz.org/template/precreated/
本地下完后上传到虚拟机中
2、cat tar包 | docker import - 镜像名(自定义) 导入镜像
[root@arslinux-01 ~]# cat centos-6-x86-minimal.tar.gz |docker import - centos6 sha256:d0fc4ef11ea3c1cd7039fb595bdb51023d1f01af68d7c58c45b201000e12306e [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6 latest d0fc4ef11ea3 9 seconds ago 512MB centos_with_net latest 9c213599fd8f 53 minutes ago 314MB centos latest 8f92b9698cb1 56 minutes ago 314MB ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB test111 190808 9f38484d220f 4 months ago 202MB
3、启动容器,进入
[root@arslinux-01 ~]# docker run -itd centos6 ced4739b62045d77aa6983589a1e43edeacad2b8ec979b77a47b901131cd637 [root@arslinux-01 ~]# docker exec -it ced4739b62 bash [root@ced4739b6204 /]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m [root@ced4739b6204 /]# uname -a Linux ced4739b6204 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
docker 用的是虚拟机 linux 的内核,哪怕是 centos6 的docker ,用的依然是虚拟机 centos7 的内核
4、docker save -o 保存的文件名 镜像名 导出镜像
[root@arslinux-01 ~]# docker save -o centos7_with_nettool.tar centos_with_net [root@arslinux-01 ~]# du -sh centos7_with_nettool.tar 307Mcento7_with_nettool.tar
5、先删除镜像
[root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ced4739b6204 centos6 "bash" 11 minutes ago Up 11 minutes suspicious_perlman b1778f203633 centos_with_net "/bin/bash" About an hour ago Up About an hour dazzling_bell 77fbe7aeaee6 9f38484d220f "/bin/bash" 2 hours ago Up 2 hours blissful_yonath [root@arslinux-01 ~]# docker rm -f b1778f203633 b1778f203633 [root@arslinux-01 ~]# docker rmi 9c213599fd8f Untagged: centos_with_net:latest Deleted: sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6 [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6 latest d0fc4ef11ea3 12 minutes ago 512MB centos latest 8f92b9698cb1 About an hour ago 314MB ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB test111 190808 9f38484d220f 4 months ago 202MB
6、再用 docker load 载入
docker load imput 镜像包
docker load < 镜像包
以上两种命令皆可
[root@arslinux-01 ~]# docker load < centos7_with_nettool.tar Loaded image: centos_with_net:latest
[root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6 latest d0fc4ef11ea3 16 minutes ago 512MB centos_with_net latest 9c213599fd8f About an hour ago 314MB centos latest 8f92b9698cb1 About an hour ago 314MB ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 4 months ago 202MB test111 190808 9f38484d220f 4 months ago 202MB
25.6 容器管理
docker create -it 镜像名 bash 创建容器
docker start 容器id 启动容器
[root@arslinux-01 ~]# docker create -it centos6 bash 03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534 [root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03cea99df286 centos6 "bash" 8 seconds ago Created hopeful_darwin ced4739b6204 centos6 "bash" 37 minutes ago Up 37 minutes suspicious_perlman 77fbe7aeaee6 9f38484d220f "/bin/bash" 2 hours ago Up 2 hours blissful_yonath 9d06f43b6871 9f38484d220f "/bin/bash" 2 days ago Exited (137) 2 days ago zealous_mestorf
[root@arslinux-01 ~]# docker start 03cea99df286 03cea99df286 [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03cea99df286 centos6 "bash" 29 seconds ago Up 5 seconds hopeful_darwin ced4739b6204 centos6 "bash" 37 minutes ago Up 37 minutes suspicious_perlman 77fbe7aeaee6 9f38484d220f "/bin/bash" 2 hours ago Up 2 hours blissful_yonath
启动容器后,可以使用 docker ps 查看到,有 start 就有 stop,和 restart
docker create 再 docker start 相当于 docker run
docker run -it 镜像名 bash 启动并进入容器
[root@arslinux-01 ~]# docker run -it centos bash
docker run -d 可以让容器在后台运行
例如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"
docker run --name 名称 -itd 镜像 bash 给容器自定义名字
[root@arslinux-01 ~]# docker run --name arsenal_pepe -itd centos6 bash 6b975789ec240520e4316699cfb83a9752a84da8890f62b18085ce759d252152 [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b975789ec24 centos6 "bash" 22 seconds ago Up 21 seconds arsenal_pepe 03cea99df286 centos6 "bash" 27 minutes ago Up 27 minutes hopeful_darwin ced4739b6204 centos6 "bash" About an hour ago Up About an hour suspicious_perlman 77fbe7aeaee6 centos "/bin/bash" 2 hours ago Up 2 hours blissful_yonath [root@arslinux-01 ~]# docker exec -it arsenal_pepe bash [root@6b975789ec24 /]#
docker run --rm -it 容器名 bash -c "命令" 命令执行完容器就会退出
[root@arslinux-01 ~]# docker run --rm -it centos bash -c "sleep 15"
--rm 可以让容器退出后直接删除,在这里命令执行完容器就会退出
docker logs 容器id 查看容器运行历史信息
[root@arslinux-01 ~]# docker run -itd centos bash -c "echo 123" 066f53f67367fe0da907c26d7abd2d0c95acff40eaf2a0203fba556787f4ab83 [root@arslinux-01 ~]# docker logs 066f53f67367 123
docker attach 容器id 进入后台运行的容器
docker exec -it 容器id bash 临时打开一个虚拟终端,exit 后,容器依然运行着
attach命令不算好用,比如我们想要退出终端,就得exit了,这样容器也就退出了,用 exec 比较
docker rm 容器id 删除容器
[root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 066f53f67367 centos "bash -c 'echo 123'" 5 minutes ago Exited (0) 5 minutes ago sharp_liskov 6b975789ec24 centos6 "bash" 18 minutes ago Up 18 minutes arsenal_pepe 944bc4839130 centos "bash" 30 minutes ago Exited (127) 30 minutes ago nervous_sammet 37366b75bd90 centos "/bin/bash" 30 minutes ago Exited (127) 30 minutes ago inspiring_saha a80eed529a34 centos "bash" 31 minutes ago Exited (0) 30 minutes ago hungry_elgamal 32da41e087c2 centos "bash" 32 minutes ago Exited (0) 32 minutes ago infallible_tereshkova 985de0f4f889 centos "bash" 33 minutes ago Exited (0) 33 minutes ago nifty_nightingale d55e81eae6aa centos "/bin/bash" 35 minutes ago Exited (0) 33 minutes ago condescending_varahamihira 02bccad25efb centos "/bin/bash" 38 minutes ago Exited (0) 37 minutes ago heuristic_wu 03cea99df286 centos6 "bash" 46 minutes ago Up 45 minutes hopeful_darwin ced4739b6204 centos6 "bash" About an hour ago Up About an hour suspicious_perlman 77fbe7aeaee6 centos "/bin/bash" 3 hours ago Up 3 hours blissful_yonath 9d06f43b6871 centos "/bin/bash" 2 days ago Exited (137) 2 days ago zealous_mestorf
[root@arslinux-01 ~]# docker rm 066f53f67367 066f53f67367 [root@arslinux-01 ~]# docker rm 944bc4839130 944bc4839130 [root@arslinux-01 ~]# docker rm 37366b75bd90 37366b75bd90 [root@arslinux-01 ~]# docker rm a80eed529a34 a80eed529a34 [root@arslinux-01 ~]# docker rm 32da41e087c2 32da41e087c2 [root@arslinux-01 ~]# docker rm 985de0f4f889 985de0f4f889
[root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b975789ec24 centos6 "bash" 19 minutes ago Up 19 minutes arsenal_pepe d55e81eae6aa centos "/bin/bash" 36 minutes ago Exited (0) 34 minutes ago condescending_varahamihira 02bccad25efb centos "/bin/bash" 40 minutes ago Exited (0) 38 minutes ago heuristic_wu 03cea99df286 centos6 "bash" 47 minutes ago Up 46 minutes hopeful_darwin ced4739b6204 centos6 "bash" About an hour ago Up About an hour suspicious_perlman 77fbe7aeaee6 centos "/bin/bash" 3 hours ago Up 3 hours blissful_yonath 9d06f43b6871 centos "/bin/bash" 2 days ago Exited (137) 2 days ago zealous_mestor
——对于启动状态的容器,删除需要加 -f 强行删除
[root@arslinux-01 ~]# docker rm 03cea99df286 Error response from daemon: You cannot remove a running container 03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534. Stop the container before attempting removal or force remove [root@arslinux-01 ~]# docker rm -f 03cea99df286 03cea99df286 [root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b975789ec24 centos6 "bash" 21 minutes ago Up 21 minutes arsenal_pepe d55e81eae6aa centos "/bin/bash" 38 minutes ago Exited (0) 36 minutes ago condescending_varahamihira 02bccad25efb centos "/bin/bash" 41 minutes ago Exited (0) 40 minutes ago heuristic_wu ced4739b6204 centos6 "bash" About an hour ago Up About an hour suspicious_perlman 77fbe7aeaee6 centos "/bin/bash" 3 hours ago Up 3 hours blissful_yonath 9d06f43b6871 centos "/bin/bash" 2 days ago Exited (137) 2 days ago zealous_mestor
docker export 容器id > 文件.tar 导出容器
cat 文件.tar |docker import - 镜像名 生成镜像
[root@arslinux-01 ~]# docker export 9d06f43b6871 > centos.tar
[root@arslinux-01 ~]# cat centos.tar |docker import - centos111
sha256:48f71221434353e6f02227794e1a95fcc8ae5f0dc9608142febc957dd124dba0
25.7 仓库管理
docker start 启动未启动的容器(重启docker服务后,容器会退出)
[root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b975789ec24 centos6 "bash" 27 hours ago Exited (137) 22 hours ago arsenal_pepe d55e81eae6aa centos "/bin/bash" 28 hours ago Exited (0) 28 hours ago condescending_varahamihira 02bccad25efb centos "/bin/bash" 28 hours ago Exited (0) 28 hours ago heuristic_wu ced4739b6204 centos6 "bash" 29 hours ago Exited (137) 22 hours ago suspicious_perlman 77fbe7aeaee6 centos "/bin/bash" 30 hours ago Exited (137) 22 hours ago blissful_yonath 9d06f43b6871 centos "/bin/bash" 3 days ago Exited (137) 3 days ago zealous_mestorf [root@arslinux-01 ~]# docker start 6b975789ec24 6b975789ec24 [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b975789ec24 centos6 "bash" 27 hours ago Up 1 second arsenal_pepe
1、利用 registry 创建私有仓库
1)下载 registry 镜像
[root@arslinux-01 ~]# docker pull registry
2)启动 registry 容器
docker run -d -p 宿主机端口号:容器端口号 registry 启动 registry 容器
[root@arslinux-01 ~]# docker run -d -p 5000:5000 registry 3f51ff27501d3f81a29996cc04b8a16ae9b85a9c5b03f4876d13f16fc53e2315 [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f51ff27501d registry "/entrypoint.sh /etc…" 9 seconds ago Up 7 seconds 0.0.0.0:5000->5000/tcp quizzical_chandrasekhar 6b975789ec24 centos6 "bash" 28 hours ago Up 14 minutes arsenal_pepe
说明:以registry镜像启动容器,-p会把容器的端口映射到宿主机上,:左边为宿主机监听端口,:右边为容器监听端口
3)查看 registry 仓库内容
curl 127.0.0.1:5000/v2/_catalog 查看 registry 仓库内容
[root@arslinux-01 ~]# curl 127.0.0.1:5000/v2/_catalog {"repositories":[]}
2、上传镜像到私有仓库
1)标记镜像
docker tag 要标记的镜像 ip:端口/镜像 标记tag,必须带私有仓库 ip:端口
[root@arslinux-01 ~]# docker tag centos6 192.168.194.130:5000/centos6 [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.194.130:5000/centos6 latest d0fc4ef11ea3 29 hours ago 512MB centos6 latest d0fc4ef11ea3 29 hours ago 512MB centos_with_net latest 9c213599fd8f 30 hours ago 314MB ubuntu latest 3556258649b2 2 weeks ago 64.2MB arslinux latest 9f38484d220f 5 months ago 202MB centos latest 9f38484d220f 5 months ago 202MB test111 190808 9f38484d220f 5 months ago 202MB registry latest f32a97de94e1 5 months ago 25.8MB
2)上传镜像
docker push ip:端口/镜像 上传镜像到 registry
[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6 The push refers to repository [192.168.194.130:5000/centos6] Get https://192.168.194.130:5000/v2/: http: server gave HTTP response to HTTPS client
3)指定私有仓库地址
以上报错需要修改配置文件 /etc/docker/daemon.json,在 { } 中加一行内容(如果原本有内容需用逗号 , 隔开)
[root@arslinux-01 ~]# vim /etc/docker/daemon.json { "insecure-registries":["192.168.194.130:5000"] }
4)重启 docker、镜像
[root@arslinux-01 ~]# systemctl restart docker [root@arslinux-01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f51ff27501d registry "/entrypoint.sh /etc…" 31 minutes ago Exited (2) 2 minutes ago quizzical_chandrasekhar 6b975789ec24 centos6 "bash" 28 hours ago Exited (137) 4 minutes ago arsenal_pepe d55e81eae6aa centos "/bin/bash" 28 hours ago Exited (0) 28 hours ago condescending_varahamihira 02bccad25efb centos "/bin/bash" 29 hours ago Exited (0) 29 hours ago heuristic_wu ced4739b6204 centos6 "bash" 29 hours ago Exited (137) 23 hours ago suspicious_perlman 77fbe7aeaee6 centos "/bin/bash" 31 hours ago Exited (137) 23 hours ago blissful_yonath 9d06f43b6871 centos "/bin/bash" 4 days ago Exited (137) 4 days ago zealous_mestorf [root@arslinux-01 ~]# docker start 3f51ff27501d 3f51ff27501d [root@arslinux-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f51ff27501d registry "/entrypoint.sh /etc…" 31 minutes ago Up 3 seconds 0.0.0.0:5000->5000/tcp quizzical_chandrasekhar
5)重新上传
[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6 The push refers to repository [192.168.194.130:5000/centos6] 0a2f11f7b1ef: Pushed latest: digest: sha256:5c0d4992624653abc8764aec2409903be43105246fffd961fa69c3adbc78f0be size: 529
6)查看仓库内容 curl
[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog {"repositories":["centos6"]}
7)再用以上方法上传 ubuntu
[root@arslinux-01 ~]# docker tag ubuntu 192.168.194.130:5000/ubuntu [root@arslinux-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.194.130:5000/centos6 latest d0fc4ef11ea3 29 hours ago 512MB centos6 latest d0fc4ef11ea3 29 hours ago 512MB centos_with_net latest 9c213599fd8f 30 hours ago 314MB 192.168.194.130:5000/ubuntu latest 3556258649b2 2 weeks ago 64.2MB ubuntu latest 3556258649b2 2 weeks ago 64.2MB centos latest 9f38484d220f 5 months ago 202MB test111 190808 9f38484d220f 5 months ago 202MB arslinux latest 9f38484d220f 5 months ago 202MB registry latest f32a97de94e1 5 months ago 25.8MB [root@arslinux-01 ~]# docker push 192.168.194.130:5000/ubuntu The push refers to repository [192.168.194.130:5000/ubuntu] b079b3fa8d1b: Pushed a31dbd3063d7: Pushed c56e09e1bd18: Pushed 543791078bdb: Pushed latest: digest: sha256:d91842ef309155b85a9e5c59566719308fab816b40d376809c39cf1cf4de3c6a size: 1152 [root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog {"repositories":["centos6","ubuntu"]}
未完待续