单机编排之-Docker Compose
Posted 小怪獣55
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单机编排之-Docker Compose相关的知识,希望对你有一定的参考价值。
docker 单机编排工具:docker compose
docker compose是docker容器的一种编排服务
docker compose是一个管理多个容器的工具,比如可以解决容器之间的依赖关系,就像启动一个web就必须得先把
数据库服务先启动一样,docker compose 完全可以替代docker run启动容器
github地址:https://github.com/docker/compose
基础环境准备
#python环境和Pip命令
yum install https://mirrors.aliyun.com/epel/epel-release-latest 7.noarch.rpm -y
yum install python-pip -y
pip install --upgrade pip
#安装docker compose
pip install docker-compose
docker-compose versin
docker-compose --help
docker compose启动单个容器
mkdir docker-compose
cd docker-compose
#设置一个yml格式的配置文件,因此要注意前后的缩进
# cat docker-compose.yml
web1:
image: 192.168.10.206/nginx/nginx_1.10.3
expose:
-80
-443
container_ name: nginx web1 #自定义容器名称
ports:
-"80:80"
-"443:443"
必须要在docker compose文件所在的目录执行
docker-compose up #第一次启动如果本地没有镜像,会从仓库拉取镜像;前台启动
docker-compose up -d #后台启动
#查看容器进程
docker-compose ps
docker compose启动多个容器
# cat docker compose.yml
web1:
image: 192.168.10.206/nginx/nginx_1.10.3
expose:
-80
-443
container_name:ngix-web1 #自定义容器名称
ports:
-"80:80"
-"443:443"
web2:
image: 192.168.10.206/nginx/nginx_1.10.3
expose:
-80
-443
container_name:ngix-web2
ports:
-"81:80"
-"444:443"
#重新启动容器
docker-compose stop
docker-compose up -d
定义数据卷挂载
mkdir -p /data/nginx
echo "Test Nginx Volume" > /data/nginx/index.html
vim docker-compose.yml
-------------------------------------------------------------
web1:
image: 192.168.10.206/nginx/nginx_1.10.3
expose:
-80
-443
container_name: nginx-web1
volumes:
-/data/nginx:/usr/local/nginx/html
ports:
-"80:80"
-"443:443"
web2:
image: 192.168.10.206/nginx/nginx_1.10.3
expose:
-80
-443
container_name: nginx-web2
ports:
-"81:80"
-"444:443"
-------------------------------------------------------------
docker-compose stop
docker-compose up -d
同一个文件,数据卷的优先级比镜像内的文件优先级高
#重启单个指定容器
docker-compose restart web1
#重启所有容器
docker-compse restart
#停止和启动单个容器
docker-compose stop web1
docker-compose start web1
#停止和启动所有容器
docker-compose stop
docker-compose start
实现单机版的 HA+Nginx+Tomcat
vim docker-compose.yml
-------------------------------------------------------------
nginx-web1:
image: 192.168.10.205/nginx/nginx_1.10.3
expose:
-80
-443
container_name: nginx-web1
volumes:
-/data/nginx:/usr/local/nginx/html
-/usr/local/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf
links:
-tomcat web1
-tomcat web2
nginx-web2:
image: 192.168.10.205/nginx/nginx_1.10.3
expose:
-80
-443
container_name: nginx-web2
volumes:
-/usr/local/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf
links:
-tomcat web1
-tomcat web2
tomcat-web1:
container_name: tomcat-web1
image: 192.168.10.205/centos/jdk1.7.0.79_tomcat1.7.0.69
user: www
command: /apps/tomcat/bin/run_tomcat.sh
volumes:
-/apps/tomcat/webapps/SalesManager:/apps/tomcat/webapps/SalesManager
expose:
-8080
-8443
tomcat-web2:
container_name: tomcat-web2
image: 192.168.10.205/centos/jdk1.7.0.79_tomcat1.7.0.69
user: www
command: /apps/tomcat/bin/run_tomcat.sh
volumes:
-/apps/tomcat/webapps/SalesManager:/apps/tomcat/webapps/SalesManager
expose:
-8080
-8443
haproxy:
container_name: haproxy-web1
image: 192.168.10.205/centos/centos_7.2.1511_haproxy_1.7.9
command: /root/script/run_haproxy.sh
volumes:
-/etc/haproxy/haproxy.cfg:/etc/haproxy/haproxy.cfg
ports:
-"9999:9999"
-"80:80"
links:
-nginx-web1
-nginx-web2
docker-compose up -d
docker-compose logs -f
showhost.jsp
<%@page import="java.util.Enumeration"%>
<br /
host:
<%tryout.println(""+java.net.InetAddress.getLocalHost().getHostName());catch(Exc
eption e)%>
<br />
remoteAddr: <%=request.getRemoteAddr()%>
<br />
remoteHost: <%=request.getRemoteHost()%>
<br />
sessionId: <%=request.getSession().getId()%>
<br />
serverName:<%=request.getServerName()%>
<br />
scheme:<%=request.getScheme()%>
<br />
<%request.getSession().setAttribute("t1","t2");%>
<%
Enumeration en = request.getHeaderNames();
while(en.hasMoreElements())
String hd = en.nex tElement().toString();
out.println(hd+" : "+request.getHeader(hd));
out.println("<br />");
%>
nginx配置
upstream tomcat_webserver
server tomcat web1:8080;
server tomcat web2:8080;
server
location /SalesManager
proxy_pass http://tomcat_webserver;
proxy_set_header Host $host;
proxy_set_header X Forwarded For $proxy_add_x_forwarded_for;
prox y_set_header X Real IP $remote_addr;
以上是关于单机编排之-Docker Compose的主要内容,如果未能解决你的问题,请参考以下文章