1. Docker 安裝Nginx
[root@langzi01 nginx]# pwd
/data/nginx
[root@langzi01 nginx]# ls
conf conf.d html logs
[root@langzi01 nginx]# docker search nginx
[root@langzi01 nginx]# docker pull nginx:1.14.2
[root@langzi01 nginx]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx 1.14.2 e706cb01fa6b 2 hours ago 109 MB
##運(yùn)行容器
[root@langzi01 nginx]# docker run --name mynginx -d -p 82:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx
命令說(shuō)明:
-p 80:80:將容器的80端口映射到主機(jī)的80端口
--name mynginx:將容器命名為mynginx
## -v $PWD/www:/www:將主機(jī)中當(dāng)前目錄下的www掛載到容器的/www
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:將主機(jī)中當(dāng)前目錄下的nginx.conf掛載到容器的/etc/nginx/nginx.conf
-v /data/nginx/logs:/var/log/nginx:將主機(jī)中當(dāng)前目錄下的logs掛載到容器的/var/log/nginx
其中nginx.conf的內(nèi)容如下
[root@langzi01 nginx]# cat conf/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://pic;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
upstream pic{
server 172.17.0.1:8088 weight=5;
server 172.17.0.1:8089 weight=5;
}
}
2. Docker 安裝Tomcat
[root@langzi01 tomcat]# pwd
/data/tomcat
[root@langzi01 tomcat]# ls
conf logs test webapps
[root@langzi01 test]# docker images|grep tomcat
docker.io/tomcat 8.5 168588387c68 3 weeks ago 463 MB
docker.io/tomcat latest 168588387c68 3 weeks ago 463 MB
#啟動(dòng)兩個(gè)tomcat服務(wù)
[root@langzi01 test]# docker run --name tomcat1 -p 8088:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
[root@langzi01 test]# docker run --name tomcat2 -p 8089:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
[root@langzi01 test]# docker ps -a|grep tomcat
4714c3831de7 tomcat "catalina.sh run" 41 minutes ago Up 41 minutes 0.0.0.0:8089->8080/tcp tomcat2
9b01100cf846 tomcat "catalina.sh run" 41 minutes ago Up 41 minutes 0.0.0.0:8088->8080/tcp tomcat1
# 訪問(wèn)
[root@langzi01 test]# curl '172.17.0.1:8088'
[root@langzi01 test]# curl '172.17.0.1:8089'
3. nginx代理tomcat服務(wù)器
upstream pic{
server 172.17.0.1:8088 weight=5;
server 172.17.0.1:8089 weight=5;
}
查看nginx的端口
[root@langzi01 test]# docker ps -a|grep nginx
cce508aa20bc docker.io/nginx "nginx -g 'daemon ..." About an hour ago Up 58 minutes 0.0.0.0:82->80/tcp mynginx
我的公網(wǎng)ip為:http://47.104.136.162 瀏覽器訪問(wèn):http://47.104.136.162:82/
結(jié)果如下: