Docker二 | 镜像制作与推送操作

目录

Docker镜像制作

搭建私服

将本地镜像推送到私服

从私服拉取镜像

将本地镜像推送到阿里云

从阿里云拉取镜像


Docker镜像制作

以创建一个新ubuntu镜像,并安装vim命令示例

运行一个ubuntu镜像,发现在镜像里面无法使用vim命令因为该ubuntu镜像只包括了其最基本的内核命令

[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED         SIZE
ubuntu                              latest    ba6acccedd29   2 years ago     72.8MB
[root@localhost ~]# docker run -it ba6acccedd29 /bin/bash
root@f1e088df465f:/# vim a.txt
bash: vim: command not found

给ubuntu容器安装vim

root@f1e088df465f:/# apt-get update
root@f1e088df465f:/# apt-get -y install vim

安装完成之后就可以在容器里面使用vim编辑器进行文件的编辑了

root@f1e088df465f:/# vim a.txt
root@f1e088df465f:/# 
将这个运行的容器制作成一个带有vim功能的ubuntu镜像
docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]
[root@localhost ~]# docker commit -m="add vim" -a="mgaw" f1e088df465f linux1:1.00001
sha256:6eb1515df77a8a00c6ae3ff5c541f26a50fd585a4b67d321280612cef1f852e1
查看镜像,发现比原镜像大了很多
[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
linux1                              1.00001   6eb1515df77a   31 seconds ago   189MB
ubuntu                              latest    ba6acccedd29   2 years ago      72.8MB

运行自己制作的镜像

[root@localhost ~]# docker run -it 6eb1515df77a /bin/bash
root@cc4ba90ce5d4:/# vim a.txt
root@cc4ba90ce5d4:/# 
发现确实带有vim功能了

搭建私服

下载镜像Docker Registry

[root@localhost ~]# docker pull registry

运行私服库Registry,相当于本地的私有Docker hub

[root@localhost ~]# docker run -d -p 5000:5000 -v /mgaw/myregistry/:/tmp/registry --privileged=true registry
18d989f67ba7cab18d1654227bfb8aa4350d19b3e2d9f912302f1b19bc7d852e

将本地镜像推送到私服

以创建一个新ubuntu镜像,并安装ifconfig命令示例

运行一个ubuntu镜像,发现在镜像里面无法使用ifconfig命令因为该ubuntu镜像只包括了其最基本的内核命令

[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED         SIZE
ubuntu                              latest    ba6acccedd29   2 years ago     72.8MB
[root@localhost ~]# docker run -it ba6acccedd29 /bin/bash
root@32766e3fc651:/# ifconfig
bash: ifconfig: command not found

给ubuntu容器安装ifconfig

root@32766e3fc651:/# apt-get update
root@32766e3fc651:/# apt-get install net-tools
root@32766e3fc651:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.7  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:07  txqueuelen 0  (Ethernet)
        RX packets 9126  bytes 30122477 (30.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7207  bytes 395192 (395.1 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

将这个运行的容器制作成一个带有ifconfig功能的ubuntu镜像

[root@localhost ~]# docker commit -m="ifconfig" -a="mgaw" 32766e3fc651 ubuntu1:1.00002
sha256:120ca7640729ad7ca74912b3ca8f9f0dceedf7a798e39b59d122652907dd3a0e

查看镜像,发现比原镜像大了很多

[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
ubuntu1                             1.00002   120ca7640729   47 seconds ago   122MB
ubuntu                              latest    ba6acccedd29   2 years ago      72.8MB
使用 curl 工具验证私服库上有什么镜像
[root@localhost ~]# curl -XGET http://192.168.117.131:5000/v2/_catalog
{"repositories":[]}
目前私服库没有镜像上传过
将新镜像 ubuntu1:1.00002 修改符合私服规范的 Tag
命令格式: docker tag 镜像:Tag Host:Port/Repository:Tag
[root@localhost ~]# docker tag ubuntu1:1.00002 192.168.117.131:5000/ubuntu1:1.00002

查看修改后的镜像

[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
ubuntu1                             1.00002   120ca7640729   5 minutes ago    122MB
192.168.117.131:5000/ubuntu1        1.00002   120ca7640729   5 minutes ago    122MB
ubuntu                              latest    ba6acccedd29   2 years ago      72.8MB
修改 docker 配置文件使之支持 http
vim /etc/docker/daemon.json

新增如下内容

"insecure-registries": ["192.168.117.131:5000"]

最后的结果如下

 重新启动docker,并保证linux防火墙没有对5000端口拦截

[root@localhost ~]# systemctl restart docker
由于重新启动了docker,需要重新启动 docker registry 容器
[root@localhost ~]# docker run -d -p 5000:5000 -v /mgaw/myregistry/:/tmp/registry --privileged=true registry
bbef35a7bd2f80525932769666c320751565f7f01bf095f30bbfe30fc219b564

推送镜像到私服

[root@localhost ~]# docker push 192.168.117.131:5000/ubuntu1:1.00002
The push refers to repository [192.168.117.131:5000/ubuntu1]
48d7d917047c: Pushed 
9f54eef41275: Pushed 
1.00002: digest: sha256:94872dd08e5e7d4c5921cb3581f8e01631f85a81a34e9dea83a28212ffb48593 size: 741

查看私服库上是否存在镜像

[root@localhost ~]# curl -XGET http://192.168.117.131:5000/v2/_catalog
{"repositories":["ubuntu1"]}

至此推送操作完成 

从私服拉取镜像

首先删除192.168.117.131:5000/ubuntu1镜像

[root@localhost ~]# docker rmi -f 120ca7640729
[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
linux1                              1.00001   6eb1515df77a   48 minutes ago   189MB
ubuntu                              latest    ba6acccedd29   2 years ago      72.8MB
[root@localhost ~]# docker pull 192.168.117.131:5000/ubuntu1:1.00002
1.00002: Pulling from ubuntu1
7b1a6ab2e44d: Already exists 
0981e371e319: Pull complete 
Digest: sha256:94872dd08e5e7d4c5921cb3581f8e01631f85a81a34e9dea83a28212ffb48593
Status: Downloaded newer image for 192.168.117.131:5000/ubuntu1:1.00002
192.168.117.131:5000/ubuntu1:1.00002

再次查看镜像,发现镜像成功拉取下来了

[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
192.168.117.131:5000/ubuntu1        1.00002   120ca7640729   19 minutes ago   122MB
linux1                              1.00001   6eb1515df77a   49 minutes ago   189MB
ubuntu                              latest    ba6acccedd29   2 years ago      72.8MB

至此拉取操作完成

将本地镜像推送到阿里云

登录阿里云后进行如下操作

选择管理控制台

选择创建个人版实例

完善镜像仓库

完善仓库信息

注意如下命令

先在Docker下远程登录阿里云

​
[root@localhost ~]# docker login --username=aliyun5258341332 registry.cn-hangzhou.aliyuncs.com
Password: 
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

将要推送的镜像改名

[root@localhost ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED         SIZE
192.168.117.131:5000/ubuntu1        1.00002   120ca7640729   5 days ago      122MB
linux1                              1.00001   6eb1515df77a   5 days ago      189MB
[root@localhost ~]# docker tag 6eb1515df77a registry.cn-hangzhou.aliyuncs.com/awawcr/linux1:1.00001

 查看改名后的镜像

[root@localhost ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED         SIZE
192.168.117.131:5000/ubuntu1                      1.00002   120ca7640729   5 days ago      122MB
linux1                                            1.00001   6eb1515df77a   5 days ago      189MB
registry.cn-hangzhou.aliyuncs.com/awawcr/linux1   1.00001   6eb1515df77a   5 days ago      189MB

推送镜像到阿里云

[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/awawcr/linux1:1.00001
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/awawcr/linux1]
b553c8e0972f: Pushed 
9f54eef41275: Pushed 
1.00001: digest: sha256:09fc3901e5e3094cce97ab89fb6d1afb794df0afa313a7570343c44b25c5a9eb size: 741

查看阿里云发现刚刚推送的镜像

至此推送操作完成 

从阿里云拉取镜像

首先删除之前的镜像

[root@localhost ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED         SIZE
192.168.117.131:5000/ubuntu1                      1.00002   120ca7640729   5 days ago      122MB
linux1                                            1.00001   6eb1515df77a   5 days ago      189MB
registry.cn-hangzhou.aliyuncs.com/awawcr/linux1   1.00001   6eb1515df77a   5 days ago      189MB
[root@localhost ~]# docker rmi -f 6eb1515df77a
Untagged: linux1:1.00001
Untagged: registry.cn-hangzhou.aliyuncs.com/awawcr/linux1:1.00001
Untagged: registry.cn-hangzhou.aliyuncs.com/awawcr/linux1@sha256:09fc3901e5e3094cce97ab89fb6d1afb794df0afa313a7570343c44b25c5a9eb
Deleted: sha256:6eb1515df77a8a00c6ae3ff5c541f26a50fd585a4b67d321280612cef1f852e1
[root@localhost ~]# docker images#发现删除成功
REPOSITORY                          TAG       IMAGE ID       CREATED         SIZE
192.168.117.131:5000/ubuntu1        1.00002   120ca7640729   5 days ago      122MB

拉取镜像

[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/awawcr/linux1:1.00001
1.00001: Pulling from awawcr/linux1
7b1a6ab2e44d: Already exists 
3245ac4203cc: Already exists 
Digest: sha256:09fc3901e5e3094cce97ab89fb6d1afb794df0afa313a7570343c44b25c5a9eb
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/awawcr/linux1:1.00001
registry.cn-hangzhou.aliyuncs.com/awawcr/linux1:1.00001

查看镜像发现拉取成功

[root@localhost ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED         SIZE
192.168.117.131:5000/ubuntu1                      1.00002   120ca7640729   5 days ago      122MB
registry.cn-hangzhou.aliyuncs.com/awawcr/linux1   1.00001   6eb1515df77a   5 days ago      189MB

至此拉取操作完成

  • 16
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值