docker_practice/repository/registry_auth.md

219 lines
5.4 KiB
Go
Raw Normal View History

#
2017-12-11 20:22:51 +08:00
使 `Docker Compose` TLS
##
2017-12-11 20:22:51 +08:00
使 `openssl`
`docker.domain.com`使 `openssl` `docker.domain.com` SSL
`CA`
```bash
$ openssl genrsa -out "root-ca.key" 4096
```
`CA`
```bash
$ openssl req \
-new -key "root-ca.key" \
-out "root-ca.csr" -sha256 \
-subj '/C=CN/ST=Shanxi/L=Datong/O=Your Company Name/CN=Your Company Name Docker Registry CA'
```
> `-subj` `/C` `CN``/ST` `/L` `/O` `/CN`
`CA` `root-ca.cnf`
```bash
[root_ca]
basicConstraints = critical,CA:TRUE,pathlen:1
keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
subjectKeyIdentifier=hash
```
```bash
$ 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`
```bash
$ openssl genrsa -out "docker.domain.com.key" 4096
```
使
```bash
$ openssl req -new -key "docker.domain.com.key" -out "site.csr" -sha256 \
-subj '/C=CN/ST=Shanxi/L=Datong/O=Your Company Name/CN=docker.domain.com'
```
`site.cnf`
```bash
[server]
authorityKeyIdentifier=keyid,issuer
basicConstraints = critical,CA:FALSE
extendedKeyUsage=serverAuth
keyUsage = critical, digitalSignature, keyEncipherment
subjectAltName = DNS:docker.domain.com, IP:127.0.0.1
subjectKeyIdentifier=hash
```
`SSL`
```bash
$ openssl x509 -req -days 750 -in "site.csr" -sha256 \
-CA "root-ca.crt" -CAkey "root-ca.key" -CAcreateserial \
-out "docker.domain.com.crt" -extfile "site.cnf" -extensions server
```
2018-07-10 12:24:42 +08:00
`docker.domain.com` SSL `docker.domain.com.key` SSL `docker.domain.com.crt` CA `root-ca.crt`
2017-12-11 20:22:51 +08:00
2018-07-10 12:24:42 +08:00
`ssl` `docker.domain.com.key` `docker.domain.com.crt` `root-ca.crt`
##
2017-12-11 20:22:51 +08:00
`/etc/docker/registry/config.yml` `config.yml`
```yaml
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://docker.domain.com
headers:
X-Content-Type-Options: [nosniff]
http2:
disabled: false
tls:
certificate: /etc/docker/registry/ssl/docker.domain.com.crt
key: /etc/docker/registry/ssl/docker.domain.com.key
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
```
## http
2017-12-11 20:22:51 +08:00
```bash
$ mkdir auth
$ docker run --rm \
--entrypoint htpasswd \
httpd:alpine \
2017-12-11 20:22:51 +08:00
-Bbn username password > auth/nginx.htpasswd
```
> `username` `password`
## `docker-compose.yml`
2017-12-11 20:22:51 +08:00
```yaml
version: '3'
services:
registry:
image: registry
ports:
- "443:443"
volumes:
- ./:/etc/docker/registry
- registry-data:/var/lib/registry
volumes:
registry-data:
```
## hosts
2017-12-11 20:22:51 +08:00
`/etc/hosts`
```bash
2018-06-03 21:28:31 +08:00
127.0.0.1 docker.domain.com
2017-12-11 20:22:51 +08:00
```
##
2017-12-11 20:22:51 +08:00
```bash
$ docker-compose up -d
```
TLS
##
2017-12-11 20:22:51 +08:00
2018-07-11 08:13:17 +08:00
CA CA `ssl/root-ca.crt` `/etc/docker/certs.d/docker.domain.com`
```bash
$ sudo mkdir -p /etc/docker/certs.d/docker.domain.com
$ sudo cp ssl/root-ca.crt /etc/docker/certs.d/docker.domain.com/ca.crt
```
2017-12-11 20:22:51 +08:00
```bash
$ docker login docker.domain.com
```
```bash
2018-07-10 08:09:10 +08:00
$ docker pull ubuntu:18.04
2017-12-11 20:22:51 +08:00
2018-07-10 08:09:10 +08:00
$ docker tag ubuntu:18.04 docker.domain.com/username/ubuntu:18.04
2017-12-11 20:22:51 +08:00
2018-07-10 08:09:10 +08:00
$ docker push docker.domain.com/username/ubuntu:18.04
2017-12-11 20:22:51 +08:00
2018-07-10 08:09:10 +08:00
$ docker image rm docker.domain.com/username/ubuntu:18.04
2017-12-11 20:22:51 +08:00
2018-07-10 08:09:10 +08:00
$ docker pull docker.domain.com/username/ubuntu:18.04
2017-12-11 20:22:51 +08:00
```
退
```bash
$ docker logout docker.domain.com
2018-07-10 08:09:10 +08:00
$ docker push docker.domain.com/username/ubuntu:18.04
2017-12-11 20:22:51 +08:00
no basic auth credentials
```
##
2017-12-11 20:22:51 +08:00
`443` [Nginx ](https://docs.docker.com/registry/recipes/nginx/),这里不再赘述。