十四、私有仓库(docker registry)

14.1 普通的registry

14.1.1 上传registry镜像压缩包并导入

[root@docker01 ~]# ls
registry.tar.gz
[root@docker01 ~]# docker load -i registry.tar.gz

14.1.2 创建私有仓库容器

docker run -d -p 5000:5000 --restart=always --name registry \
 -v /opt/myregistry:/var/lib/registry registry

14.1.3 上传镜像到私有仓库

#给镜像打标签
docker tag alpine:latest 10.0.0.11:5000/alpine:latest
#上传镜像
docker push 10.0.0.11:5000/alpine:latest

14.1.4 上传镜像报错解决方法

说明:如果在上传镜像过程中遇到如下报错,下面给出了解决方法
The push refers to repository [10.0.0.11:5000/alpine]
Get https://10.0.0.11:5000/v2/: http: server gave HTTP response to HTTPS client
#解决方法:/etc/docker/daemon.json中添加配置文件,重启docker
[root@docker01 ~]# cat /etc/docker/daemon.json     #<==注意需要在已有配置的末尾加逗号
{
  "registry-mirrors": ["https://registry.docker-cn.com"],
  "insecure-registries": ["10.0.0.11:5000"]
} 
systemctl restart docker

14.2 带basic认证的registry

yum install httpd-tools -y
mkdir /opt/registry-var/auth/ -p
htpasswd  -Bbn test123 123456  >> /opt/registry-var/auth/htpasswd
docker run -d -p 5000:5000 --restart=always -v /opt/registry-var/auth/:/auth/ \
 -v /opt/myregistry:/var/lib/registry -e "REGISTRY_AUTH=htpasswd" \
 -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
 -e  "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" registry
提示:以后下载或上传镜像都需要先docker login 10.0.0.11:5000 登录

14.3 删除镜像

14.3.1 进入docker registry的容器中

docker exec -it registry /bin/sh

14.3.2 删除repo(索引文件)

rm -fr /var/lib/registry/docker/registry/v2/repositories/alpine

14.3.3 清除掉blod(实体镜像)

registry garbage-collect /etc/docker/registry/config.yml

总结:上传相似镜像到docker私有仓库的时候,只会将两个镜像不同的分层再次上传,相同的分层不重复
上传,下载镜像的时候也是如此,如先下载一个 centos6.9_ssh:v2,在下载一个 centos6.9_ssh:v3,只会将不同的分层再次下载,相同的分层不在重复下载。

十五、docker-compose(单机版的容器编排工具,类似ansible剧本)

官方地址:https://docs.docker.com/compose/compose-file/
### 15.1 安装
[root@docker01 ~]# yum install epel-release -y
[root@docker01 ~]# yum install docker-compose.noarch -y

15.2 上传wordpress镜像包到 /root并导入

[root@docker01 ~]# ls
wordpress-latest.tar.gz
[root@docker01 ~]# docker load -i wordpress-latest.tar.gz

15.3 创建目录并添加yaml文件

[root@docker01 ~]# mkdir /data/docker-compose/wordpress
[root@docker01 /data/docker-compose/wordpress]# vim docker-compose.yaml
version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - web_data:/var/www/html
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
web_data:

15.4 启动命令

docker-compose up                   #<==启动
docker-compose down             #<==关闭
docker-compose restart              #<==重启
docker-compose up -d                #<==后台启动

做事滴水穿石,做人滴水不漏