你可以将本文看作一个练习题,也可以看作一篇搭建私有 Docker
镜像仓库的教程。
- 搭建本地镜像仓库
- 构建镜像
- 上传镜像至本地仓库
- 拉取本地镜像仓库的镜像,并运行测试
- 本地搭建镜像仓库
- 拉取最新的官方
ubuntu
镜像,并运行,发现官方镜像中不携带ifconfig
指令- 在官方
ubuntu
镜像运行后的实例容器中,安装ifconfig
指令- 将安装了
ifconfig
指令的容器构建成一个新的镜像,名为myubuntu
- 将
myubuntu
镜像上传至本地搭建的镜像仓库- 删除当前docker中,所有
ubuntu
、myubuntu
镜像及容器- 拉取本地镜像仓库中的
myubuntu
,并运行测试ifconfig
指令
官方提供了一个名为
registry
的镜像,可以用该镜像快速搭建本地镜像仓库。
shdocker pull registry
sh# docker run 启动
docker run -d -p 5000:5000 -v /var/local/docker/registry:/var/lib/registry --restart always --name registry registry
-v /var/local/docker/registry:/var/lib/registry
:将本机/var/local/docker/registry
文件夹与容器/var/lib/registry
做映射
--restart always
:重启docker
时,自动启动该容器
也可以使用如下 docker-compose
的配置运行:
yamlservices:
registry:
#name: registry
image: registry:latest
restart: always
ports:
- 5050:5000
volumes:
- /var/local/docker/registry:/var/lib/registry
sh# 查看本地仓库,为空
curl -XGET http://127.0.0.1:5000/v2/_catalog
# {"repositories":[]}
ubuntu
镜像,并运行为容器:shdocker pull ubuntu
docker run -d --name myubuntu ubuntu bash
docker ps
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 3e314b01d654 ubuntu "bash" 38 seconds ago Up 37 seconds myubuntu
Ubuntu
容器中,额外安装 ifconfig
指令,以此区分新容器与默认容器的区别:sh# 进入刚才启动的容器
docker exec -it 3e314b01d654 bash
# 容器内安装ifconfig命令
apt-get update
apt-get install net-tools
# 测试命令
ifconfig
# 输出
# eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
# inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
# ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
# RX packets 3852 bytes 23648752 (23.6 MB)
# RX errors 0 dropped 0 overruns 0 frame 0
# TX packets 3623 bytes 243470 (243.4 KB)
# 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
# 退出容器
exit
sh# 依据容器 3e314b01d654 创建镜像 myubuntu:1.0
docker commit -m "add ifconfig to myubuntu" -a "ddd" 3e314b01d654 myubuntu:1.0
# 查看镜像
docker images
# 输出,可以看到刚构建的镜像
# REPOSITORY TAG IMAGE ID CREATED SIZE
# myubuntu 1.0 0356315dc26d 4 seconds ago 116MB
# ubuntu latest 2dc39ba059dc 3 weeks ago 77.8MB
127.0.0.1:5000
)shdocker tag myubuntu:1.0 127.0.0.1:5000/myubuntu:1.0
docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# myubuntu 1.0 0356315dc26d 40 minutes ago 116MB
# 127.0.0.1:5000/myubuntu 1.0 0356315dc26d 40 minutes ago 116MB
sh# 第一次尝试推送
docker push 127.0.0.1:5000/myubuntu:1.0
可能报错如下:
bashThe push refers to repository [127.0.0.1:5000/myubuntu]
Get "https://127.0.0.1:5000/v2/": http: server gave HTTP response to HTTPS client
出现该错误时,需要在本机的 daemon.json
增加仓库服务地址配置:
shvim /etc/docker/daemon.json
# 注意,如果以前配置过镜像仓库加速,直接在原配置下加上("insecure-registries":["127.0.0.1:5000"])配置即可
# 并且此处可以配多个,如:"insecure-registries":["127.0.0.1:5000","192.168.0.1:5000"]
# 若原来 daemon.json 这个文件,则直接将将以下信息填入 json 文件保存即可
{
"insecure-registries":["127.0.0.1:5000"]
}
# 修改完成后保存配置,并重启 docker 服务,使配置生效
systemctl restart docker
sh# 再次推送,并成功
docker push 127.0.0.1:5000/myubuntu:1.0
# The push refers to repository [127.0.0.1:5000/myubuntu]
# 25afe370111a: Pushed
# 7f5cbd8cc787: Pushed
# 1.0: digest: sha256:a64348f65424c792501aa59aa6eaa00620209e81b3055af87c7132dc17758d60 size: 741
# 再次查看仓库
curl -XGET http://127.0.0.1:5000/v2/_catalog
{"repositories":["myubuntu"]}
注意:文件路径很长,可以分为两部分
- 前四节
/var/local/docker/registry
,为本机文件夹。可以修改,但要与registry
镜像启动时配置的映射路径匹配- 后四节
/docker/registry/v2/repositories
是直接从本地仓库容器registry
中同步过来的,不可修改
sh# 查看本机目录文件
cd /var/local/docker/registry/docker/registry/v2/repositories
ll
# total 4
# drwxr-xr-x 5 root root 4096 Sep 25 19:35 myubuntu
pwd
# /var/local/docker/registry/docker/registry/v2/repositories
ubuntu
sh# 删除原有镜像,若有容器引用过,记得先删除容器哦,(即使容器已经停了也要先删容器再删镜像)
docker rmi 127.0.0.1:5000/myubuntu:1.0
docker rmi myubuntu:1.0
docker rmi ubuntu:latest
sh
docker pull 127.0.0.1:5000/myubuntu:1.0
# 1.0: Pulling from myubuntu
# 2b55860d4c66: Pull complete
# b72656c01e84: Pull complete
# Digest: sha256:a64348f65424c792501aa59aa6eaa00620209e81b3055af87c7132dc17758d60
# Status: Downloaded newer image for 127.0.0.1:5000/myubuntu:1.0
# 127.0.0.1:5000/myubuntu:1.0
docker run -it --name myubuntu 127.0.0.1:5000/myubuntu:1.0 bash
sh# 进入容器
docker exec -it myubuntu
# 验证命令,确定是咱们上传的容器镜像
ifconfig
# eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
# inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
# ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
# RX packets 6 bytes 516 (516.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
# 退出容器
exit
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!