Skip to content

Latest commit

 

History

History
392 lines (293 loc) · 18.3 KB

20230710_01.md

File metadata and controls

392 lines (293 loc) · 18.3 KB

配置阿里云 Docker 镜像加速服务, 以及如何查找docker hub官方镜像tags 信息

作者

digoal

日期

2023-07-10

标签

PostgreSQL , PolarDB , docker , 加速 , 镜像 , hub , official images , tag


背景

docker在生产和学习领域都是不错的选择, 可以大幅度降低部署门槛, 避免大家把时间浪费在部署上面. 例如自制PolarDB或PostgreSQL开源数据库docker镜像, 将常用插件打包进来, 便于学习、交流、做实验.

docker hub网络不太稳定, 可以用阿里云的个人免费加速镜像服务来解决.

配置阿里云 Docker 镜像加速服务例子

1、申请阿里云个人账号, 略.

2、登陆控制台, 获得docker镜像加速地址, 具体操作参考手册

https://help.aliyun.com/document_detail/60750.html

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

ACR会为每一个账号(阿里云账号或RAM用户)生成一个镜像加速器地址,配置镜像加速器前,您需要获取镜像加速器地址. 加速器地址:[系统分配前缀].mirror.aliyuncs.com

会得到类似这样的地址: https://xxxxxxxx.mirror.aliyuncs.com

3、配置docker desktop on macos, 将docker registry mirror加入配置文件

启动docker desktop - 进入preferences - 进入docker engine - 修改配置

{  
  "builder": {  
    "gc": {  
      "defaultKeepStorage": "20GB",  
      "enabled": true  
    }  
  },  
  "experimental": false,  
  "features": {  
    "buildkit": true  
  }  
}  

改成. 注意aliyun的请改成你自己的个人docker加速地址.

{  
  "builder": {  
    "gc": {  
      "defaultKeepStorage": "20GB",  
      "enabled": true  
    }  
  },  
  "experimental": false,  
  "features": {  
    "buildkit": true  
  },  
  "registry-mirrors": [  
    "https://xxxxxxxx.mirror.aliyuncs.com",  
    "https://hub-mirror.c.163.com"  
  ]  
}  

点击 apply&restart

4、确认配置是否生效

IT-C02YW2EFLVDL:~ digoal$ docker info  
...  
  
 Registry Mirrors:  
  https://xxxxxxxx.mirror.aliyuncs.com/  
  https://hub-mirror.c.163.com  
...  

5、使用mirror拉取镜像注意事项:

我拉取镜像的 latest 版本,得到的是 5 个月前的版本,而 docker hub 上面明明显示最新版本是 14 天前。之前单独用阿里云的加速就有这个问题.

缓存问题,国内缓存的旧的,可以用指定tag的方式拉取,避免出现这种情况.

例如:
https://github.com/docker-library/official-images/blob/master/library/debian

# bullseye -- Debian 11.7 Released 29 April 2023
Tags: bullseye, bullseye-20230703, 11.7, 11
Architectures: amd64, arm32v5, arm32v7, arm64v8, i386, mips64le, ppc64le, s390x
Directory: bullseye

debian:bullseye拉取到的是18 months ago的缓存.

IT-C02YW2EFLVDL:~ digoal$ docker images
REPOSITORY   TAG             IMAGE ID       CREATED         SIZE
postgres     15.3-bullseye   68ca5f232776   6 days ago      379MB
debian       bullseye        6f4986d78878   18 months ago   124MB

IT-C02YW2EFLVDL:~ digoal$ docker rmi debian:bullseye
Untagged: debian:bullseye
Untagged: debian@sha256:2906804d2a64e8a13a434a1a127fe3f6a28bf7cf3696be4223b06276f32f1f2d
Deleted: sha256:6f4986d78878699c680b97e3d7a2fd131bf4def477f1abad9a9edcb5d99cda3c
Deleted: sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce

