docker | 打包容器\提交镜像\拉取镜像(私服库)
这里做个人案例演示
1.下载centos7.9.2009镜像
下载镜像作为后续例子演示
# 查看本地镜像
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 2 hours ago 559MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 11 months ago 231MB
# 下载centos7.9.2009
[root@echohye ~]# docker pull centos:7.9.2009
7.9.2009: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7.9.2009
docker.io/library/centos:7.9.2009
# 再次查看本地镜像
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 2 hours ago 559MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos 7.9.2009 eeb6ee3f44bd 11 months ago 204MB
2.运行centos镜像,并下载net-tools库
# 运行并进入容器
[root@echohye ~]# docker run --name=net-test -it eeb6ee3f44bd /bin/bash
# 执行ifconfig命令,可以知道目前系统没有相关指令
[root@df05dbd0f5b7 /]# ifconfig
bash: ifconfig: command not found
# 安装net-tools库
[root@df05dbd0f5b7 /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 153 kB 00:00:01
(2/4): extras/7/x86_64/primary_db | 247 kB 00:00:07
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:07
(4/4): updates/7/x86_64/primary_db | 16 MB 00:00:13
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution
...
Running transaction
Installing : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1
Installed:
net-tools.x86_64 0:2.0-0.25.20131004git.el7
Complete!
# 再次执行ifconfig,发现已经可以运行了
[root@df05dbd0f5b7 /]# 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 11596 bytes 25107054 (23.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9382 bytes 625925 (611.2 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
现在,我们就以这个容器作为例子,将其打包上传至本地docker私有库。
3.下载镜像registry
[root@echohye ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete
0d96da54f60b: Pull complete
5b27040df4a2: Pull complete
e2ead8259a04: Pull complete
3790aef225b9: Pull complete
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 2 hours ago 559MB
registry latest b8604a3fe854 9 months ago 26.2MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos 7.9.2009 eeb6ee3f44bd 11 months ago 204MB
4.运行私有库Registry,相当于本地有个私有Docker hub
[root@echohye ~]# docker run -d -p 5000:5000 -v /echohye/:/var/lib/registry --privileged=true registry
a2c56f5ac7fe0ad6ed5824a378cd60622e05639b4c9f148049e6eecce89b31ce
[root@echohye ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2c56f5ac7fe registry "/entrypoint.sh /etc…" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp goofy_heisenberg
默认情况下,仓库被创建在容器的/var/lib/registry目录下,建议自行用容器卷映射,方便于宿主机联调
5.容器打包成镜像
[root@echohye ~]# docker commit -m="ifconfig cmd add ok" -a="echohye" df05dbd0f5b7 mycentos:1.1
sha256:409b137b25d9c63171fca9bddf0cc4c732a53b1e3f06b69141276daa0602c645
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.1 409b137b25d9 4 seconds ago 381MB
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 2 hours ago 559MB
registry latest b8604a3fe854 9 months ago 26.2MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos 7.9.2009 eeb6ee3f44bd 11 months ago 204MB
centos latest 5d0da3dc9764 11 months ago 231MB
5.curl验证私服库上有什么镜像
[root@echohye ~]# curl -X GET http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}
6.将新镜像mycentos:1.1修改成符号私服规范的Tag
公式:docker tag 镜像:Tag Host:Port/Repository:Tag
[root@echohye ~]# docker tag mycentos:1.1 127.0.0.1:5000/mycentos:1.1
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/mycentos 1.1 409b137b25d9 15 minutes ago 381MB
mycentos 1.1 409b137b25d9 15 minutes ago 381MB
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 3 hours ago 559MB
registry latest b8604a3fe854 9 months ago 26.2MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos 7.9.2009 eeb6ee3f44bd 11 months ago 204MB
centos latest 5d0da3dc9764 11 months ago 231MB
7.更改配置文件,使之支持http
[root@echohye ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://cgoiq7v4.mirror.aliyuncs.com"]
}
[root@echohye ~]# vim /etc/docker/daemon.json
[root@echohye ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://cgoiq7v4.mirror.aliyuncs.com"],
"insecure-registries":["127.0.0.1:5000"]
}
[root@echohye ~]# systemctl restart docker
[root@echohye ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@echohye ~]# docker run -d -p 5000:5000 -v /echohye/:/tmp/registry --privileged=true registry
05b9757555cdb494340fd760a0bfa12203e8d4cb62ecf5215ebb798d2c5292d2
[root@echohye ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05b9757555cd registry "/entrypoint.sh /etc…" 6 seconds ago Up 5 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp hungry_ganguly
8.push推送到私服库
[root@echohye ~]# docker push 127.0.0.1:5000/mycentos:1.1
The push refers to repository [127.0.0.1:5000/mycentos]
f10afec81581: Pushed
174f56854903: Pushed
1.1: digest: sha256:14f79ad79a364acabb42fee033373adc2355cbd7dbcc54be0d45d9b58c136dde size: 741
[root@echohye docker]# curl -X GET http://127.0.0.1:5000/v2/_catalog
{"repositories":["mycentos"]}
删除本地的该镜像
9.pull到本地镜像
[root@echohye docker]# docker pull 127.0.0.1:5000/mycentos:1.1
1.1: Pulling from mycentos
2d473b07cdd5: Already exists
45f728a977da: Pull complete
Digest: sha256:14f79ad79a364acabb42fee033373adc2355cbd7dbcc54be0d45d9b58c136dde
Status: Downloaded newer image for 127.0.0.1:5000/mycentos:1.1
127.0.0.1:5000/mycentos:1.1
[root@echohye docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/mycentos 1.1 409b137b25d9 2 hours ago 381MB
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 5 hours ago 559MB
registry latest b8604a3fe854 9 months ago 26.2MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos 7.9.2009 eeb6ee3f44bd 11 months ago 204MB
参考文章
https://www.cnblogs.com/bowendown/p/12623756.html
https://blog.csdn.net/weixin_45829957/article/details/120318724
https://blog.csdn.net/MLLeo/article/details/117398234