docker不是一个值得投入的领域,它解决的问题是Unix系统最初设计的一个疏忽。从一个不会用docker的小白,自己一步一步的摸索,中间也踩过许多坑。但任然,坚持从哪里跌倒就从哪里爬起来。不求感动自己,但求人生无悔。
下载方式:
1 容器简介
1.1 什么是 Linux 容器
1.2 容器不就是虚拟化吗
1.3 容器发展简史
2 什么是 Docker?
2.1 Docker 如何工作?
2.2 Docker 技术是否与传统的 Linux 容器相同?
2.3 docker的目标
3 安装Docker
3.1 Docker基础命令操作
3.2 启动第一个容器
3.3 Docker镜像生命周期
4 docker镜像相关操作
4.1 搜索官方仓库镜像
4.2 获取镜像
4.3 导出镜像
4.4 删除镜像
4.5 导入镜像
4.6 查看镜像的详细信息
5 容器的日常管理
5.1 容器的起/停
5.2 进入容器方法
5.3 删除所有容器
5.4 启动时进行端口映射
6 Docker 数据卷的管理
6.1 挂载时创建卷
6.2 创建卷后挂载
6.3 手动将容器保存为镜像
7 Dockerfile自动构建docker镜像
7.1 Dockerfile指令集
7.2 创建一个Dockerfile
7.3 使用Dcokerfile安装kodexplorer
8 Docker中的镜像分层
8.1 Docker 镜像为什么分层
8.2 可写的容器层
8.3 容器层的细节说明
9 使用docker运行zabbix-server
9.1 容器间的互联
9.2 启动zabbix容器
9.3 关于zabbix API
10 docker 仓库(registry)
10.1 创建一个普通仓库
10.2 带basic认证的仓库
11 docker-compose编排工具
11.1 安装docker-compose
11.2 编排启动镜像
11.3 haproxy代理后端docker容器
11.4 安装socat 直接操作socket控制haproxy
12 重启docker服务,容器全部退出的解决办法
12.1 在启动是指定自动重启
12.2 修改docker默认配置文件
13 Docker网络类型
13.1 docker的网络类型
13.2 不为容器配置网络功能
13.3 与其他容器共享网络配置(Container)
13.4 使用宿主机网络
13.5 查看网络列表
13.6 用PIPEWORK为docker容器配置独立IP
13.7 Docker跨主机通信之macvlan
14 docker企业级镜像仓库harbor
14.1 使用容器的建议
14.2 关于Docker容器的监控
15 参考文献
更加详细地来说,请您假定您在开发一个应用。您使用的是一台笔记本电脑,而且您的开发环境具有特定的配置。其他开发人员身处的环境配置可能稍有不同。您正在开发的应用依赖于您当前的配置,还要依赖于某些特定文件。与此同时,您的企业还拥有标准化的测试和生产环境,且具有自身的配置和一系列支持文件。您希望尽可能多在本地模拟这些环境,而不产生重新创建服务器环境的开销。
因此,您要如何确保应用能够在这些环境中运行和通过质量检测,并且在部署过程中不出现令人头疼的问题,也无需重新编写代码和进行故障修复?答案就是使用容器。容器可以确保您的应用拥有必需的配置和文件,使得这些应用能够在从开发到测试、再到生产的整个流程中顺利运行,而不出现任何不良问题。这样可以避免危机,做到皆大欢喜。
IT 软件中所说的 “Docker” ,是指容器化技术,用于支持创建和使用 Linux 容器。 开源 Docker 社区致力于改进这类技术,并免费提供给所有用户,使之获益。 Docker Inc. 公司凭借 Docker 社区产品起家,它主要负责提升社区版本的安全性,并将改进后的版本与更广泛的技术社区分享。此外,它还专门对这些技术产品进行完善和安全固化,以服务于企业客户。
# 需要两台几点进行安装
[root@docker01 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@docker01 ~]# uname -r
3.10.0-327.el7.x86_64
[root@docker01 ~]# hostname -I
10.0.0.100172.16.1.100
[root@docker02 ~]# hostname -I
10.0.0.101172.16.1.101
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
sed -i 's#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
yum install docker-ce -y
docker01
配置:# 修改启动文件,监听远程端口
vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://10.0.0.100:2375
systemctl daemon-reload
systemctl enable docker.service
systemctl restart docker.service
# ps -ef检查进行,是否启动
docker02
测试[root@docker02 ~]# docker -H 10.0.0.100 info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.12.0-ce
Storage Driver: devicemapper
···
[root@docker01 ~]# docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 2720:10:142017
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 2720:12:462017
OS/Arch: linux/amd64
Experimental: false
vi /etc/docker/daemon.json
{ 'registry-mirrors': ['https://registry.docker-cn.com']
}
[root@docker01 ~]# docker run -d -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e7bb522d92ff: Pull complete
6edc05228666: Pull complete
cd866a17e81f: Pull complete
Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
Status: Downloaded newer image for nginx:latest
8d8f81da12b5c10af6ba1a5d07f4abc041cb95b01f3d632c3d638922800b0b4d
# 容器启动后,在浏览器进行访问测试
[root@docker01 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 3992 [OK]
ansible/centos7-ansible Ansible on Centos7 105
[root@docker01 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
af4b0a2388c6: Downloading 34.65MB/73.67MB
[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest ff426288ea90 3 weeks ago 207MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB
docker pull index.tenxcloud.com/tenxcloud/httpd
[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest ff426288ea90 3 weeks ago 207MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB
# 导出
[root@docker01 ~]# docker image save centos > docker-centos.tar.gz
[root@docker01 ~]# docker image rm centos:latest
[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 3f8a4339aadd 5 weeks ago 108MB
[root@docker01 ~]# docker image load -i docker-centos.tar.gz
e15afa4858b6: Loading layer 215.8MB/215.8MB
Loaded image: centos:latest
[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest ff426288ea90 3 weeks ago 207MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB
[root@docker01 ~]# docker image inspect centos
[root@docker01 ~]# docker run nginx
[root@docker01 ~]# docker create centos:latest /bin/bash
bb7f32368ecf0492adb59e20032ab2e6cf6a563a0e6751e58930ee5f7aaef204
[root@docker01 ~]# docker start stupefied_nobel
stupefied_nobel
[root@docker01 ~]# docker run centos:latest /usr/bin/sleep 20;
[root@docker01 ~]# docker container ls
或
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8708e93fd767 nginx 'nginx -g 'daemon of…'6 seconds ago Up 4 seconds 80/tcp keen_lewin
[root@docker01 ~]# docker container inspect 容器名称/id
root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8708e93fd767 nginx 'nginx -g 'daemon of…'4minutes ago Exited (0) 59 seconds ago keen_lewin
f9f3e6af7508 nginx 'nginx -g 'daemon of…' 5 minutes ago Exited (0) 5 minutes ago optimistic_haibt
8d8f81da12b5 nginx 'nginx -g 'daemon of…' 3 hours ago Exited (0) 3 hours ago lucid_bohr
[root@docker01 ~]# docker stop 容器名称/id
或
[root@docker01 ~]# docker container kill 容器名称/id
[root@docker01 ~]# docker run -it #参数:-it 可交互终端
[root@docker01 ~]# docker run -it nginx:latest /bin/bash
root@79241093859e:/#
1 | ctrl+p & ctrl+q
[root@docker01 ~]# docker run -it centos:latest
[root@1bf0f43c4d2f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 10015:47 pts/000:00:00 /bin/bash
root 131015:47 pts/000:00:00 ps -ef
[root@docker01 ~]# docker attach 1bf0f43c4d2f
[root@1bf0f43c4d2f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 10015:47 pts/000:00:00 /bin/bash
root 141015:49 pts/000:00:00 ps -ef
[root@docker01 ~]# docker attach 1bf0f43c4d2f
[root@1bf0f43c4d2f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 10015:47 pts/000:00:00 /bin/bash
root 141015:49 pts/000:00:00 ps -ef
[root@docker01 ~]# docker exec -it clsn1 /bin/bash
[root@b20fa75b4b40 /]# 重新分配一个终端
[root@b20fa75b4b40 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 10016:11 pts/000:00:00 /bin/bash
root 130016:14 pts/100:00:00 /bin/bash
root 2613016:14 pts/100:00:00 ps -ef
[root@docker01 ~]# docker rm -f `docker ps -a -q`
# -f 强制删除
[root@docker01 ~]# docker run -d -p 8888:80 nginx:latest
287bec5c60263166c03e1fc5b0b8262fe76507be3dfae4ce5cd2ee2d1e8a89a9
docker run -P (大P)# 需要镜像支持
[root@docker01 ~]# docker run -d -p 80:80 -v /data:/usr/share/nginx/html nginx:latest
079786c1e297b5c5031e7a841160c74e91d4ad06516505043c60dbb78a259d09
[root@docker01 ~]# echo 'http://www.nmtui.com' >/data/index.html
[root@docker01 ~]# curl 10.0.0.100
http://www.nmtui.com
[root@docker01 ~]# docker run -d -p 8080:80 -v /data:/usr/share/nginx/html nginx:latest
351f0bd78d273604bd0971b186979aa0f3cbf45247274493d2490527babb4e42
[root@docker01 ~]# curl 10.0.0.100:8080
http://www.nmtui.com
[root@docker01 ~]# docker volume ls
DRIVER VOLUME NAME
[root@docker01 ~]# docker volume create
f3b95f7bd17da220e63d4e70850b8d7fb3e20f8ad02043423a39fdd072b83521
[root@docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local f3b95f7bd17da220e63d4e70850b8d7fb3e20f8ad02043423a39fdd072b83521
[root@docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local clsn
local f3b95f7bd17da220e63d4e70850b8d7fb3e20f8ad02043423a39fdd072b83521
[root@docker01 ~]# docker volume inspect clsn
[
{
'CreatedAt': '2018-02-01T00:39:25+08:00',
'Driver': 'local',
'Labels': {},
'Mountpoint': '/var/lib/docker/volumes/clsn/_data',
'Name': 'clsn',
'Options': {},
'Scope': 'local'
}
]
[root@docker01 ~]# docker run -d -p 9000:80 -v clsn:/usr/share/nginx/html nginx:latest
1434559cff996162da7ce71820ed8f5937fb7c02113bbc84e965845c219d3503
# 宿主机测试
[root@docker01 ~]# echo 'blog.nmtui.com' >/var/lib/docker/volumes/clsn/_data/index.html
[root@docker01 ~]# curl 10.0.0.100:9000
blog.nmtui.com
[root@docker01 ~]# docker run -d -P --volumes-from 079786c1e297 nginx:latest
b54b9c9930b417ab3257c6e4a8280b54fae57043c0b76b9dc60b4788e92369fb
[root@docker01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1400/sshd
tcp 0 0 10.0.0.100:2375 0.0.0.0:* LISTEN 26218/dockerd
tcp6 0 0 :::9000 :::* LISTEN 32015/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 31853/docker-proxy
tcp6 0 0 :::80 :::* LISTEN 31752/docker-proxy
tcp6 0 0 :::22 :::* LISTEN 1400/sshd
tcp6 0 0 :::32769 :::* LISTEN 32300/docker-proxy
[root@docker01 ~]# curl 10.0.0.100:32769
http://www.nmtui.com
[root@docker01 ~]# docker pull centos:6.8
[root@docker01 ~]# docker run -it -p 1022:22 centos:6.8 /bin/bash
# 在容器种安装sshd服务,并修改系统密码
[root@582051b2b92b ~]# yum install openssh-server -y
[root@582051b2b92b ~]# echo 'root:123456' |chpasswd
[root@582051b2b92b ~]# /etc/init.d/sshd start
[root@docker01 ~]# docker commit brave_mcclintock centos6-ssh
[root@docker01 ~]# docker run -d -p 1122:22 centos6-ssh:latest /usr/sbin/sshd -D
5b8161fda2a9f2c39c196c67e2eb9274977e7723fe51c4f08a0190217ae93094
[root@5b8161fda2a9 /]# yum install httpd -y
[root@5b8161fda2a9 /]# cat init.sh
#!/bin/bash
/etc/init.d/httpd start
/usr/sbin/sshd -D
[root@5b8161fda2a9 /]# chmod +x init.sh
# 注意执行权限
[root@docker01 ~]# docker commit 5b8161fda2a9 centos6-httpd
sha256:705d67a786cac040800b8485cf046fd57b1828b805c515377fc3e9cea3a481c1
[root@docker01 ~]# docker run -d -p 1222:22 -p 80:80 centos6-httpd /init.sh
46fa6a06644e31701dc019fb3a8c3b6ef008d4c2c10d46662a97664f838d8c2c
基础镜像信息 FROM centos:6.8
制作镜像操作指令RUN yum insatll openssh-server -y
容器启动时执行指令 CMD ['/bin/bash']
FROM 这个镜像的妈妈是谁?(指定基础镜像)
MAINTAINER 告诉别人,谁负责养它?(指定维护者信息,可以没有)
RUN 你想让它干啥(在命令前面加上RUN即可)
ADD 给它点创业资金(COPY文件,会自动解压)
WORKDIR 我是cd,今天刚化了妆(设置当前工作目录)
VOLUME 给它一个存放行李的地方(设置卷,挂载主机目录)
EXPOSE 它要打开的门是啥(指定对外的端口)
CMD 奔跑吧,兄弟!(指定容器启动后的要干的事情)
COPY 复制文件
ENV 环境变量
ENTRYPOINT 容器启动后执行的命令
# 创建目录
[root@docker01 base]# cd /opt/base
# 创建Dcokerfile文件,注意大小写
[root@docker01 base]# vim Dockerfile
FROM centos:6.8
RUN yum install openssh-server -y
RUN echo 'root:123456' |chpasswd
RUN /etc/init.d/sshd start
CMD ['/usr/sbin/sshd','-D']
[root@docker01 base]# docker image build -t centos6.8-ssh .
-t 为镜像标签打标签 . 表示当前路径
[root@docker01 base]# docker run -d -p 2022:22 centos6.8-ssh-b
dc3027d3c15dac881e8e2aeff80724216f3ac725f142daa66484f7cb5d074e7a
FROM centos:6.8
RUN yum install wget unzip php php-gd php-mbstring -y && yum clean all
# 设置工作目录,之后的操作都在这个目录中
WORKDIR /var/www/html/
RUN wget -c http://static.kodcloud.com/update/download/kodexplorer4.25.zip
RUN unzip kodexplorer4.25.zip && rm -f kodexplorer4.25.zip
RUN chown -R apache.apache .
CMD ['/usr/sbin/apachectl','-D','FOREGROUND']
# 创建一个nginx容器
docker run -d -p 80:80 nginx
# 创建容器,做link,并进入容器中
docker run -it --link quirky_brown:web01 centos-ssh /bin/bash
# 在容器中访问nginx容器可以ping通
ping web01
# 启动apache容器
[root@docker01 ~]# docker run -d httpd:2.4
3f1f7fc554720424327286bd2b04aeab1b084a3fb011a785b0deab6a34e56955
^[[A[root@docker01 docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f1f7fc55472 httpd:2.4'httpd-foreground'6 seconds ago Up 5 seconds 80/tcp determined_clarke
# 拉取一个busybox 镜像
[root@docker01 ~]# docker pull busybox
# 启动容器
[root@docker01 ~]# docker run -it --link determined_clarke:web busybox:latest /bin/sh
/ #
# 使用新的容器访问最初的web容器
/ # ping web
PING web (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.058 ms
^C
--- web ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.058/0.058/0.058 ms
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_bin
docker run --name zabbix-java-gateway -t
-d zabbix/zabbix-java-gateway:latest
docker 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:latest
docker 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
# 获取token
[root@docker02 ~]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': 'Admin',
'password': 'zabbix'
},
'id': 1
}' http://10.0.0.100/api_jsonrpc.php
{'jsonrpc':'2.0','result':'d3be707f9e866ec5d0d1c242292cbebd','id':1}
docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry
[root@docker01 ~]# cat /etc/docker/daemon.json
{
'registry-mirrors': ['https://registry.docker-cn.com'],
'insecure-registries': ['10.0.0.100:5000']
}
[root@docker01 ~]# systemctl restart docker.service
[root@docker01 ~]# docker tag busybox:latest 10.0.0.100:5000/clsn/busybox:1.0
[root@docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6-ssh latest 3c2b1e57a0f5 18 hours ago 393MB
httpd 2.42e202f453940 6 days ago 179MB
10.0.0.100:5000/clsn/busybox 1.05b0d59026729 8 days ago 1.15MB
[root@docker01 ~]# docker push 10.0.0.100:5000/clsn/busybox
[root@docker01 clsn]# yum install httpd-tools -y
mkdir /opt/registry-var/auth/ -p
htpasswd -Bbn clsn 123456 > /opt/registry-var/auth/htpasswd
docker run -d -p 5000:5000 -v /opt/registry-var/auth/:/auth/ -e 'REGISTRY_AUTH=htpasswd' -e 'REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm' -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
# 登陆用户
[root@docker01 ~]# docker login 10.0.0.100:5000
Username: clsn
Password: 123456
Login Succeeded
# 推送镜像到仓库
[root@docker01 ~]# docker push 10.0.0.100:5000/clsn/busybox
The push refers to repository [10.0.0.100:5000/clsn/busybox]
4febd3792a1f: Pushed
1.0: digest: sha256:4cee1979ba0bf7db9fc5d28fb7b798ca69ae95a47c5fecf46327720df4ff352d size: 527
#认证文件的保存位置
[root@docker01 ~]# cat .docker/config.json
{
'auths': {
'10.0.0.100:5000': {
'auth': 'Y2xzbjoxMjM0NTY='
},
'https://index.docker.io/v1/': {
'auth': 'Y2xzbjpIenNAMTk5Ng=='
}
},
'HttpHeaders': {
'User-Agent': 'Docker-Client/17.12.0-ce (linux)'
}
}
# 下载pip软件
yum install -y python2-pip
# 下载 docker-compose
pip install docker-compose
mkdir ~/.pip/
cat > ~/.pip/pip.conf <<'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
[root@docker01 ~]# mkdir /opt/my_wordpress/
[root@docker01 ~]# cd /opt/my_wordpress/
[root@docker01 my_wordpress]# vim docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/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:
- /data/web_data:/var/www/html
ports:
- '8000:80'
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
[root@docker01 my_wordpress]# docker-compose up
#启动方法:docker-compose up
#后台启动方法:docker-compose up -d
[root@docker01 my_wordpress]# cat docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/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:
- /data/web_data:/var/www/html
ports:
- '80'
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
[root@docker01 my_wordpress]# docker-compose scale wordpress=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... done
[root@docker01 ~]# yum install haproxy -y
[root@docker01 ~]#cp /etc/haproxy/haproxy.cfg{,.bak}
[root@docker01 ~]# vim /etc/haproxy/haproxy.cfg
global
log127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats level admin #支持命令行控制
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats
mode http
bind 0.0.0.0:8888
stats enable
stats uri /haproxy-status
stats auth admin:123456
frontend frontend_www_example_com
bind 10.0.0.100:8000
mode http
option httplog
log global
default_backend backend_www_example_com
backend backend_www_example_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
balance roundrobin
server web-node1 10.0.0.100:32768 check inter 2000 rise 30 fall 15
server web-node2 10.0.0.100:32769 check inter 2000 rise 30 fall 15
systemctl start haproxy
systemctl enable haproxy
yum install socat.x86_64 -y
[root@docker01 web_data]# echo 'help'|socat stdio /var/lib/haproxy/stats
echo 'disable server backend_www_example_com/web-node2'|socat stdio /var/lib/haproxy/stats
echo 'enable server backend_www_example_com/web-node3'|socat stdio /var/lib/haproxy/stats
[root@docker01 web_data]# vim check.php
<html>
<head>
<title>PHP测试</title>
</head>
<body>
<?php echo '<p>Hello World </p>'; ?>
<?php echo '访问的服务器地址是:'.'<fontcolor=red>'.$_SERVER['SERVER_ADDR'].'</font>'.'<br>';
echo'访问的服务器域名是:'.'<fontcolor=red>'.$_SERVER['SERVER_NAME'].'</font>'.'<br>';
?>
</body>
</html>
docker run --restart=always
# 添加上下面这行
'live-restore': true
[root@docker02 ~]# cat /etc/docker/daemon.json
{
'registry-mirrors': ['https://registry.docker-cn.com'],
'graph': '/opt/mydocker', # 修改数据的存放目录到/opt/mydocker/,原/var/lib/docker/
'insecure-registries': ['10.0.0.100:5000'],
'live-restore': true
}
[root@docker01 ~]# systemctl restart docker.service
[root@docker01 ~]# docker run -it --network none busybox:latest /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
[root@docker01 ~]# docker run -it --network container:mywordpress_db_1 busybox:latest /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
105: eth0@if106: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@docker01 ~]# docker run -it --network host busybox:latest /bin/sh
[root@docker01 ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
b15e8a720d3b bridge bridge local
345d65b4c2a0 host host local
bc5e2a32bb55 mywordpress_default bridge local
ebf76eea91bb none null local
wget https://github.com/jpetazzo/pipework/archive/master.zip
unzip master.zip
cp pipework-master/pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework
yum install bridge-utils.x86_64 -y
# 修改eth0配置,让br0实现桥接
[root@docker01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
[root@docker01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=10.0.0.100
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
DNS1=223.5.5.5
# 重启网络
[root@docker01 ~]# /etc/init.d/network restart
pipework br0 $(docker run -d -it -p 6880:80 --name httpd_pw httpd) 10.0.0.220/24@10.0.0.254
[root@docker01 ~]# curl 10.0.0.220
<html><body><h1>It works!</h1></body></html>
[root@docker01 ~]# ping 10.0.0.220 -c 1
PING 10.0.0.220 (10.0.0.220) 56(84) bytes of data.
64 bytes from 10.0.0.220: icmp_seq=1 ttl=64 time=0.043 ms
pipework br0 $(docker run -d -it --net=none --name test httpd:2.4) 10.0.0.221/24@10.0.0.254
[root@docker01 ~]# curl 10.0.0.221
<html><body><h1>It works!</h1></body></html>
pipework br0 testduliip 172.16.146.113/24@172.16.146.1 pipework br0 testduliip01 172.16.146.112/24@172.16.146.1
[root@docker01 ~]# docker network create --driver macvlan --subnet 10.1.0.0/24 --gateway 10.1.0.254 -o parent=eth0 macvlan_1
33a1f41dcc074f91b5bd45e7dfedabfb2b8ec82db16542f05213839a119b62ca
ip link set eth0 promisc on
[root@docker02 ~]# docker run -it --network macvlan_1 --ip=10.1.0.222 busybox /bin/sh
[root@docker01 harbor]# pwd
/opt/harbor
[root@docker01 harbor]# docker-compose stop
cd /opt && https://storage.googleapis.com/harbor-releases/harbor-offline-installer-v1.3.0.tgz
tar xf harbor-offline-installer-v1.3.0.tgz
[root@docker01 harbor]# vim harbor.cfg
···
hostname = 10.0.0.100
harbor_admin_password = Harbor12345
···
[root@docker01 harbor]# ./install.sh
[root@docker02 ~]# docker tag centos:6.8 10.0.0.100/clsn/centos6.8:1.0
[root@docker02 ~]#
[root@docker02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 5b0d59026729 8 days ago 1.15MB
10.0.0.100/clsn/centos6.81.06704d778b3ba 2 months ago 195MB
centos 6.86704d778b3ba 2 months ago 195MB
[root@docker02 ~]# docker login 10.0.0.100
Username: admin
Password:
Login Succeeded
[root@docker02 ~]# docker push 10.0.0.100/clsn/centos6.8
The push refers to repository [10.0.0.100/clsn/centos6.8]
e00c9229b481: Pushing 13.53MB/194.5MB
方式一:关注微信公众号,回复“Docker实战文档”获取下载链接。
方式二:知识星球下载,获取更多资料。
Docker实战文档(Part1)
Docker实战文档(Part2)
Kubernetes基础介绍
Docker容器基础介绍
作者:惨绿少年 来源:https://www.cnblogs.com/clsn/p/8410309.html
高端微信群介绍 | |
创业投资群 | AI、IOT、芯片创始人、投资人、分析师、券商 |
闪存群 | 覆盖5000多位全球华人闪存、存储芯片精英 |
云计算群 | 全闪存、软件定义存储SDS、超融合等公有云和私有云讨论 |
AI芯片群 | 讨论AI芯片和GPU、FPGA、CPU异构计算 |
5G群 | 物联网、5G芯片讨论 |
第三代半导体群 | 氮化镓、碳化硅等化合物半导体讨论 |
存储芯片群 | DRAM、NAND、3D XPoint等各类存储介质和主控讨论 |
汽车电子群 | MCU、电源、传感器等汽车电子讨论 |
光电器件群 | 光通信、激光器、ToF、AR、VCSEL等光电器件讨论 |
渠道群 | 存储和芯片产品报价、行情、渠道、供应链 |
< 长按识别二维码添加好友 >
加入上述群聊
带你走进万物存储、万物智能、
万物互联信息革命新时代