debian:bullseye-20230703拉取到最近的镜像.

IT-C02YW2EFLVDL:~ digoal$ docker pull debian:bullseye-20230703
bullseye-20230703: Pulling from library/debian
34df401c391c: Pull complete 
Digest: sha256:a648e10e02af129706b1fb89e1ac9694ae3db7f2b8439aa906321e68cc281bc0
Status: Downloaded newer image for debian:bullseye-20230703
docker.io/library/debian:bullseye-20230703

IT-C02YW2EFLVDL:~ digoal$ docker images
REPOSITORY   TAG                 IMAGE ID       CREATED      SIZE
postgres     15.3-bullseye       68ca5f232776   6 days ago   379MB
debian       bullseye-20230703   189a2f977ff1   6 days ago   124MB

6、获得私有registry的catalog, tag list等.

API参考:

https://docs.docker.com/registry/spec/api/#get-tags

GET /v2/_catalog  

https://docs.docker.com/registry/spec/api/#get-tags

GET /v2/<name>/tags/list  
Host: <registry host>  
Authorization: <scheme> <token>  

例子:

curl -X GET https://xxxxxxxx.mirror.aliyuncs.com/v2/_catalog  
  
curl -X GET https://xxxxxxxx.mirror.aliyuncs.com/v2/debian/tags/list  
IT-C02YW2EFLVDL:~ digoal$ curl -X GET https://xxxxxxxx.mirror.aliyuncs.com/v2/_catalog  
{"repositories":["000019950000/ubu3","000019950000/ubuntuzephyrdev","000393287/weather2","0006021998/projectz-ui","000666/restwithaspnetudemy-api","000754796/weather2","000763141/weather2","000765293/weather2","000766490/weather2","000769495/weather2","000769837/weather2","000770004/weather2","000771660/weather2","000772836/weather2","000773374/weather2","000780393/weather2","000a/libvirtkvm-scripts","001466/kali-metasploit","0070707007/daz","00718/androiddm-astore-slim-dev","00718/androiddm-javaenv-slim-dev","00718/androiddm-mosquitto-dev","00718/androiddm-novnc-dev","007594/haproxy-wi","0079123/flume","0079123/redis","007ba7/dr-elastic","007ba7/dr-elastic-bible","007backups/p7zip","007mhh/busybox","007mhh/cni","007mhh/composer","007mhh/elasticsearch","007mhh/filebeat","007mhh/heketi","007mhh/kibana","007mhh/maven","007mhh/node","007mhh/php-fpm","007mhh/registry","007musa/getting-started","00arthur00/kind","00arthur00/openresty","00arthur00/webhookprinter","010010/php","01010011/tizen","01011001e/app_hello","010120190101/hotelservice","010120190101/hotelservice2","010120190101/travel-planner-ui","010309/biaums","010309/host-nginx","010309/mmmiddleware","010309/pbxrecording","010502/node-alpine-apicalcimov","010612/hit-application","01109559275/buscadoronline-back","01109559275/mpnc-front","011184/docker-wordpress-nginx-ssh","011545313/starbucks","0116736/mysql","0116736/zookeeper","01207189428/alpinegit","01369695/centos-vim-new","0157/nginx-imgs","01720/ruoxi-jdk1.8","01822531439/node-api","0192654/gitlab","0192654/kube","0192654/test","01e9/defacto.md","01e9/ide","01e9/parereata.md","01fb16ecs482/deployer","01shreeram/phpmyadmin","01streams/firstimage","01techray/dockerfile-example","01tonythomas/alpine_osagent","02021024/latplan_ubuntu18","020387010/ta_python","0215/crontab","0215/crontab-server","02164211/pythonlearn","021982/155_138","021982/66_42_53_57","021982/66_42_93_164","021982/xmrig","03004968719/ms-auth","03004968719/multi-client","03004968719/multi-server","03004968719/multi-worker","03004968719/vidly-client","03004968719/vidly-server","0303/testrepo","0303ff/kali","0312birdzhang/filebate-kubernetes","0312birdzhang/helloworld","0312birdzhang/kubernetes-dashboard-amd64","03140729/tomcat01","03157721671/comments"]}  

