Docker 镜像管理(私有云&阿里云)

文章目录

  • 一、本地镜像发布到阿里云
    • 1、容器打包成镜像
    • 2、创建阿里云镜像仓库
    • 3、本地镜像发布到阿里云
    • 4、本地镜像发布到阿里云后下载测试
  • 二、本地镜像发布到私有云
    • 1、拉取Docker Registry
    • 2、运行register,作为本地的docker hub
    • 3、准备一个本地镜像
    • 4、curl查看私服镜像
    • 5、修改本地镜像格式
    • 6、修改docker允许http方式推送镜像
    • 7、将本地镜像推送到私服
    • 8、拉取私有云的镜像


一、本地镜像发布到阿里云

1、容器打包成镜像

docker commit -m "add a.txt" -a="alx" a8579a697673 qlx/tomcat-self:1.1
[root@localhost /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS      NAMES
a8579a697673   tomcat    "catalina.sh run"   14 minutes ago   Up 14 minutes   8080/tcp   tomcat1
[root@localhost /]# docker commit -m "add a.txt" -a="alx" a8579a697673 qlx/tomcat-self:1.1
sha256:25fdb18a8210ebc7f562c91c0fc84ed2049af6ab93b32d9daae93ae2402c2448
[root@localhost /]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
qlx/tomcat-self   1.1       25fdb18a8210   7 seconds ago    680MB
tomcat            latest    fb5657adc892   10 months ago    680MB
hello-world       latest    feb5d9fea6a5   13 months ago    13.3kB
[root@localhost /]# docker run -d --name tomcat1-1 qlx/tomcat-self:1.1
708291eec891b0ba2e97ea691f1d216ca9e96db479169cc066b662cec87bef31
[root@localhost /]# docker ps
CONTAINER ID   IMAGE                 COMMAND             CREATED         STATUS         PORTS      NAMES
708291eec891   qlx/tomcat-self:1.1   "catalina.sh run"   4 seconds ago   Up 3 seconds   8080/tcp   tomcat1-1

2、创建阿里云镜像仓库

1、登录到阿里云
阿里云地址

2、控制台 ——>> 镜像容器服务 ——>> 个人实例
创建命名空间设置为公开
在这里插入图片描述

3、创建镜像仓库
Docker 镜像管理(私有云&阿里云)_第1张图片

3、本地镜像发布到阿里云

进入镜像管理界面,按提示推送本地的镜像到阿里云平台
执行管理界面内的命令即可,将镜像ID和版本号替换成自己本地镜像

[root@localhost /]# docker login --username=XXXXXXXXXX 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 tag 25fdb18a8210 registry.cn-hangzhou.aliyuncs.com/milu_qlx/tomcat-self:1.0
[root@localhost /]# docker push registry.cn-hangzhou.aliyuncs.com/milu_qlx/tomcat-self:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/milu_qlx/tomcat-self]
39804b54e3da: Pushed
3e2ed6847c7a: Pushed
bd2befca2f7e: Pushed
59c516e5b6fa: Pushed
3bb5258f46d2: Pushed
832e177bb500: Pushed
f9e18e59a565: Pushed
26a504e63be4: Pushed
8bf42db0de72: Pushed
31892cc314cb: Pushed
11936051f93b: Pushed
1.0: digest: sha256:34bda726fbb58ef54cc0f37741b3ec0282bb5cff1682c881c82120a962f01ce6 size: 2630
[root@localhost /]#

4、本地镜像发布到阿里云后下载测试

docker pull registry.cn-hangzhou.aliyuncs.com/milu_qlx/tomcat-self:[镜像版本号]

二、本地镜像发布到私有云

1 、官方Docker Hub地址:https://hub.docker.com/,中国大陆访问太慢了且准备被阿里云取代的趋势,不太主流。

2、 Dockerhub、阿里云这样的公共镜像仓库可能不太方便,涉及机密的公司不可能提供镜像给公网,所以需要创建一个本地私人仓库供给团队使用,基于公司内部项目构建镜像。

Docker Registry是官方提供的工具,可以用于构建私有镜像仓库

1、拉取Docker Registry

docker pull registry
[root@localhost /]# 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@localhost /]# docker images
REPOSITORY                                               TAG       IMAGE ID       CREATED          SIZE
registry                                                 latest    b8604a3fe854   12 months ago    26.2MB
hello-world                                              latest    feb5d9fea6a5   13 months ago    13.3kB

2、运行register,作为本地的docker hub

docker run -d -p 5000:5000  -v /data/docker-registry/:/tmp/registry --privileged=true registry
[root@localhost /]# docker run -d -p 5000:5000  -v /data/docker-registry/:/tmp/registry --privileged=true registry
ec086f2cfd3f325d3aa7340952c856e2457640b7f96694808c9f84df1f2b455d

[root@localhost /]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                       NAMES
ec086f2cfd3f   registry              "/entrypoint.sh /etc…"   8 seconds ago    Up 7 seconds    0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   interesting_pasteur
708291eec891   qlx/tomcat-self:1.1   "catalina.sh run"        34 minutes ago   Up 34 minutes   8080/tcp                                    tomcat1-1
[root@localhost docker-registry]#

3、准备一个本地镜像

运行一个tomcat容器,进入容器内部安装ifconfig
此处省略启动romcat容器

#进入容器内部

docker exec -it 708291eec891 /bin/bash  

#安装ifconfig工具

apt-get update   

#安装ifconfig工具

apt-get install net-tools  

测试ifconfig是否安装成功

ifconfig

成功后退出当前容器

exit

将容器打包成为镜像
-m: 描述说明
-a:作者author

docker commit -m "add ifconfig" -a="qlx" 708291eec891 qlx/tomcat-ifconfig

整体命令如下:

[root@localhost docker-registry]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                       NAMES
ec086f2cfd3f   registry              "/entrypoint.sh /etc…"   4 minutes ago    Up 4 minutes    0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   interesting_pasteur
708291eec891   qlx/tomcat-self:1.1   "catalina.sh run"        38 minutes ago   Up 38 minutes   8080/tcp                                    tomcat1-1
[root@localhost docker-registry]# docker exec -it 708291eec891 /bin/bash
root@708291eec891:/usr/local/tomcat# apt-get update
Get:1 http://security.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:2 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:3 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [194 kB]
Get:4 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:5 http://deb.debian.org/debian bullseye/main amd64 Packages [8184 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [14.6 kB]
Fetched 8600 kB in 12s (713 kB/s)
Reading package lists... Done
root@708291eec891:/usr/local/tomcat# apt-get install net-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  net-tools
0 upgraded, 1 newly installed, 0 to remove and 54 not upgraded.
Need to get 250 kB of archives.
After this operation, 1015 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 net-tools amd64 1.60+git20181103.0eebece-1 [250 kB]
Fetched 250 kB in 3s (73.4 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 12672 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20181103.0eebece-1_amd64.deb ...
Unpacking net-tools (1.60+git20181103.0eebece-1) ...
Setting up net-tools (1.60+git20181103.0eebece-1) ...
root@708291eec891:/usr/local/tomcat# 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 2091  bytes 8969309 (8.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1704  bytes 93648 (91.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@708291eec891:/usr/local/tomcat# exit
exit
[root@localhost docker-registry]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                       NAMES
ec086f2cfd3f   registry              "/entrypoint.sh /etc…"   6 minutes ago    Up 6 minutes    0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   interesting_pasteur
708291eec891   qlx/tomcat-self:1.1   "catalina.sh run"        41 minutes ago   Up 41 minutes   8080/tcp                                    tomcat1-1
[root@localhost docker-registry]# docker commit -m "add ifconfig" -a="qlx" 708291eec891 qlx/tomcat-ifconfig
sha256:c08104e85072636d1d8a15e24c323c2628427b758903e7946e9631e939b15e7d
[root@localhost docker-registry]# docker images
REPOSITORY                                               TAG       IMAGE ID       CREATED          SIZE
qlx/tomcat-ifconfig                                      latest    c08104e85072   5 seconds ago    700MB
qlx/tomcat-self                                          1.1       25fdb18a8210   43 minutes ago   680MB
registry.cn-hangzhou.aliyuncs.com/milu_qlx/tomcat-self   1.0       25fdb18a8210   43 minutes ago   680MB
tomcat1                                                  latest    9e913bd19141   54 minutes ago   672MB
tomcat                                                   latest    fb5657adc892   10 months ago    680MB
registry                                                 latest    b8604a3fe854   12 months ago    26.2MB
hello-world                                              latest    feb5d9fea6a5   13 months ago    13.3kB
[root@localhost docker-registry]# 

4、curl查看私服镜像

[root@localhost docker-registry]# curl -XGET http://192.168.109.141:5000/v2/_catalog
响应如下:说明私有仓库没有镜像
{"repositories":[]}

5、修改本地镜像格式

将本地镜像格式修改为符合私服镜像的格式
按照公式: docker tag 镜像:Tag Host:Port/Repository:Tag

[root@localhost docker-registry]# docker tag qlx/tomcat-ifconfig 192.168.109.141:5000/qlx/tomcat-ifconfig
[root@localhost docker-registry]# docker images
REPOSITORY                                               TAG       IMAGE ID       CREATED             SIZE
192.168.109.141:5000/qlx/tomcat-ifconfig                 latest    c08104e85072   12 minutes ago      700MB

6、修改docker允许http方式推送镜像

docker默认不允许http方式推送镜像,通过配置选项来取消这个限制。
修改完后如果不生效,建议重启docker

vim /etc/docker/daemon.json

将内容修改为如下配置,新增"insecure-registries": [“192.168.109.141:5000”]

[root@localhost docker-registry]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://ljcd9lt8.mirror.aliyuncs.com"],
  "insecure-registries": ["192.168.109.141:5000"]
}

需要重启docker

systemctl restart docker

重新运行register

docker run -d -p 5000:5000  -v /data/docker-registry/:/tmp/registry --privileged=true registry

7、将本地镜像推送到私服

[root@localhost docker-registry]# docker push 192.168.109.141:5000/qlx/tomcat-ifconfig
Using default tag: latest
The push refers to repository [192.168.109.141:5000/qlx/tomcat-ifconfig]
f3e8bdea284c: Pushed
39804b54e3da: Pushed
3e2ed6847c7a: Pushed
bd2befca2f7e: Pushed
59c516e5b6fa: Pushed
3bb5258f46d2: Pushed
832e177bb500: Pushed
f9e18e59a565: Pushed
26a504e63be4: Pushed
8bf42db0de72: Pushed
31892cc314cb: Pushed
11936051f93b: Pushed
latest: digest: sha256:dc4c7e53b4dea2dd05e1acf4a56e1c8d40d4692c4a5deb3203a8989136434e9f size: 2842

验证是否推送成功

[root@localhost docker-registry]# curl -XGET http://192.168.109.141:5000/v2/_catalog
{"repositories":["qlx/tomcat-ifconfig"]}

8、拉取私有云的镜像

拉取镜像

docker pull 192.168.109.141:5000/qlx/tomcat-ifconfig

启动容器

docker run -it 镜像ID /bin/bash

你可能感兴趣的:(docker,阿里云,容器)