7 docker容器演练
一 环境说明:
tomat mysql nginx 三个容器,如下图,目的让nginx反向代理tomcat,并且连接数据库
二 tomcat配置
里面跑了两个应用程序,一个是tomcat自带的程序端口8080,另一个是java -jar 启动的程序端口8000
demo-0.0.1-SNAPSHOT.jar 这个jar包里面填写了数据库的一些信息
启动tomcat 容器
docker run -p 8080:8080 -d --rm --name tomcat_jdk -v /mydockerfile/tomcat/test:/usr/local/apache-tomcat-7.0.63/webapps/test -v /mydockerfile/tomcat/tomcatlogs/:/usr/local/apache-tomcat-7.0.63/logs --privileged=true hnftomcat java -jar /usr/local/apache-tomcat-7.0.63/webapps/test/demo-0.0.1-SNAPSHOT.jar
三 nginx配置
反向代理了两个应用,分别是8080和8000
1 nginx配置文件如下:
upstream tm {
server 172.17.0.2:8000; #tomcat容器的ip,jar包的端口
}
upstream tomcat {
server 172.17.0.2:8080; #tomat的端口
}
server {
listen 80;
server_name www.tomcat.com;
access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name www.hu.com;
access_log /var/log/nginx/host.access.log main;
location /pc/ {
alias /usr/share/nginx/html/;
index index.html index.htm;
}
location / {
proxy_pass http://tm;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /tomcat/ {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
2 启动nginx容器
docker run -p 80:80 --rm -v /mydockerfile/nginx/conf:/etc/nginx/conf.d/ -v /mydockerfile/nginx/logs/:/var/log/nginx -v /mydockerfile/nginx/html/:/usr/share/nginx/html -d nginx
浏览器访问 ,访问之前,绑定两个域名
http://www.hu.com/pc/这里面的数据是从数据库读取过来的
下面是代理的tomcat,www.tomcat.com
四 启动mysql容器
docker run -p 3306:3306 --name mysql --rm -v /mydockerfile/mysql/conf:/etc/mysql/conf.d -v /mydockerfile/mysql/logs:/logs -v /mydockerfile/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6