docker简单使用(五)

docker简单使用(五)

仓库

Docker Hub

类似github,docker也有Docker Hub注册了账号之后,也就可以进行镜像的
拉取:docker pull nginx
上传:docker push nginx
上传到Ddocker Hub时候,可以给自己的镜像一个标注,自己的docker hub id加上镜像kyle/nginx这样

1
2
3
docker tag nginx:v3 kyle/nginx:v3

docker push kyle/nginx:v3

私仓

个人、公司级别的镜像,可能不适合上传到公有仓库,类似gitlab一样,可以在本地创建私有仓库

创建私仓

官方提供的工具:docker-registry
运行:

1
docker run -d -p 8090:5000 --restart=always --name registry registry

进入容器:docker exec -it registry sh
仓库默认会创建到容器的该路径:/var/lib/registry

或者通过-v更改路径,将镜像文件放在本地指定路径:

1
docker run -d -p 8081:5000 -v /home/images:/var/lib/registry  --restart=always --name registry2 registry

将本地/home/images作为镜像上传路径,替换默认路径(该文件不存在,会自动创建)

操作镜像

先打tag

1
docker tag ubuntu:16.04 192.168.0.97:8081/ubuntu:16.04

再推送

1
2
3
[root@localhost images]# docker push 192.168.0.97:8081/ubuntu:16.04
The push refers to repository [192.168.0.97:8081/ubuntu]
Get https://192.168.0.97:8081/v2/: http: server gave HTTP response to HTTPS client

嗯。失败了、、、因为Docker默认不允许非HTTPS方式推送镜像,解决办法,要么是该配置,要么配置https

更改配置

适用于:ubuntu 16.04+Debian 8+centos 7
修改/etc/docker/daemon.json(不存在就新建)

1
2
3
4
5
6
7
8
{
"registry-mirrors":[
"https://registry.docker-cn.com"
],
"insecure-registries":[
"192.168.0.97:8081"
]
}

重启docker:systemctl restart docker
重新push:

1
2
3
4
5
6
7
[root@localhost images]# docker push 192.168.0.97:8081/ubuntu:16.04
The push refers to repository [192.168.0.97:8081/ubuntu]
4c54072a5034: Pushed
49652298c779: Pushed
e15278fcccca: Pushed
739482a9723d: Pushed
16.04: digest: sha256:08f4295167241c59fc4a24f18816618ff8f959756fb4b236e880a3b7f45f0ba0 size: 1150

看到成功了。。。
在本地/home/images

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[root@localhost home]# tree images/
images/
└── docker
└── registry
└── v2
├── blobs
│   └── sha256
│   ├── 08
│   │   └── 08f4295167241c59fc4a24f18816618ff8f959756fb4b236e880a3b7f45f0ba0
│   │   └── data
│   ├── 2a
│   │   └── 2a697363a8709093834e852b26bedb1d85b316c613120720fea9524f0e98e4a2
│   │   └── data
│   ├── 59
│   │   └── 59856638ac9f32d4caa0f5761b2597fe251642786fdfe1b917ddbb074b890c29
│   │   └── data
│   ├── 6f
│   │   └── 6f317d6d954b9a59c54b2cb09e1f30cd3e872796e431cd2ceac5ed570beb2939
│   │   └── data
│   ├── 9f
│   │   └── 9ff7e2e5f967fb9c4e8099e63508ab0dddebe3f820d08ca7fd568431b0d10c0e
│   │   └── data
│   └── a9
│   └── a9dde5e2a643eca8fde0eed52f4aed31f3ecd9c1b2f24d5e3729cd8d2ae68177
│   └── data
└── repositories
└── ubuntu
├── _layers
│   └── sha256
│   ├── 2a697363a8709093834e852b26bedb1d85b316c613120720fea9524f0e98e4a2
│   │   └── link
│   ├── 59856638ac9f32d4caa0f5761b2597fe251642786fdfe1b917ddbb074b890c29
│   │   └── link
│   ├── 6f317d6d954b9a59c54b2cb09e1f30cd3e872796e431cd2ceac5ed570beb2939
│   │   └── link
│   ├── 9ff7e2e5f967fb9c4e8099e63508ab0dddebe3f820d08ca7fd568431b0d10c0e
│   │   └── link
│   └── a9dde5e2a643eca8fde0eed52f4aed31f3ecd9c1b2f24d5e3729cd8d2ae68177
│   └── link
├── _manifests
│   ├── revisions
│   │   └── sha256
│   │   └── 08f4295167241c59fc4a24f18816618ff8f959756fb4b236e880a3b7f45f0ba0
│   │   └── link
│   └── tags
│   └── 16.04
│   ├── current
│   │   └── link
│   └── index
│   └── sha256
│   └── 08f4295167241c59fc4a24f18816618ff8f959756fb4b236e880a3b7f45f0ba0
│   └── link
└── _uploads

