The deployment steps are described as follows

  1. The Docker service is deployed on the host
  2. Docker. IO selects the desired image version and pulls it to the host
  3. Docker deploys mysql and initializes it
  4. Deploy Tomcat and place the package in the Tomcat WebApps
  5. Nginx deployment, domain name configuration

The Docker service is deployed on the host

  • Docker yum install dependencies
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
  • View the current version of the Docker under yum and install the selected version
-- Showduplicates yum install -y Docker-ce-18.09.9-3.el7.x86_64Copy the code
  • Modify the default docker data directory (optional if the/directory space is small)
vim /usr/lib/systemd/system/docker.service
Graph /home/ dockerData = graph /home/ dockerData
ExecStart=/usr/bin/dockerd  --graph /home/dockerdata
Copy the code
  • Configuration of image acceleration due to domestic access to docker. IO is not stable, Can choose to use ali free accelerate the address Accelerate the address in the https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors for the/etc/docker/daemon. Json file add the following content
{
  "live-restore": true."registry-mirrors": ["https://kb56o5ld.mirror.aliyuncs.com"]}Copy the code
  • Start Docker and configure boot
systemctl start docker && systemctl enable docker
Copy the code

Select the mirroring version you want

  • Pull the mirror
Docker pull nginx: 1.16.1&& docker pull mysql: 5.7&& Docker pull Tomcat :8.5Copy the code

Deploy mysql

  • Create a mysql data directory on the host
mkdir -p /home/Data/mysql
Copy the code
  • Port and directory mappings
docker run -d -p 3306:3306 --name mysql -v /home/mysqldata:/var/lib/mysql \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD="Password"Mysql: 5.7Copy the code
  • Database initialization can be done using the mysql command line or navicat

To deploy tomcat

  • The host creates a new package directory
mkdir -p /home/App/webapps
Copy the code
  • Start the container and map port 8080 of the container to host 8008
  • Link mysql container, which facilitates the use of environment variables to connect mysql services
docker run -d -p 8008:8080  \
-v  /home/App/webapps:/usr/local/ tomcat/webapps \ - v/etc/localtime: / etc/localtime: ro \ - link mysql: mysql \ - name tomcat tomcat: 8.5Copy the code
  • View environment variables in the Tomcat container if only one Tomcat-related container is running
docker exec -it `docker ps |grep tomcat|awk '{print $1}'` env
Copy the code

Or use docker PS to query the CONTAINER ID

docker exec -it ${CONTAINER ID} env
Copy the code

You can see that env has MYSQL related environment variables in the deployed Tomcat container

MYSQL_PORT = TCP: / / 172.17.0.2:3306 MYSQL_PORT_3306_TCP = TCP: / / 172.17.0.2:3306 MYSQL_PORT_3306_TCP_ADDR = 172.17.0.2 MYSQL_PORT_3306_TCP_PORT = 3306 MYSQL_PORT_3306_TCP_PROTO = TCP MYSQL_PORT_33060_TCP = TCP: / / 172.17.0.2:33060 MYSQL_PORT_33060_TCP_ADDR = 172.17.0.2 MYSQL_PORT_33060_TCP_PORT = 33060 MYSQL_PORT_33060_TCP_PROTO = TCP MYSQL_NAME=/tomcat/mysql MYSQL_ENV_MYSQL_ROOT_PASSWORD="Password"MYSQL_ENV_GOSU_VERSION = 1.7 5.7 MYSQL_ENV_MYSQL_VERSION MYSQL_ENV_MYSQL_MAJOR = = debian10 5.7.29-1Copy the code

Use the above environment variables for jDBC-related configuration in your application;

  • The package is placed in the Webapps

    Put the code war package or decompressed war package in /home/app/webapps. You cannot directly place the compiled content in the first layer directory.

    The reason is:

    The official tomcat image cannot directly identify files under Webapps, but only the contents of each directory under Webapps. That is, a single Tomcat can deploy multiple applications.

    Use IP:PORT/dist1/*** and IP:PORT/dist2/*** to distinguish the access address, so nginx is required to handle the path problem.

    The directory structure of the placed program content is as follows:


Deployed nginx

  • Create nginx configuration directory, log directory, etc
mkdir -p /home/Config/nginx
mkdir -p /home/Logs/nginx
mkdir -p /home/Data/nginx/html
Copy the code
  • Initialize nginx.conf on the host
  • [1] Start the temporary nginx container to get the configuration file
Docker run -itd nginx:1.16.1 docker cp${CONTAINER ID}:/etc/nginx/nginx.conf /home/Config/nginx/nginx.conf
docker cp ${CONTAINER ID}:/etc/nginx/conf.d /home/Config/nginx/
Delete the temporary container after the replication is complete
docker stop ${CONTAINER ID} && docker rm ${CONTAINER ID}
Copy the code

If you want to create a new nginx.conf file on the host machine, you will not be able to start the container docker because there is no configuration file.

  • [2] Start the nginx container
docker run  -d -p 8005:80 \
-v /home/Data/nginx/html:/usr/share/nginx/html \
-v /home/Config/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/Config/nginx/conf.d:/etc/nginx/conf.d \
-v /etc/localtime:/etc/localtime:ro \
-v /home/Logs/nginx:/var/log/nginx \
--name nginx nginx:1.16.1
Copy the code
  • Get the IP address of the Tomcat container
docker exec -it ${CONTAINER ID} ip a
Copy the code

  • Configuring Domain Name Proxy
Cp/home/Config/nginx/conf. D/default. Conf/home/Config/nginx/conf. D/test. The conf bash modify ` ` ` json server {listen 80;Change server_name to the desired domain name
    server_name  dist.timodiediedie.again;
    location / {
        Proxy to /dist/ when accessing/directory directlyProxy_pass http://172.17.0.3:8080/dist/; }Dist in #location /dist/ refers to the directory name under Webapps when Tomcat is deployed
    location /dist/ {
        /dist/*** proxy directly to the backend, no path processingProxy_pass http://172.17.0.3:8080; }}Copy the code

Note that different applications have different rules for URL processing. Some applications only need to configure location /. The priority of localtion /dist/ is higher than that of /, that is, when there is no location of other directories, / takes effect. The/takes effect last if another directory location exists.

  • Reload nginx configuration
docker exec -it ${CONTAINER ID} nginx -t
docker exec -it ${CONTAINER ID} nginx -s reload
Copy the code