docker镜像的分层_docker的镜像和镜像源加速二

时间:2021年05月10日 00:30:18

一、介绍

docker镜像的分层(kvm 链接克隆,写时复制的特性)镜像分层的好处:复用,节省磁盘空间,相同的内容只需加载一份到内存。修改dockerfile之后,再次构建速度快

二、查看docker镜像分层

dockerload -i 镜像名 导入镜像时可以查看分层信息dockerimage history镜像名(id)查看镜像分层信息

三、通过修改dockerfile来优化镜像分层信息

影响docker分层信息的dockerfile字段:ADD、RUN等。尽量合并RUN和ADD的使用来减少镜像分层数,达到优化镜像目的。

四、容器间的互联(--link 是单方向的!!!)

docker run -d -p 80:80 nginxdocker run -it --link web01:web01 nginx /bin/bashping web01

五、使用docker运行zabbix-server1.下载并上传相关zabbix和mysql镜像包

zabbix-java-gateway.tar.gzzabbix-server-mysql.tar.gzzabbix-web-nginx-mysql.tar.gzdocker-mysql-5.7.tar.gz

2.执行docker命令

docker run --name mysql-server -t \-e MYSQL_DATABASE="zabbix" \-e MYSQL_USER="zabbix" \-e MYSQL_PASSWORD="zabbix_pwd" \-e MYSQL_ROOT_PASSWORD="root_pwd" \-d mysql:5.7 \--character-set-server=utf8 --collation-server=utf8_bindocker run --name zabbix-java-gateway -t \-d zabbix/zabbix-java-gateway:latestdocker run --name zabbix-server-mysql -t \-e DB_SERVER_HOST="mysql-server" \-e MYSQL_DATABASE="zabbix" \-e MYSQL_USER="zabbix" \-e MYSQL_PASSWORD="zabbix_pwd" \-e MYSQL_ROOT_PASSWORD="root_pwd" \-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \--link mysql-server:mysql \--link zabbix-java-gateway:zabbix-java-gateway \-p 10051:10051 \-d zabbix/zabbix-server-mysql:latestdocker run --name zabbix-web-nginx-mysql -t \-e DB_SERVER_HOST="mysql-server" \-e MYSQL_DATABASE="zabbix" \-e MYSQL_USER="zabbix" \-e MYSQL_PASSWORD="zabbix_pwd" \-e MYSQL_ROOT_PASSWORD="root_pwd" \--link mysql-server:mysql \--link zabbix-server-mysql:zabbix-server \-p 80:80 \-d zabbix/zabbix-web-nginx-mysql:latest

3.在客户端安装zabbix-agent,并配置server

vim /etc/zabbix/zabbix_agentd.confserver 10.0.0.11启动agentsystemctlrestartzabbix-agent.service

4.服务端重启mysql的docker服务

systemctl restartzabbix_mysql-server

3.浏览器访问查看,并配置zabbix

image.png注意:监控报警:微信报警,alpineyum 安装zabbix好使

六、docker registry(私有仓库)1.普通的registry

docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registryregistry上传镜像到私有仓库:a:给镜像打标签dockertag nginx:v1 10.0.0.11:5000/nginx:v1b:上传镜像docker push 10.0.0.11:5000/nginx:v1docker run -d 10.0.0.11:5000/nginx:v1如果遇到报错:The push refers to repository [10.0.0.11:5000/centos6.9_ssh]Get https://10.0.0.11:5000/v2/: http: server gave HTTP response to HTTPS client解决方法:vim/etc/docker/daemon.json{"insecure-registries": ["10.0.0.11:5000"]}systemctl restart docker

2.带basic认证的registry

安装httpd并创建加密文件yum install httpd-tools -ymkdir /opt/registry-var/auth/ -phtpasswd-Bbn zwb 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

命令行测试:

image.png

七、docker-compose(单机版的容器编排工具)

1.安装(一)yum install -y python2-pip(需要epel源)pip install docker-compose(默认pypi源在国外)##pip 加速1.安装(二)yum install docker-compose-y (需要epel源)2.##详细指令http://www.jianshu.com/p/2217cfed29d73.docker-compose.yml文件配置mkdir/data/my_wordpress/ -pcd /data/my_wordpress/vim docker-compose.ymlversion: '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: wordpressvolumes:db_data:web_data:4.#启动docker-compose up[start|stop|restart|down]5.#后台启动docker-compose up -d

八、重启docker服务,容器全部退出的解决办法

方法一:docker run--restart=always方法二:"live-restore": truedocker server配置文件/etc/docker/daemon.json参考{ "registry-mirrors": ["http://b7a9017d.m.daocloud.io"], "insecure-registries":["10.0.0.11:5000"], "live-restore": true}