只能查找在registry mirror中的repo:

IT-C02YW2EFLVDL:~ digoal$ curl -s https://xxxxxxxx.mirror.aliyuncs.com/v2/000393287/weather2/tags/list  
{"name":"000393287/weather2","tags":["latest"]}  

从docker镜像加速地址获得的catalog和tag都是加速器本地的信息, 不是docker hub的信息, 所以如果你想获得例如debian, ubuntu, postgresql在docker hub的信息, 恐怕通过registry mirror行不通.

直接显示没有debian这个repo  
  
IT-C02YW2EFLVDL:~ digoal$ curl -s https://xxxxxxxx.mirror.aliyuncs.com/v2/debian/tags/list  
{"errors":[{"code":"NAME_UNKNOWN","message":"repository name not known to registry","detail":{"name":"debian"}}]}  

7、在docker hub无法访问的情况下, 可以在docker official images的github中找到docker官方镜像的tags信息.

https://github.com/docker-library/official-images/blob/master/library/debian

也就是标记了OFFICIAL的镜像

IT-C02YW2EFLVDL:~ digoal$ docker search postgresql  
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED  
postgres                               The PostgreSQL object-relational database sy…   12404     [OK]         
bitnami/postgresql                     Bitnami PostgreSQL Docker Image                 223                  [OK]  
...  
  
IT-C02YW2EFLVDL:~ digoal$ docker search debian  
NAME                                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED  
ubuntu                                Ubuntu is a Debian-based Linux operating sys…   16143     [OK]         
debian                                Debian is a Linux distribution that's compos…   4715      [OK]         
neurodebian                           NeuroDebian provides neuroscience research s…   102       [OK]    

8、常见dockerfile

https://github.com/ApsaraDB/PolarDB-ImageBuilder

https://github.com/docker-library/postgres

9、pull 容器测试

从official 镜像信息了解到debian和postgres的tag信息.

https://github.com/docker-library/official-images/blob/master/library/debian
https://github.com/docker-library/official-images/blob/master/library/postgres

# bullseye -- Debian 11.7 Released 29 April 2023  
Tags: bullseye, bullseye-20230703, 11.7, 11  
Architectures: amd64, arm32v5, arm32v7, arm64v8, i386, mips64le, ppc64le, s390x  
Directory: bullseye  
  
  
Tags: 15.3-bullseye, 15-bullseye, bullseye  
Architectures: amd64, arm32v5, arm32v7, arm64v8, i386, mips64le, ppc64le, s390x  
GitCommit: a23c0e97980edae5be2cd4eb68ff1f0762d031cd  
Directory: 15/bullseye  

基于tag拉取镜像

IT-C02YW2EFLVDL:~ digoal$ docker pull debian:bullseye  
bullseye: Pulling from library/debian  
0e29546d541c: Pull complete   
Digest: sha256:2906804d2a64e8a13a434a1a127fe3f6a28bf7cf3696be4223b06276f32f1f2d  
Status: Downloaded newer image for debian:bullseye  
docker.io/library/debian:bullseye  
  
  
  
IT-C02YW2EFLVDL:~ digoal$ docker pull postgres:15.3-bullseye  
15.3-bullseye: Pulling from library/postgres  
9d21b12d5fab: Pull complete   
ec5fc10339ab: Pull complete   
3264721a5f26: Pull complete   
1fdf08161ac8: Pull complete   
4cb15a94fccd: Pull complete   
03abbfe9b1d1: Pull complete   
ac7037be39d6: Pull complete   
ed8446f22be5: Pull complete   
e3c8662eead5: Pull complete   
db67de0965a3: Pull complete   
5a81ceb7df55: Pull complete   
90bed52e1b96: Pull complete   
babe394d6a8d: Pull complete   
Digest: sha256:cca29368d8d12e1825afd3c38cde6d5268cec3097719f06f2020870ea2bc6993  
Status: Downloaded newer image for postgres:15.3-bullseye  
docker.io/library/postgres:15.3-bullseye  