使用curl获取:

1
2
[root@localhost home]# curl 192.168.0.97:8081/v2/_catalog
{"repositories":["ubuntu"]}

Docker Compose建私仓

参照《docker practice》建私仓,配置全新啊认证,TLS
使用openssl自动签发站点SSL证书

新建一个空白文件夹,进入文件夹,开始操作
/etc/docker/registry

创建CA私钥

1
openssl genrsa -out "root-ca.key" 4096

创建CA根证书请求文件

1
2
openssl req -new -key "root-ca.key" -out "root-ca.csr" -sha256 \
> -subj '/C=CN/ST=Jiangsu/L=Suzhou/O=Company Xaa/CN=Company Xaa Docker Registry CA'

其中:

1
/C表示国家,如CN;/ST表示省,如Jiangsu;/L表示城市或地区,如Suzhou;/O表示组织名,如公司名;/CN表示组织通用名称,如公司对外的名称,XXX company

配置CA根证书

vim root-ca.cnf

1
2
3
4
[root_ca]
basicConstraints = critical,CA:TRUE,pathlen:1
keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
subjectKeyIdentifier=hash

签发根证书

1
2
3
4
openssl x509 -req -days 3650 -in "root-ca.csr" \
> -signkey "root-ca.key" -sha256 -out "root-ca.crt" \
> -extfile "root-ca.cnf" -extensions \
> root_ca

生成站点SSL私钥

1
openssl genrsa -out "tokyle.com.key" 4096

使用私钥生成证书请求文件

1
2
openssl req -new -key "tokyle.com.key" -out "site.csr" -sha256 \
> -subj '/C=CN/ST=Jiangsu/L=Suzhou/O=Company Xaa/CN=tokyle.com'

配置证书

新建site.cnf文件 vim site.cnf

1
2
3
4
5
6
7
[server]
authorityKeyIdentifier=keyid,issuer
basicConstraints = critical,CA:FALSE
extendedKeyUsage=serverAuth
keyUsage = critical, digitalSignature, keyEncipherment
subjectAltName = DNS:tokyle.com, IP:127.0.0.1
subjectKeyIdentifier=hash

签署站点SSL证书

1
2
3
openssl x509 -req -days 750 -in "site.csr" -sha256 \
> -CA "root-ca.crt" -CAkey "root-ca.key" -CAcreateserial \
> -out "tokyle.com.crt" -extfile "site.cnf" -extensions server

小结

1
2
3
4
5
6
7
8
9
10
11
[root@localhost registry]# ll
总用量 36
-rw-r--r-- 1 root root 140 6月 13 17:03 root-ca.cnf
-rw-r--r-- 1 root root 2021 6月 13 17:05 root-ca.crt
-rw-r--r-- 1 root root 1708 6月 13 16:49 root-ca.csr
-rw-r--r-- 1 root root 3247 6月 13 16:45 root-ca.key
-rw-r--r-- 1 root root 17 6月 13 17:18 root-ca.srl
-rw-r--r-- 1 root root 237 6月 13 17:14 site.cnf
-rw-r--r-- 1 root root 1679 6月 13 17:10 site.csr
-rw-r--r-- 1 root root 2098 6月 13 17:18 tokyle.com.crt
-rw-r--r-- 1 root root 3243 6月 13 17:08 tokyle.com.key

这样,就已经拥有了tokyle.com的网站SSL私钥tokyle.com.key和SSL证书tokyle.com.crt及CA根证书root-ca.crt

