Mirror installation
You can pull nginx and Tomcat images as tests by the following command
[[email protected]docker /]# docker pull nginx
[[email protected]ker /]# docker pull tomcat
nginx
- After downloading the image, execute the following command to start nginx
[[email protected] /]# docker run --name=my_nginx -p 8000:80 -d nginx
–Name: specify a name for nginx container for easy management
-P: proxy the internal 80 port of nginx to the 8000 port of the host throughHost: 8000
Access nginx 80 port
-D: background operation
- You can view the operation of the container through the docker PS command
[[email protected] /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1833fcff605b nginx "nginx -g 'daemon off" 2 minutes ago Up 2 minutes 0.0.0.0:8000->80/tcp my_nginx
- Browser access
http://192.168.43.32:8000/
See if you can access it, or use the curl command (recommended)
[[email protected] /]# curl http://192.168.43.32:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
....
Nginx profile
Enter nginx container background
[[email protected] /]# docker exec -it my_nginx bash
[email protected]:/# cd /etc/nginx/
[email protected]:/etc/nginx# ls -l
total 36
drwxr-xr-x. 2 root root 26 Dec 26 18:16 conf.d
-rw-r--r--. 1 root root 1007 Dec 26 11:11 fastcgi_params
-rw-r--r--. 1 root root 2837 Dec 26 11:11 koi-utf
-rw-r--r--. 1 root root 2223 Dec 26 11:11 koi-win
-rw-r--r--. 1 root root 5170 Dec 26 11:11 mime.types
lrwxrwxrwx. 1 root root 22 Dec 26 11:11 modules -> /usr/lib/nginx/modules
-rw-r--r--. 1 root root 643 Dec 26 11:11 nginx.conf
-rw-r--r--. 1 root root 636 Dec 26 11:11 scgi_params
-rw-r--r--. 1 root root 664 Dec 26 11:11 uwsgi_params
-rw-r--r--. 1 root root 3610 Dec 26 11:11 win-utf
Nginx.conf is the main configuration file of nginx. You can view nginx.conf through the more command (the container does not install VI tools by default)
[email protected]:/etc/nginx# more nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
Note the last line configurationinclude /etc/nginx/conf.d/*.conf;
Include can import other configuration files and enter/etc/nginx/conf.d/
View under directory
[email protected]:/etc/nginx# cd /etc/nginx/conf.d/
[email protected]:/etc/nginx/conf.d# ls
default.conf
default.conf
[email protected]:/etc/nginx/conf.d# more default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#Indicates that the comment can be ignored
Server: represents a virtual server. What is a virtual server
Server name: server name
Location: access path matching rules, which is also the most flexible place for nginx, regular expressions can be used
Explain what a virtual server is
If you configure two domain names on DNS for nginx server
domain1.nginx.com
domain2.nginx.com
When users access these two domain names, you may want them to access the same background service, or you may want to access different background services
In nginx, you can configure two virtual servers, that is, two server nodes
server {
listen 80;
server_name domain1.nginx.com;
...
}
server {
listen 80;
server_name domain2.nginx.com;
...
}
Two virtual servers listen to the same port 80. Nginx can proxy to different servers according to the host (HTTP header host) field accessed by users
Location – > root: static resource directory. Web static resources such as pictures, JS and HTML can be stored in this way
Location – > index: if the user does not specify the request resource name, access the file specified by index by default, such as accesshttp://host:port/html/
Default accesshttp://host:port/html/index.html
Test scenario
Let’s assume three scenarios
- Scene 1
The host has a directory to store static resources, which needs to be sent out through nginx agent and accessed by usershttp://host:port/resource/xxxx
Visit
- Scene 2
To run a web program on the Tomcat server (docker container), you need to proxy it out through nginx. The context root of the web program isWebTestApp
- Scene 3
The web program runs in two Tomcat containers and needs to do load balancing through nginx
Nginx profile mount
Every time the docker container is restarted, it is a new environment, that is to say, any changes made in the docker container will be restored, but the configuration file of nginx is in the docker container. If we make changes directly, it will be restored after every restart, which is not what we want, so we need to “move” the configuration file from the docker container to the host, which will pass the docker volume here (volume) implementation.
Volume can mount the local file to the docker container, so that the information will not be lost after the container is restarted
For nginx, you can “move” the nginx.conf file and the conf.d directory out of the container
Create nginx.conf file and conf.d directory locally
[[email protected] /]# mkdir -p /u01/nginx
[[email protected] /]# mkdir -p /u01/nginx/conf.d
[[email protected] /]# touch /u01/nginx/nginx.conf
[[email protected] /]# touch /u01/nginx/conf.d/default.conf
The nginx.conf file and the default.conf content can be copied directly from within the container
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Restart nginx
[[email protected] /]# docker stop my_nginx
my_nginx
[[email protected] /]# docker rm my_nginx
my_nginx
[[email protected] nginx]# docker run --name=my_nginx -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -p 8000:80 -d nginx
6efe91858f071a50197da104cdccf8500234f1bf6d0f4f56d3dc5de02261272c
[[email protected] /]# curl http://192.168.43.32:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
....
Note that a docker RM my ﹣ nginx command is executed. This is because the container needs to specify a different name each time it is started. A name cannot be used more than once. You can view all containers, including those that are not running, through docker ps-a. RM command can delete the specified container
The following three scenarios are formally configured
Scenario 1 – static resource agent
Create directory resource in u01 directory and upload static resource
[[email protected] resource]# pwd
/u01/resource
[[email protected] resource]# ll
total 164
-rw-r--r--. 1 root root 147291 Oct 8 2015 angular.min.js
-rw-r--r--. 1 root root 17189 Nov 3 10:32 docker.jpg
[[email protected] resource]#
Modify / u01 / nginx / conf.d/default.conf file
default.conf
server {
listen 80;
server_name localhost;
location /resource {
root /u01;
index index.html index.htm;
}
}
PS: root is / u01 not / u01 / resource. If root is configured as / u01 / resource /, nginx will go to the / u01 / resource / resource directory to find files
Restart the nginx container and mount the resource directory inside the container
[[email protected] u01]# docker stop my_nginx
my_nginx
[[email protected] u01]# docker rm my_nginx
my_nginx
[[email protected] u01]# docker run --name=my_nginx -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -v /u01/resource:/u01/resource -p 8000:80 -d nginx
Visit
http://192.168.43.32:8000/resource/docker.jpg
http://192.168.43.32:8000/resource/angular.min.js
Test whether the configuration is successful (replace the IP above with the host IP)
Scenario 2 – Tomcat agent
Prepare a server as a test
package com.df.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class WebTestService extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.print("hello docker tomcat");
out.close();
}
}
Make a war package named webtestapp.war. Create the webapps directory on u01 and upload the war package
[[email protected] webapps]# pwd
/u01/webapps
[[email protected] webapps]# ls
WebTestApp.war
Start the Tomcat container and mount the / u01 / webapps directory to the / usr / local / Tomcat / webapps directory of Tomcat
[[email protected] ~]# docker run --name=my_tomcat1 -v /u01/webapps:/usr/local/tomcat/webapps -p 8001:8080 -d tomcat
[[email protected] ~]# curl http://localhost:8001/WebTestApp
hello docker tomcat
The program has been successfully deployed to Tomcat
Modify / u01 / nginx / conf.d/default.conf file
default.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat_server;
}
}
Modify / u01 / nginx / nginx.conf to add the following configuration in the HTTP configuration node
upstream tomcat_server {
server t1:8080;
}
Upstream can define a set of servers
Proxy? Pass sets the proxy server in the format of http: / / upstream? Name
Restart nginx container, here we need to use a new docker parameter — link
[[email protected] u01]# docker run --name=my_nginx1 --link=my_tomcat1:t1 -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -p 8000:80 -d nginx
[[email protected] ~]# curl http://192.168.43.32:8000/WebTestApp
hello docker tomcat
PS: Here I change my name to my nginx 1. I can’t start the container with my nginx. The following error will be reported
2018/01/27 08:40:44 [emerg] 1#1: host not found in upstream "t1:8080" in /etc/nginx/nginx.conf:30
nginx: [emerg] host not found in upstream "t1:8080" in /etc/nginx/nginx.conf:30
T1 can’t be found, but if you don’t use — name to specify the name, it can start normally. The reason is unknown.
The link parameter can establish a network connection between two containers. The format is — link = my_tomcat1: T1 my_tomcat1 is the container name and T1 is the alias
You can log in the nginx container to view the / etc / hosts file, and docker will add T1 to the hosts file
[[email protected] ~]# docker exec -it my_nginx1 bash
[email protected]:/# more /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.5 t1 1f9b1d432ab0 my_tomcat1
172.17.0.6 fa5f782b9448
Scenario 3 – Tomcat load balancing
- Modify / u01 / nginx / nginx.conf to add a new server based on scenario 2
upstream tomcat_server {
server t1:8080;
server t2:8080;
}
- Starting a Tomcat container my “tomcat2
[[email protected] ~]# docker run --name=my_tomcat2 -v /u01/webapps:/usr/local/tomcat/webapps -d tomcat
- Restart nginx
[[email protected] /]# docker stop my_nginx1
[[email protected] /]# dcoker rm my_nginx1
[[email protected] /]# docker run --name=my_nginx1 --link=my_tomcat1:t1 --link=my_tomcat2:t2 -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -p 8000:80 -d nginx
[[email protected] ~]# curl http://192.168.43.32:8000/WebTestApp
hello docker tomcat
- You can log in to two Tomcat containers to view access.log to view the results. Nginx polls between two servers. Polling is the default load balancing method of nginx.