镜像如下:

IT-C02YW2EFLVDL:~ digoal$ docker images  
docREPOSITORY   TAG             IMAGE ID       CREATED         SIZE  
postgres     15.3-bullseye   68ca5f232776   6 days ago      379MB  
debian       bullseye        6f4986d78878   18 months ago   124MB  
  
IT-C02YW2EFLVDL:~ digoal$ docker system df  
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE  
Images          2         0         503MB     503MB (100%)  
Containers      0         0         0B        0B  
Local Volumes   47        0         293.1MB   293.1MB (100%)  
Build Cache     0         0         0B        0B  

启动容器

IT-C02YW2EFLVDL:~ digoal$ docker run -d --name pg -e "POSTGRES_PASSWORD=123" -e "POSTGRES_HOST_AUTH_METHOD=trust" postgres:15.3-bullseye  
144519fb6885733c8714748e66d22f207893d904da90d7f6006f8501e06a8c05  
  
  
IT-C02YW2EFLVDL:~ digoal$ docker ps -a  
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS        PORTS      NAMES  
144519fb6885   postgres:15.3-bullseye   "docker-entrypoint.s…"   2 seconds ago   Up 1 second   5432/tcp   pg  

进入容器

IT-C02YW2EFLVDL:~ digoal$ docker exec -ti pg /bin/bash  
root@144519fb6885:/# df -h  
Filesystem      Size  Used Avail Use% Mounted on  
overlay          59G  2.4G   54G   5% /  
tmpfs            64M     0   64M   0% /dev  
shm              64M  1.1M   63M   2% /dev/shm  
/dev/vda1        59G  2.4G   54G   5% /etc/hosts  
tmpfs           3.9G     0  3.9G   0% /proc/acpi  
tmpfs           3.9G     0  3.9G   0% /sys/firmware  
  
  
  
root@144519fb6885:/# su - postgres  
postgres@144519fb6885:~$ pwd  
/var/lib/postgresql  
postgres@144519fb6885:~$ ls  
data  
postgres@144519fb6885:~$ cd data  
postgres@144519fb6885:~/data$ ls -la  
total 136  
drwx------ 19 postgres postgres  4096 Jul 10 03:13 .  
drwxr-xr-x  1 postgres postgres  4096 Jul  4 02:14 ..  
-rw-------  1 postgres postgres     3 Jul 10 03:13 PG_VERSION  
drwx------  5 postgres postgres  4096 Jul 10 03:13 base  
drwx------  2 postgres postgres  4096 Jul 10 03:13 global  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_commit_ts  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_dynshmem  
-rw-------  1 postgres postgres  4917 Jul 10 03:13 pg_hba.conf  
-rw-------  1 postgres postgres  1636 Jul 10 03:13 pg_ident.conf  
drwx------  4 postgres postgres  4096 Jul 10 03:13 pg_logical  
drwx------  4 postgres postgres  4096 Jul 10 03:13 pg_multixact  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_notify  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_replslot  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_serial  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_snapshots  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_stat  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_stat_tmp  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_subtrans  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_tblspc  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_twophase  
drwx------  3 postgres postgres  4096 Jul 10 03:13 pg_wal  
drwx------  2 postgres postgres  4096 Jul 10 03:13 pg_xact  
-rw-------  1 postgres postgres    88 Jul 10 03:13 postgresql.auto.conf  
-rw-------  1 postgres postgres 29525 Jul 10 03:13 postgresql.conf  
-rw-------  1 postgres postgres    36 Jul 10 03:13 postmaster.opts  
-rw-------  1 postgres postgres    94 Jul 10 03:13 postmaster.pid  
postgres@144519fb6885:~/data$ du -sh  
39M	.  
postgres@144519fb6885:~/data$ psql  
psql (15.3 (Debian 15.3-1.pgdg110+1))  
Type "help" for help.  
  
postgres=# \dt  
Did not find any relations.  
postgres=# \q  
postgres@144519fb6885:~/data$ pg_config  
BINDIR = /usr/lib/postgresql/15/bin  
DOCDIR = /usr/share/doc/postgresql-doc-15  
HTMLDIR = /usr/share/doc/postgresql-doc-15  
INCLUDEDIR = /usr/include/postgresql  
PKGINCLUDEDIR = /usr/include/postgresql  
INCLUDEDIR-SERVER = /usr/include/postgresql/15/server  
LIBDIR = /usr/lib/x86_64-linux-gnu  
PKGLIBDIR = /usr/lib/postgresql/15/lib  
LOCALEDIR = /usr/share/locale  
MANDIR = /usr/share/postgresql/15/man  
SHAREDIR = /usr/share/postgresql/15  
SYSCONFDIR = /etc/postgresql-common  
PGXS = /usr/lib/postgresql/15/lib/pgxs/src/makefiles/pgxs.mk  
CONFIGURE =  '--build=x86_64-linux-gnu' '--prefix=/usr' '--includedir=${prefix}/include' '--mandir=${prefix}/share/man' '--infodir=${prefix}/share/info' '--sysconfdir=/etc' '--localstatedir=/var' '--disable-option-checking' '--disable-silent-rules' '--libdir=${prefix}/lib/x86_64-linux-gnu' '--runstatedir=/run' '--disable-maintainer-mode' '--disable-dependency-tracking' '--with-tcl' '--with-perl' '--with-python' '--with-pam' '--with-openssl' '--with-libxml' '--with-libxslt' '--mandir=/usr/share/postgresql/15/man' '--docdir=/usr/share/doc/postgresql-doc-15' '--sysconfdir=/etc/postgresql-common' '--datarootdir=/usr/share/' '--datadir=/usr/share/postgresql/15' '--bindir=/usr/lib/postgresql/15/bin' '--libdir=/usr/lib/x86_64-linux-gnu/' '--libexecdir=/usr/lib/postgresql/' '--includedir=/usr/include/postgresql/' '--with-extra-version= (Debian 15.3-1.pgdg110+1)' '--enable-nls' '--enable-thread-safety' '--enable-debug' '--enable-dtrace' '--disable-rpath' '--with-uuid=e2fs' '--with-gnu-ld' '--with-gssapi' '--with-ldap' '--with-pgport=5432' '--with-system-tzdata=/usr/share/zoneinfo' 'AWK=mawk' 'MKDIR_P=/bin/mkdir -p' 'PROVE=/usr/bin/prove' 'PYTHON=/usr/bin/python3' 'TAR=/bin/tar' 'XSLTPROC=xsltproc --nonet' 'CFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer' 'LDFLAGS=-Wl,-z,relro -Wl,-z,now' '--enable-tap-tests' '--with-icu' '--with-llvm' 'LLVM_CONFIG=/usr/bin/llvm-config-11' 'CLANG=/usr/bin/clang-11' '--with-lz4' '--with-zstd' '--with-systemd' '--with-selinux' 'build_alias=x86_64-linux-gnu' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' 'CXXFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security'  
CC = gcc  
CPPFLAGS = -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer  
CFLAGS_SL = -fPIC  
LDFLAGS = -Wl,-z,relro -Wl,-z,now -L/usr/lib/llvm-11/lib -Wl,--as-needed  
LDFLAGS_EX =   
LDFLAGS_SL =   
LIBS = -lpgcommon -lpgport -lselinux -lzstd -llz4 -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lpthread -lrt -ldl -lm   
VERSION = PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1)  

digoal's wechat