Docker(三):镜像仓库 - 公共仓库、私有仓库 - commit、tag、push

参考地址:https://blog.csdn.net/weixin_43526371/article/details/125828194

镜像分层 和 UnionFS
docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统就是UnionFS。

UnionFS是一种分层、轻量级并且高性能的文件系统。联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。

docker镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。

特性: 一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。

 

通过为容器添加vim功能来创建一个新的带vim功能的镜像
commit 命令生成镜像到本地

例:ubuntu添加vim功能并导出为新的镜像

$ docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# mysql latest 33037edcac9b 2 days ago 444MB
# nginx latest 41b0e86104ba 3 days ago 142MB
# ubuntu latest 27941809078c 5 weeks ago 77.8MB

$ docker run -it ubuntu /bin/bash
# 进入到容器内

$ vim
# bash: vim: command not found

$ apt-get update # 更新包管理工具
# ··· ···
# Fetched 21.9 MB in 9s (2475 kB/s)
# Reading package lists... Done

$ apt-get -y install vim
# ··· ···

$ vim --version
# VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 18 2022 19:26:30)
# ··· ···

$ Ctrl + p + q # 退出容器

$ docker ps
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# e290dd267fe4 ubuntu "/bin/bash" 8 minutes ago Up 8 minutes laughing_diffie

# 提交生成新镜像到本地 docker commit -m="信息" -a="作者" 容器ID 镜像名:[标签名]
$ docker commit -m="==== add vim ====" -a="ProsperLee" e290dd267fe4 ubuntu_vim:v1

# 测试
$ docker run -it ubuntu_vim:v1 /bin/bash
$ vim --version
# VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 18 2022 19:26:30)
# ··· ···

思考:
可在现有带有vim功能上的镜像基础上在添加其他功能生成新的镜像(形成一层一层的逐渐进行加强镜像)
提交本地镜像到线上仓库
$ docker login -u prosperlee -p ******
# WARNING! Using --password via the CLI is insecure. Use --password-stdin.
# WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
# Configure a credential helper to remove this warning. See
# https://docs.docker.com/engine/reference/commandline/login/#credentials-store
# Login Succeeded

$ docker tag 294189a8321b prosperlee/ubuntu_vim # [:tag]
$ docker push prosperlee/ubuntu_vim # [:tag]
# Using default tag: latest
# The push refers to repository [docker.io/prosperlee/ubuntu_vim]
# 7f2a07241fd7: Pushed
# a790f937a6ae: Mounted from library/ubuntu
# latest: digest: sha256:f4c0537037ad7f9c2ee18fc6e5ca641daf4144e4d766f60afc7b31d918bb4f94 size: 741

$ docker pull prosperlee/ubuntu_vim

将本地镜像推送到私有库上(Docker Registry)
https://hub.docker.com/_/registry

# 拉取镜像
$ docker pull registry

# 运行镜像
$ docker run -d --name=registry -p 8080:5000 registry

# 创建daemon.json配置文件
$ touch /etc/docker/daemon.json

# 添加 { "insecure-registries": ["127.0.0.1:8080"] }
$ vi /etc/docker/daemon.json

# 如果推送不上去可以重启下docker试下 systemctl restart docker

# 查看目前仓库中存在哪些镜像 {"repositories":[]}
$ curl -XGET http://127.0.0.1:8080/v2/_catalog

# 运行ubuntu
$ docker run -it ubuntu /bin/bash

# 更新包管理工具
$ apt-get update

# 安装vim
$ apt-get -y install vim

# 查看当前运行的容器
$ docker ps
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 725b59e0d810 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes wonderful_bohr
# d449838d77cb registry "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:8080->5000/tcp, :::8080->5000/tcp registry
# 在本地创建一个属于自己的独一无二的镜像
$ docker commit -m="==== new ubuntu v1.0.0 ====" -a="ProsperLee" 725b59e0d810 lee/ubuntu:v1.0.0
# sha256:5530e920c230eddfd12665c4e7cf1cf472f002ef27eddc3e89b8a294163833be

# 查看镜像
$ docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# lee/ubuntu v1.0.0 5530e920c230 58 seconds ago 172MB
# ubuntu latest 27941809078c 5 weeks ago 77.8MB
# registry latest 773dbf02e42e 7 weeks ago 24.1MB

# 标记本地镜像,将其归入某一仓库
$ docker tag 5530e920c230 127.0.0.1:8080/lee/ubuntu:v1.0.0

# push到私有库
$ docker push 127.0.0.1:8080/lee/ubuntu:v1.0.0

# 查看目前仓库中存在哪些镜像 {"repositories":["lee/ubuntu"]}
$ curl -XGET http://127.0.0.1:8080/v2/_catalog

# 查看私有库中镜像的tag {"name":"lee/ubuntu","tags":["v1.0.0"]}
$ curl -XGET http://127.0.0.1:8080/v2/lee/ubuntu/tags/list

# 拉取私有库中镜像
$ docker pull 127.0.0.1:8080/lee/ubuntu:v1.0.0

# 查看镜像
$ docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# 127.0.0.1:8080/lee/ubuntu v1.0.0 5530e920c230 5 minutes ago 172MB

# 运行镜像
$ docker run -it 5530e920c230 /bin/bash

# 检查是否存在vim
$ vim --version

posted @ 2023-01-07 10:50  嘻嘻哈哈的人生  阅读(345)  评论(0编辑  收藏  举报