新建ssl文件夹,将tokyle.com.keytokyle.com.crtroot-ca.crt拷入,其他删除。

配置私有仓库

私仓默认配置文件位于:/etc/docker/registry/config.yml,先本地编辑config.yml再挂在到容器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
version: 0.1
log:
accesslog:
disabled: true
level: debug
formatter: text
fields:
service: registry
environment: staging
storage:
delete:
enabled: true
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
auth:
htpasswd:
realm: basic-realm
path: /etc/docker/registry/auth/nginx.htpasswd
http:
addr: :443
host: https://tokyle.com
headers:
X-Content-Type-Options: [nosniff]
http2:
disabled: false
tls:
certificate: /etc/docker/registry/ssl/tokyle.com.crt
key: /etc/docker/registry/ssl/tokyle.com.key
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3

生成http认证文件

1
mkdir auth
1
2
3
4
docker run --rm \
--entrypoint htpasswd \
registry \
-Bbn username pwd > auth/nginx.htpasswd

用户名密码用自己的

编辑docker-compose.yml

vim docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
version: '3'

services:
registry:
image: registry
ports:
- "443:443"
volumes:
- ./:/etc/docker/registry
- registry-data:/var/lib/registry

volumes:
registry-data:

修改hosts

vim /etc/hosts

1
127.0.0.1 tokyle.com

启动

安装docker-compose

1
pip install -U docker-compose

启动:

1
docker-compose up -d

由于自行签发的 CA 根证书不被系统信任,所以我们需要将 CA 根证书 ssl/root-ca.crt 移入/etc/docker/certs.d/docker.domain.com 文件夹中

1
2
mkdir -p /etc/docker/certs.d/tokyle.com
cp ssl/root-ca.crt /etc/docker/certs.d/tokyle.com/ca.crt

私仓操作

登录私仓:docker login tokyle.com
就可以进行镜像推送、拉取了(需要登录进去)

参考docker practice

私仓高级配置

使用Nexus建私仓

docker官方的registry建立的仓库,有些镜像删除后不会回收空间,必须命令去释放空间,重启Registry程序。当前比较常见的做法是使用Nexus来管理DockerYumPyPI

启动:

1
2
docker run -d --name nexus3 --restart=always \
-p 8081:8081 sonatype/nexus3

需要等一段时间,可以进容器docker exec -it nexus3 bash,看日志/nexus-data/log/nexus.log看到

1
Started Sonatype Nexus OSS 3.16.2-01

就是起好了,可以通过http://IP:8088访问web页面。默认账密:`admin/admin123`

Nexus配置

创建仓库

进入nexus界面之后,创建仓库:
点击设置(齿轮)

1
Repository->Repositories 点击右边菜单 Create repository 选择 docker (hosted)
  • Name: 仓库的名称,如dockertest
  • HTTP: 仓库单独的访问端口,如8082
  • Enable Docker V1 API: 如果需要同时支持 V1 版本请勾选此项
  • Hosted -> Deployment pollcy: 请选择 Allow redeploy 否则无法上传 Docker 镜像
添加权限

菜单 Security->Realms 把 Docker Bearer Token Realm 移到右边的框中保存。

添加用户规则:菜单 Security->Roles->Create role 在 Privlleges 选项搜索 docker 把相应的规则移动到右边的框中然后保存。

添加用户:菜单 Security->Users->Create local user 在 Roles 选项中选中刚才创建的规则移动到右边的窗口保存。

操作Nexus私仓

和之前官方私仓一样,不是https访问,需要在/etc/docker/daemon.json中添加:

1
2
3
4
5
{

"insecure-registries":["http://192.168.0.97:8082"]

}

重启docker:

1
2
3
systemctl daemon-reload

systemctl restart docker

登录:docker login 192.168.0.97:8082,输入创建的用户名和密码,完成登录。

Error response from daemon: Get http://192.168.0.97:8082/v2/: dial tcp 192.168.0.97:8082: connect: connection refused

docker login 192.168.0.97:8082时候,登录不进去,确认用户名密码正确,确认有添加到daemon.json中。
这个问题折腾我一个上午,想来想去,配置的没有问题,然后翻Stack Overflow时候看到差不多的问题,才知道什么鬼。。

