Overall environment configuration
The configuration of the overall environment, if one Dockerfile to write, then it is quite troublesome, fortunately, Docker has a tool called docker-compose, we can use it to complete the configuration of the overall environment:
First, let's look at the contents of the DOCKER-COMPOSE.YML configuration file:
Version: "3"
Services
Mysql:
Container_name:mysql
image:192.168.1.30:5000/mysql:5.7 #从私有仓库拉镜像
Restart:always
Volumes
-./mysql/data/:/var/lib/mysql/#映射mysql的数据目录到宿主机, save data
-./MYSQL/CONF/MYSQLD.CNF:/ETC/MYSQL/MYSQL.CONF.D/MYSQLD.CNF #把mysql的配置文件映射到容器的相应目录
Ports
-"6,033:3,306"
Environment:
-mysql_root_password=123456
Nginx:
Container_name:nginx
Restart:always
Image:192.168.1.30:5000/nginx
Ports
-80:80
-443:443
-5,050:5,050
-4,040:4,040
Volumes
-./nginx/conf/nginx.conf:/etc/nginx/nginx.conf #映射nginx的配置文件到容器里
-./nginx/logs/:/var/log/nginx/
-./nginx/data/:/var/share/nginx/html/#映射nginx的网页目录到容器里
Links
-Tomcat:t1 #连接 Tomcat Mirror
Tomcat:
Container_name:tomcat
Restart:always
Image:192.168.1.30:5000/tomcat
Ports
-8,080:8,080
-8,009:8,009
Volumes
-./tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml #映射 Tomcat configuration file into the container
-./tomcat/webapps/web:/usr/local/tomcat/webapps/web #映射一个web服务
-./tomcat/logs/:/usr/local/tomcat/logs/
Links
-Mysql:m1 #连接数据库镜像
A total of three service sets, namely MySQL, Nginx, Tomcat, where the need to note is their volumes and links.
MySQL Environment configuration
First look at the simplest MySQL, it does not set links, because it is the other container to connect to it, do not need to set links. But MySQL's volumes is the most important, if not set up volumes, every time the Docker restart, or MySQL container restart, the database data will be nothing. So MySQL volumes set up MySQL generated data files need to be mapped to the host's./mysql/data directory, this directory can be self-determined, MySQL configuration file from the host's./mysql/conf/mysqld.conf Read, The content can be configured on its own.
nginx Environment Configuration
It also set up the volumes and links, this place links, established the connection with the Tomcat container, because Nginx is responsible for listening to 80 ports, Tomcat is responsible for monitoring 8080 ports, Nginx received dynamic Web pages need to be handled by Tomcat, to be forwarded to 8080 ports. In the Docker environment, the Nginx directly forwards the request to 8080,tomcat is not forwarded. So with Llinks, the value here is T1, which is the name.
In the nginx.conf file, add the following configuration:
On the HTTP side, add
Upstream Backend {#后台负载均衡容器及端口, this example is the name of a T1 tomcat container
Server t1:8080;
}
In Server Plus
Location/{
Proxy_pass Http://backend$request_uri;
Proxy_set_header Host $host: $server _port;
Proxy_set_header X-real-ip $remote _addr;
Client_max_body_size 10m;
}
Tomcat Environment Configuration
Volumes
-./tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml #映射 Tomcat configuration file into the container
-./tomcat/webapps/web:/usr/local/tomcat/webapps/web #映射一个web服务
The Server.xml file is configured in advance on the host, including Web pages, and the Web is mapped to the appropriate directory for the container.
Finally, execute the following command:
Docker-compose up or docker-compose-f *.yaml file name
Create a container. If the error does not occur, the creation succeeds.
General error, because the mount directory has errors, some are wrong, there are some mappings to the appropriate directory in the container is not correct, about the container directory I summarized as follows:
MYSQL::/ETC/MYSQL/MYSQL.CONF.D/MYSQLD.CNF for Profile location
Nginx::/etc/nginx/nginx.conf for configuration file location,/var/share/nginx/html/for Nginx web directory
Tomcat::/usr/local/tomcat/conf/server.xml The configuration file location for Tomcat,/usr/local/tomcat/webapps for the Web site Directory
Through the above we can not only create a mysql,nginx, tomcat and static separation site environment, at the same time we have a certain understanding of docker-compose, making it easy for us to complete the docker-compose of the introductory learning.
Docker-compose Getting Started example: one-click Deployment Nginx+tomcat+mysql