docker integration tomcat + nginx

Description: Test domain name nic1.com, the server / etc hosts and local computer hosts file settings / good resolution.

1, and pull tomcat mirror nginx

[root@Server1 ~]# docker pull tomcat:9.0.26

[root@Server1 ~]# docker pull nginx

2, run temporary container

Considering the persistent data, it is necessary to mount the container to the associated directory local disk, so the first run temporarily container, copy the desired directory. Here are the name of the temporary container tomcat-test and nginx-test

[root@Server1 ~]# docker run -d --name tomcat-test tomcat:9.0.26

[root@Server1 ~]# docker run -d --name nginx-test nginx 

[root@Server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47dba8844f41 nginx "nginx -g 'daemon of…" 9 hours ago Up 44 seconds 80/tcp nginx-test
4388ad13d9fb tomcat:9.0.26 "catalina.sh run" 2 days ago Up 2 days 8080/tcp tomcat-test

3, operation and data persistence containers

tomcat persistent

The actual generation environment, we use more of tomcat configuration files, logs and webapps (used to store war package). Hence the need for persistence directory conf, logs, webapps. After entering the container where the default path is / usr / local / tomcat, and the tomcat files in that directory.

Run temporary container

[root@Server1 ~]# docker exec -it tomcat-test bash

root@4388ad13d9fb:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work

Copy the file to a temporary container conf Local / data / tomcat directory

[root@Server1 ~]# docker cp tomcat-test:/usr/local/tomcat/conf /data/tomcat/conf

[root@Server1 ~]# ls /data/tomcat/conf/
Catalina catalina.policy catalina.properties context.xml jaspic-providers.xml jaspic-providers.xsd logging.properties server.xml tomcat-users.xml tomcat-users.xsd web.xml

Run officially container, for container conf, logs, webapps directory persistent mount

[root@Server1 ~]# docker run -d --name tomcat-server -p 8080:8080 -v /data/tomcat/conf:/usr/local/tomcat/conf -v /data/tomcat/logs:/usr/local/tomcat/logs -v /data/tomcat/webapps:/usr/local/tomcat/webapps tomcat:9.0.26

[root@Server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
98ba15bf63dc tomcat:9.0.26 "catalina.sh run" 10 hours ago Up 4 hours 0.0.0.0:8080->8080/tcp tomcat-server

Http://nic1.com:8080 server can be accessed through the normal port, test.

 

nginx endurance of

Nginx is running a temporary container

[root@Server1 ~]# docker run -d --name nginx-test nginx

[root@Server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47dba8844f41 nginx "nginx -g 'daemon of…" 10 hours ago Up 53 minutes 80/tcp nginx-test

Enter the temporary container, where the default path is /, nginx configuration file located in / etc / nginx, static pages located in / usr / share / nginx / html, two copies of the configuration directory

[root@Server1 ~]# docker exec -it nginx-test bash
root@47dba8844f41:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@47dba8844f41:/# ls /etc/nginx/
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@47dba8844f41:/# ls /usr/share/nginx/html/
50x.html index.html

Copy temporary container configuration file to a local disk / data / nginx directory, copy the file to a temporary site container / data / www directory on the local disk, and run the official container

[root@Server1 ~]# docker cp nginx-test:/etc/nginx /data/nginx
[root@Server1 ~]# docker cp nginx-test:/usr/share/nginx/html /data/www
[root@Server1 ~]# docker run -d -p 80:80 --name nginx-server -v /data/nginx:/etc/nginx -v /usr/local/nginx:/data/www nginx

By http://nic1.com, tests can be normal access to the nginx default page.

 

4, Nginx + Tomcat integration

By nginx anti behalf tomcat, to avoid access by ip port or domain name in the form + + port.

1) Configure nginx web hosting. According to the foregoing, the container is mounted to the local configuration directory / data / nginx directory, so the directory /data/nginx/conf.d, add virtual hosts.

Briefly configuration is as follows

[root@Server1 ~]# cd /data/nginx/conf.d/
[root@Server1 conf.d]# cat nic1.conf
server{
  listen 80;
  server_name nic1.com;

  location / {
    proxy_pass http://172.18.0.4:8080;
  }
}

Description : Here on behalf of the anti-IP is the IP 172.18.0.4 tomcat container, not here on behalf of the anti-127.0.0.1:8080, otherwise inaccessible to the tomcat container, will complain nginx 502, IP container by docker inspect tomcat-server | grep "IPAddress" View

[root@Server1 ~]# docker inspect tomcat-server|grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.18.0.4",
"IPAddress": "172.18.0.4",

 

Guess you like

Origin www.cnblogs.com/sunnynic/p/12315753.html