首先,碰到这个,用curl验证:

1
2
3
curl "http://192.168.0.97:8082/v2/_catalog"

curl: (7) Failed connect to 192.168.0.97:8082; Connection refused

证明这个端口应该压根就没起来,然后往回翻,看看起nexus3这个容器的命令,发现是这样写的:

1
2
docker run -d --name nexus3 --restart=always \
-p 8081:8081 sonatype/nexus3

只给nexus3分配了8081端口,没有其他端口暴露出来,即使在页面设置了8082端口,因为8082是在容器内部,没有暴露出来,所以在服务器上,根本就连不到192.168.0.97的8082端口,解决办法就是暴露出8082端口。

1
docker run -d -p 8081:8081 -p 8082:8082 -p 8083:8083 --restart=always --name nexus3 sonatype/nexus3

然后再进入nexus页面重新设置,这样,就可以登陆了:

1
2
3
4
5
6
7
8
[root@localhost log]# docker login 192.168.0.97:8082
Username: admin
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
镜像操作

首先登录:

1
2
3
4
5
6
7
8
[root@localhost docker]# docker login 192.168.0.97:8082
Username: kyle
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

镜像操作:

1
2
3
4
5
6
7
8
9
docker tag nginx:v2 192.168.0.97:8082/nginx:v2

[root@localhost docker]# docker push 192.168.0.97:8082/nginx:v2
The push refers to repository [192.168.0.97:8082/nginx]
7df9ba6b1822: Pushed
ea06a73e56fc: Pushed
22c458a3ff08: Pushed
6270adb5794c: Pushed
v2: digest: sha256:619a4fd83aef966bbee5677f0a2c169ae990cf83b9183aed0969c991b04e23dc size: 1155

可以到nexus界面的Docker栏看到推送上去的镜像。

删除本地镜像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost docker]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.0.97:8082/nginx v2 f531cfc896d6 3 days ago 109MB
nginx v2 f531cfc896d6 3 days ago 109MB


[root@localhost docker]# docker rmi 192.168.0.97:8082/nginx:v2
Untagged: 192.168.0.97:8082/nginx:v2
Untagged: 192.168.0.97:8082/nginx@sha256:619a4fd83aef966bbee5677f0a2c169ae990cf83b9183aed0969c991b04e23dc


[root@localhost docker]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 f531cfc896d6 3 days ago 109MB

从nexus私仓拉取镜像:

1
2
3
4
5
6
7
8
9
10
[root@localhost docker]# docker pull 192.168.0.97:8082/nginx:v2
v2: Pulling from nginx
Digest: sha256:619a4fd83aef966bbee5677f0a2c169ae990cf83b9183aed0969c991b04e23dc
Status: Downloaded newer image for 192.168.0.97:8082/nginx:v2


[root@localhost docker]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.0.97:8082/nginx v2 f531cfc896d6 3 days ago 109MB
nginx v2 f531cfc896d6 3 days ago 109MB

退出私仓:

1
2
[root@localhost docker]# docker logout 192.168.0.97:8082
Removing login credentials for 192.168.0.97:8082

然后,可以对自己的私仓加上个s,更安全一点,这个,教程一堆

文章目录
  1. Docker Hub
  2. 私仓
    1. 创建私仓
    2. 操作镜像
    3. 更改配置
  3. Docker Compose建私仓
    1. 创建CA私钥
    2. 创建CA根证书请求文件
    3. 配置CA根证书
    4. 签发根证书
    5. 生成站点SSL私钥
    6. 使用私钥生成证书请求文件
    7. 配置证书
    8. 签署站点SSL证书
    9. 小结
    10. 配置私有仓库
    11. 生成http认证文件
    12. 编辑docker-compose.yml
    13. 修改hosts
    14. 启动
    15. 私仓操作
  4. 参考docker practice
  5. 使用Nexus建私仓
    1. Nexus配置
      1. 创建仓库
      2. 添加权限
    2. 操作Nexus私仓
      1. Error response from daemon: Get http://192.168.0.97:8082/v2/: dial tcp 192.168.0.97:8082: connect: connection refused
      2. 镜像操作
|