这篇指南教你如何管理Docker镜像和仓库,包含实战案例,助你轻松掌握Docker核心概念。
原文标题:Docker镜像和仓库管理
原文作者:牧羊人的方向
冷月清谈:
**Docker 镜像管理**
Docker 镜像是容器的只读模板,由一系列层组成。每层存储指令的变更,例如运行命令、添加文件、创建环境变量等。这些指令存储在 Dockerfile 中,用于构建镜像。
镜像的分层结构使得多个容器可以共享基础镜像层,同时拥有独立的数据状态。Copy-on-write 机制提高了效率,仅在修改文件时复制,最小化了 I/O 和存储空间。
文中详细描述了运行 docker 容器的步骤,包括拉取镜像、创建容器、分配文件系统、设置网络和 IP 地址等。
还介绍了查看镜像列表、添加标签、查看详细信息、查看镜像历史、获取新镜像、查找镜像、删除镜像等常用命令。
此外,文章讲解了如何创建镜像,包括从容器更新镜像并提交、基于本地模板导入、使用 Dockerfile 创建等方法,并以 Nginx 为例进行了实战演示。
最后,文章介绍了镜像的存出、载入和上传操作。
**Docker 仓库管理**
Docker 仓库用于存储 Docker 镜像。Docker Hub 是 Docker 官方维护的公共仓库,用户可以从中下载镜像。
文章还介绍了如何创建私有仓库,包括使用 registry 镜像运行容器、修改配置文件以支持 HTTP、设置 Basic 认证等步骤。
怜星夜思:
2、Docker Hub 和私有仓库有什么区别?在什么情况下应该选择使用私有仓库?
3、文章中提到了使用 Dockerfile 管理镜像,那么如何编写一个高效的 Dockerfile 呢?有没有一些最佳实践可以参考?
原文内容
Docker镜像是Docker容器运行时的只读模板,每一个镜像由一系列的层(layers)组成,对容器的更新操作只是对顶层的可写层操作,而镜像层并没有更改。本文简要介绍了容器镜像和仓库管理和操作,通过实战加深对容器相关概念的理解。
1、Docker镜像管理
Docker镜像是Docker容器运行时的只读模板,每一个镜像由一系列的层(layers)组成,使用docker pull命令下载时会获取并输出镜像的各层信息。当不同的镜像包含相同的层的时候,本地仅存储一份内容,减少了需要的存储空间。
Docker镜像从基础镜像通过指令创建,每一个指令会在镜像中创建一个新的层,指令可以包含这些动作:
-
运行一个命令
-
增加文件或者文件夹
-
创建一个环境变量
-
当运行容器的时候哪些程序会运行
这些指令存储在Dockerfile文件中,当你需要建立镜像的时候,Docker可以从Dockerfile中读取这些指令并且运行,然后返回一个最终的镜像。
启动镜像的时候,一个新的可写层会加载到镜像的顶部。这一层通常称为“容器层”, 之下是“镜像层”。容器层可以读写,容器所有发生文件变更写都发生在这一层。镜像层read-only,只允许读取。容器和镜像的区别在顶层的可写层,所有对容器的写操作会保存到这个可写层中。当容器删除后,可写层也会被删除,基础镜像保持不变。
因为每个容器都有自己的可写容器层,并且所有的更改都存储在该容器层中,所以多个容器可以共享基础镜像层,但具有自己的数据状态。下图显示了多个共享同一Ubuntu 15.04镜像的容器。
-
Copy-on-write写入时复制
写时复制是一种共享和复制文件的策略,可以最大程度地提高效率。如果文件或目录位于镜像的较低层,而另一层(包括可写层)需要对其进行读取访问,那么它使用现有的文件。如果另一层第一次需要修改文件时(在构建镜像或运行容器时),将文件复制到该层并进行修改。这样可以将I / O和后续层的大小最小化。
-
复制使容器更高效
启动容器时,会在镜像层之上添加一个可写容器层,容器对文件系统所做的任何更改都存储在此处,不需要修改的部分不需要复制到该层,这意味着可写层尽可能小。
-
当容器需要读取文件的时候:从最上层镜像开始查找,往下找,找到文件后读取并放入内存,若已经在内存中了,直接使用。(即,同一台机器上运行的docker容器共享运行时相同的文件)。
-
当容器需要添加文件的时候:直接在最上面的容器层可写层添加文件,不会影响镜像层。
-
当容器需要修改文件的时候:从上往下层寻找文件,找到以后复制到容器可写层。对容器来说,看到的是容器层的这个文件,看不到镜像层里的文件,容器可以在容器层修改这个文件。
-
当容器需要删除文件的时候:从上往下层寻找文件,找到后在容器中记录删除,但是并不会真正的删除文件,而是软删除。这将导致镜像体积只会增加,不会减少。
因此,Docker镜像通过分层实现了资源共享,通过copy-on-write实现了文件隔离。
-
运行docker容器时发生了什么?
使用docker命令时,Docker客户端都告诉Docker守护进程运行一个容器。
$ sudo docker run -i -t ubuntu /bin/bash
以上述命令为例,Docker客户端使用docker命令来运行,run参数表明客户端要运行一个新的容器。Docker客户端要运行一个容器需要告诉Docker守护进程的最小参数信息是:
-
这个容器从哪个镜像创建,这里是ubuntu,基础的Ubuntu镜像。
-
在容器中要运行的命令,这里是/bin/bash,在容器中运行Bash shell。
按照顺序,Docker做了这些事情:
-
拉取ubuntu镜像: Docker检查ubuntu镜像是否存在,如果在本地没有该镜像,Docker会从Docker Hub下载。如果镜像已经存在,Docker会使用它来创建新的容器。
-
创建新的容器: 当Docker有了这个镜像之后,Docker会用它来创建一个新的容器。
-
分配文件系统并且挂载一个可读写的层: 容器会在这个文件系统中创建,并且一个可读写的层被添加到镜像中。
-
分配网络/桥接接口: 创建一个允许容器与本地主机通信的网络接口。
-
设置一个IP地址: 从池中寻找一个可用的IP地址并且服加到容器上。
-
运行你指定的程序: 运行指定的程序。
-
捕获并且提供应用输出: 连接并且记录标准输出、输入和错误让你可以看到你的程序是如何运行的。
1)查看当前主机镜像列表
[root@tango-01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1 2256fb90ca54 4 days ago 72.9MB
ubuntu latest bb0eaf4eee00 11 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
hello-world latest bf756fb1ae65 8 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
各个选项说明:
-
REPOSITORY:表示镜像的仓库源
-
TAG:镜像的标签
-
IMAGE ID:镜像ID
-
CREATED:镜像创建时间
-
SIZE:镜像大小
更多的命令可通过man docker-images来查看
2)使用tag命令添加镜像标签
[root@tango-01 ~]# docker tag ubuntu:latest myubuntu:latest
[root@tango-01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1 2256fb90ca54 4 days ago 72.9MB
myubuntu latest bb0eaf4eee00 11 days ago 72.9MB
ubuntu latest bb0eaf4eee00 11 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
hello-world latest bf756fb1ae65 8 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
看到镜像list中新增myubuntu:latest镜像
3)使用inspect命令查看详细信息
[root@tango-01 ~]# docker inspect myubuntu:latest
[
{
"Id": "sha256:bb0eaf4eee00c28cb8ffd54e571dd225f1dd2ed8d8751b2835c31e84188bf2de",
"RepoTags": [
"myubuntu:latest",
"ubuntu:latest"
],
"RepoDigests": [
"ubuntu@sha256:cbcf86d7781dbb3a6aa2bcea25403f6b0b443e20b9959165cf52d2cc9608e4b9"
],
"Parent": "",
"Comment": "",
"Created": "2020-09-16T22:20:25.950751723Z",
"Container": "6fbeb345a9d4f5184e942758ce74df682a834a8cc03e08b72c5c276cfaf8f05b",
"ContainerConfig": {
"Hostname": "6fbeb345a9d4",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
返回是json格式信息,如果只需要其中一项内容,可以使用参数-f指定
[root@tango-01 ~]# docker inspect myubuntu:latest -f {{".Architecture"}}
amd64
4)使用history命令查看镜像历史
镜像文件由多个层组成,使用history子命令可以列出各层创建的信息
[root@tango-01 ~]# docker history myubuntu:latest
IMAGE CREATED CREATED BY SIZE COMMENT
bb0eaf4eee00 11 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 11 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 11 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 11 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 11 days ago /bin/sh -c #(nop) ADD file:1b4ec50586b9f0621… 72.9MB
当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。
[root@tango-01 ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
d121f8d1c412: Already exists
f3cebc0b4691: Pull complete
1862755a0b37: Retrying in 1 second
489b44f3dbb4: Downloading
690874f836db: Downloading
baa8be383ffb: Downloading
55356608b4ac: Retrying in 4 seconds
dd35ceccb6eb: Downloading
429b35712b19: Waiting
162d8291095c: Waiting
5e500ef7181b: Waiting
af7528e958b6: Waiting
latest: Pulling from library/mysql
d121f8d1c412: Already exists
f3cebc0b4691: Pull complete
1862755a0b37: Pull complete
489b44f3dbb4: Pull complete
690874f836db: Pull complete
baa8be383ffb: Pull complete
55356608b4ac: Pull complete
dd35ceccb6eb: Pull complete
429b35712b19: Pull complete
162d8291095c: Pull complete
5e500ef7181b: Pull complete
af7528e958b6: Pull complete
Digest: sha256:e1bfe11693ed2052cb3b4e5fa356c65381129e87e38551c6cd6ec532ebe0e808
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为:https://hub.docker.com/ 我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。
[root@tango-01 ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3184 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 36
centos/httpd 32 [OK]
arm32v7/httpd The Apache HTTP Server Project 9
polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO… 4 [OK]
salim1983hoop/httpd24 Dockerfile running apache config 2 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
publici/httpd httpd:latest 1 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 1 [OK]
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
dockerpinata/httpd 0
e2eteam/httpd 0
interlutions/httpd httpd docker image with debian-based config … 0 [OK]
appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK]
manasip/httpd 0
amd64/httpd The Apache HTTP Server Project 0
trollin/httpd 0
hypoport/httpd-cgi httpd-cgi 0 [OK]
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
manageiq/httpd Container with httpd, built on CentOS for Ma… 0 [OK]
ppc64le/httpd The Apache HTTP Server Project 0
fpfis/httpd-php-dev 0
镜像删除使用 docker rmi 命令,比如我们删除 hello-world 镜像:
[root@tango-01 ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 4791aecec8f1 is using its referenced image bf756fb1ae65
[root@tango-01 ~]#
[root@tango-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c39cf512b3f9 training/webapp "python app.py" 4 days ago Exited (137) 3 days ago pensive_liskov
14be935835a3 nginx:latest "/docker-entrypoint.…" 4 days ago Exited (0) 4 days ago admiring_galileo
cf762ae42eb5 ubuntu "/bin/bash" 4 days ago Exited (0) 4 days ago zealous_khayyam
72c239912c08 ubuntu:latest "/bin/bash" 4 days ago Created jolly_margulis
4791aecec8f1 hello-world "/hello" 4 days ago Exited (0) 4 days ago busy_feynman
当使用docker rmi命令,后面跟上镜像的id时会先尝试删除所有指向该镜像的标签,然后删除该镜像文件本身。当有该镜像创建的容器存在时,镜像文件默认是无法删除的。
当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下方式对镜像进行更改。
-
从已经创建的容器中更新镜像,并且提交这个镜像
-
基于本地模板导入
-
使用 Dockerfile 指令来创建一个新的镜像
-
使用已有镜像的容器创建
更新镜像之前,我们需要使用镜像来创建一个容器。
[root@tango-01 ~]# docker run -t -i mysql:latest /bin/bash
root@73e23fa196b9:/#
在运行的容器内使用 apt-get update 命令进行更新。在完成操作之后,输入 exit 命令来退出这个容器。此时ID为73e23fa196b9的容器,是按我们的需求更改的容器。我们可以通过命令docker commit来提交容器副本:
[root@tango-01 ~]# docker commit -m="updated" 73e23fa196b9 test/mysql:v2
sha256:97191de67512364a3c9f54aa87685c3dbe755325d709937a5c9db38b953fefdd
-
基于本地模板导入
用户可以从一个操作系统模板文件导入一个镜像,主要使用docker import命令:
docker import [OPTIONS] file |URL|-[REPOSITORY [:TAG]]
如下载了ubuntu的模板压缩包,然后使用命令导入:
[root@tango-01 /]# cat /tmp/docker/ubuntu.tar |docker import - test/ubuntu:v1
sha256:2256fb90ca54710fc45c6544495faaf6735a1f3d7856ba17e04e40df5b32e2cd
查看镜像列表已经存在
[root@tango-01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mysql v2 97191de67512 4 minutes ago 544MB
test/ubuntu v1 2256fb90ca54 4 days ago 72.9MB
myubuntu latest bb0eaf4eee00 11 days ago 72.9MB
ubuntu latest bb0eaf4eee00 11 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
mysql latest e1d7dc9731da 2 weeks ago 544MB
hello-world latest bf756fb1ae65 8 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
1)导出镜像
[root@tango-01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mysql v2 97191de67512 19 hours ago 544MB
test/ubuntu v1 2256fb90ca54 5 days ago 72.9MB
myubuntu latest bb0eaf4eee00 12 days ago 72.9MB
ubuntu latest bb0eaf4eee00 12 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
mysql latest e1d7dc9731da 2 weeks ago 544MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
[root@tango-01 ~]# docker image save nginx > /tmp/docker-nginx.tar.gz
镜像文件保存在/tmp目录中
2)载入镜像
[root@tango-01 tmp]# docker image load -i /tmp/docker-nginx.tar.gz
Loaded image: nginx:latest
[root@tango-01 tmp]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mysql v2 97191de67512 20 hours ago 544MB
test/ubuntu v1 2256fb90ca54 5 days ago 72.9MB
myubuntu latest bb0eaf4eee00 12 days ago 72.9MB
ubuntu latest bb0eaf4eee00 12 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
mysql latest e1d7dc9731da 2 weeks ago 544MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
可以使用docker push命令上传镜像到仓库,默认上传到Docker Hub官方仓库(需要登录),命令格式为:
#docker push NAME [:TAG] | [REGISTORY_HOST [:REGISTORY_PORT]/] NAME [:TAG]
用户在Docker HUB网站注册后可以上传自制的镜像,如上传本地的test:
[root@tango-01 docker]# docker push test/test:latest
The push refers to repository [docker.io/user/test]
An image does not exist locally with the tag: user/test
第一次上传时,会提示输入登录信息或进行注册。
官方构建dockerffile文件参考:https://github.com/CentOS/CentOS-Dockerfiles
1)dockerfile主要组成部分:
-
基础镜像信息 FROM centos:6.8
-
制作镜像操作指令RUN yum insatll openssh-server -y
-
容器启动时执行指令 CMD ["/bin/bash"]
2)dockerfile常用指令:
-
FROM :基础镜像
-
MAINTAINER :维护者信息
-
RUN:在命令前面加上RUN即可
-
ADD :COPY文件,会自动解压
-
WORKDIR :设置当前工作目录
-
VOLUME :设置卷,挂载主机目录
-
EXPOSE :指定对外的端口
-
CMD :执行Dockerfile中的命令
3)dockerfile其他指令:
-
COPY:复制文件
-
ENV :环境变量
-
ENTRYPOINT:容器启动后执行的命令
1)创建目录并编辑Dockerfile
[root@tango-01 /]# mkdir -p /usr/local/docker/dockerfile/nginx
[root@tango-01 /]# cd /usr/local/docker/dockerfile/nginx
[root@tango-01 nginx]# vim Dockerfile
# This is docker file
# version v1
# Author Tango
# Base image(基础镜像)
FROM centos
# Maintainer(维护者信息)
MAINTAINER Tango
#将nginx包放入/usr/local/src并自动解压http://nginx.org/download/
ADD nginx-1.19.3.tar.gz /usr/local/src
# Commands(执行命令)
#安装依赖包
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel pcre pcre-devel
#创建目录
RUN mkdir -p /usr/local/nginx
#编译nginx
WORKDIR /usr/local/src/nginx-1.19.3
RUN ./configure --prefix=/usr/local/nginx && make && make install
#COPY .nginx_conf /usr/local/nginx/conf/nginx.conf
#RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf
# 对外的端口
EXPOSE 80
#添加环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#entrypoint的作用是,把整个container变成了一个可执行的文件,这样不能够通过替换CMD的方法来改变创建container的方式。
#但是可以通过参数传递的方法影响到container内部,每个Dockerfile只能够包含一个entrypoint
#当定义了entrypoint以后,CMD只能够作为参数进行传递
ENTRYPOINT ["nginx"]
# 执行的命令
CMD [“-g”,”daemon off;”]
2)构建docker镜像
[root@tango-01 nginx]# docker image build -t test/mynginx:v1 /usr/local/docker/dockerfile/nginx
Sending build context to Docker daemon 1.056MB
Step 1/11 : FROM centos
---> 0d120b6ccaa8
Step 2/11 : MAINTAINER Tango
---> Using cache
---> 753167b7da8c
Step 3/11 : ADD nginx-1.19.3.tar.gz /usr/local/src/
---> Using cache
---> 91be11ca14a9
Step 4/11 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
---> Using cache
---> 9bda47e7cc61
Step 5/11 : RUN yum install -y libxslt-devel -y gd gd-devel pcre pcre-devel
---> Using cache
---> c74cd06d6479
Step 6/11 : RUN mkdir -p /usr/local/nginx
---> Using cache
---> 74fdea8382d0
Step 7/11 : WORKDIR /usr/local/src/nginx-1.19.3
---> Using cache
---> 6c34ee455c4d
Step 8/11 : RUN ./configure --prefix=/usr/local/nginx && make && make install
---> Using cache
---> 44594a2cb326
Step 9/11 : EXPOSE 80
---> Running in 100e3ce072e6
Removing intermediate container 100e3ce072e6
---> 80fb726aa868
Step 10/11 : ENV PATH /usr/local/nginx/sbin:$PATH
---> Running in 762becdd8106
Removing intermediate container 762becdd8106
---> cb57ab25dabb
Step 11/11 : CMD ['nginx','-g','daemon off;']
---> Running in adececc59a96
Removing intermediate container adececc59a96
---> f5489aa03de1
Successfully built f5489aa03de1
Successfully tagged test/mynginx:v1
[root@tango-01 nginx]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mynginx v1 f5489aa03de1 5 minutes ago 556MB
test/mysql v2 97191de67512 46 hours ago 544MB
test/ubuntu v1 2256fb90ca54 6 days ago 72.9MB
myubuntu latest bb0eaf4eee00 13 days ago 72.9MB
ubuntu latest bb0eaf4eee00 13 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
mysql latest e1d7dc9731da 2 weeks ago 544MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
3)使用自构建的镜像启动
[root@tango-01 nginx]# docker run -d -p 8080:80 test/mynginx:v1
0bc00ee0c3207ce2b93fdd2a0d7ddad2b219fe566dc864e5f07b15bb840bd155
[root@tango-01 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0bc00ee0c320 test/mynginx:v1 "nginx -g 'daemon of…" 6 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp charming_khayyam
4)进入容器,查看文件系统
[root@0bc00ee0c320 /]# ls /usr/local/nginx/
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
5)访问192.168.112.10:8080
2、Docker仓库管理
Docker仓库是Docker镜像的存储仓库,可以推送镜像到Docker仓库中,也可以从Docker仓库中搜索镜像。
目前 Docker 官方维护了一个公共仓库 Docker Hub。大部分需求都可以通过在 Docker Hub 中直接下载镜像来实现。访问Docker Hub需要注册好账号进行登录:
[root@tango-01 nginx]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: tango
Password:
Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
Private Registry(私有仓库)是开发者或者企业自建的镜像存储库,通常用来保存企业内部的 Docker 镜像,用于内部开发流程和产品的发布、版本控制。
Docker 官方提供了一个搭建私有仓库的镜像 registry ,只需把镜像下载下来,运行容器并暴露5000端口,就可以使用了。
1)创建仓库
[root@tango-01 lib]# docker run -d -v /usr/local/docker/registry:/var/lib/registry -p 5000:5000 --name myregistry registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
cbdbe7a5bc2a: Pulling fs layer
47112e65547d: Pulling fs layer
46bcb632e506: Pulling fs layer
c1cc712bcecd: Waiting
3db6272dcbfa: Waiting
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete
47112e65547d: Pull complete
46bcb632e506: Pull complete
c1cc712bcecd: Pull complete
3db6272dcbfa: Pull complete
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
c2b1042df3f61a743153e040193d47bcbfa44e197b2cc51f716384c57c334dda
Registry服务默认会将上传的镜像保存在容器的/var/lib/registry,我们将主机的/usr/local/docker/registry目录挂载到该目录,即可实现将镜像保存到主机的/usr/local/docker/registry目录了。
2)修改配置文件,使之支持http
[root@tango-01 registry]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"],
"insecure-registries": ["192.168.112.10:5000"]
}
重启docker让修改生效
[root@tango-01 registry]# systemctl restart docker
[root@tango-01 registry]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-09-30 19:35:48 CST; 7s ago
Docs: https://docs.docker.com
Main PID: 14476 (dockerd)
Memory: 55.1M
CGroup: /system.slice/docker.service
└─14476 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Sep 30 19:35:46 tango-01 dockerd[14476]: time="2020-09-30T19:35:46.173249693+08:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///run/co...odule=grpc
Sep 30 19:35:46 tango-01 dockerd[14476]: time="2020-09-30T19:35:46.173260773+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Sep 30 19:35:46 tango-01 dockerd[14476]: time="2020-09-30T19:35:46.264694639+08:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
Sep 30 19:35:47 tango-01 dockerd[14476]: time="2020-09-30T19:35:47.839172947+08:00" level=info msg="Loading containers: start."
Sep 30 19:35:48 tango-01 dockerd[14476]: time="2020-09-30T19:35:48.370047601+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 17...P address"
Sep 30 19:35:48 tango-01 dockerd[14476]: time="2020-09-30T19:35:48.419169817+08:00" level=info msg="Loading containers: done."
Sep 30 19:35:48 tango-01 dockerd[14476]: time="2020-09-30T19:35:48.430996291+08:00" level=info msg="Docker daemon" commit=4484c46d9d graphdriver(s)=overlay2 v...n=19.03.13
Sep 30 19:35:48 tango-01 dockerd[14476]: time="2020-09-30T19:35:48.431155786+08:00" level=info msg="Daemon has completed initialization"
Sep 30 19:35:48 tango-01 dockerd[14476]: time="2020-09-30T19:35:48.538951692+08:00" level=info msg="API listen on /var/run/docker.sock"
Sep 30 19:35:48 tango-01 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
3)修改镜像标签
使用docker tag myubuntu:latest 192.168.112.10:5000/myubuntu:v1修改镜像标签
[root@tango-01 lib]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mynginx v1 3d8223a9f813 2 hours ago 556MB
test/mysql v2 97191de67512 2 days ago 544MB
test/ubuntu v1 2256fb90ca54 6 days ago 72.9MB
myubuntu latest bb0eaf4eee00 13 days ago 72.9MB
ubuntu latest bb0eaf4eee00 13 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
mysql latest e1d7dc9731da 2 weeks ago 544MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
[root@tango-01 lib]# docker tag myubuntu:latest 192.168.112.10:5000/myubuntu:v1
[root@tango-01 lib]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mynginx v1 3d8223a9f813 2 hours ago 556MB
test/mysql v2 97191de67512 2 days ago 544MB
test/ubuntu v1 2256fb90ca54 6 days ago 72.9MB
192.168.112.10:5000/myubuntu v1 bb0eaf4eee00 13 days ago 72.9MB
myubuntu latest bb0eaf4eee00 13 days ago 72.9MB
ubuntu latest bb0eaf4eee00 13 days ago 72.9MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
mysql latest e1d7dc9731da 2 weeks ago 544MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB
training/webapp latest 6fae60ef3446 5 years ago 349MB
4)将新打标签的镜像上传镜像到仓库
[root@tango-01 lib]# docker push 192.168.112.10:5000/myubuntu:v1
The push refers to repository [192.168.112.10:5000/myubuntu]
128fa0b0fb81: Pushed
c0151ca45f27: Pushed
b2fd17df2071: Pushed
v1: digest: sha256:028d7303257c7f36c721b40099bf5004a41f666a54c0896d5f229f1c0fd99993 size: 943
在客户端执行以下命令查看
[root@tango-01 /]# curl http://192.168.112.10:5000/v2/_catalog
{"repositories":["myubuntu"]}
1)安装加密工具
[root@tango-01 /]# yum install httpd-tools -y
2)设置认证密码
[root@tango-01 /]# mkdir /usr/local/docker/registry/auth
[root@tango-01 /]# htpasswd -Bbn tango xxxxxx > /usr/local/docker/registry/auth/htpasswd
[root@tango-01 /]# cd /usr/local/docker/registry/auth
[root@tango-01 auth]# cat htpasswd
tango:$2y$05$1qmt5IdKMJS3.pNahw2E2.d11dbUNyMeMHpfkwmQPYOJMc0tyuKW.
3)启动容器,在启动时传入认证参数
[root@tango-01 auth]# docker run -d -p 5000:5000 -v /usr/local/docker/registry/auth/:/auth/ -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
1b1ea6a1a38a59e1f204e1f6e748488d2cde0708fcc8e4c4b7443dc08ba9d6b5
4)使用验证用户测试
a) 登陆用户
[root@tango-01 auth]# docker login 192.168.112.10:5000
Username: tango
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
b) 认证文件的保存位置
[root@tango-01 auth]# cat /root/.docker/config.json
{
"auths": {
"192.168.112.10:5000": {
"auth": "dGFuZ286MTIzNDU2cWF6"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/19.03.13 (linux)"
}
c) 推送镜像到仓库
[root@tango-01 auth]# docker tag ubuntu:latest 192.168.112.10:5000/ubuntu:v1
[root@tango-01 auth]# docker push 192.168.112.10:5000/ubuntu:v1
The push refers to repository [192.168.112.10:5000/ubuntu]
128fa0b0fb81: Pushed
c0151ca45f27: Pushed
b2fd17df2071: Pushed
v1: digest: sha256:028d7303257c7f36c721b40099bf5004a41f666a54c0896d5f229f1c0fd99993 size: 943
至此,一个简单的docker镜像仓库搭建完成
参考资料
-
https://github.com/CentOS/CentOS-Dockerfiles
-
https://docs.docker.com/storage/storagedriver/
-
https://www.cnblogs.com/huanchupkblog/p/10843800.html
-
https://www.cnblogs.com/subendong/p/9029495.html