Using docker to separate service and database

Time:2020-10-20

The previous article briefly introduced how to configure a simple Tomcat on the host. This blog is an extension of the previous one

Configuration target:

Separate the server and data in the project
This project uses Tomcat as the server and MySQL as the database, that is to create docker containers for them, as shown in the figure below
Using docker to separate service and database

1、 Create MySQL container

Since you need to connect to the database when you start the project, you need to configure the database container first
1、First, download the image of the Ubuntu system with MySQL and start it. I installed MySQL directly on the Ubuntu system. The installation command is as follows

 sudo apt-get install mysql-server mysql-client

2、If you need other containers to access the database container, you need to modify mysql
1) Modification my.cnf file

$vi /etc/mysql/my.cnf 
    
    Modify bind address = 127.0.0.1 to bind address = 0.0.0.0

2) Modify database user information
Enter MySQL

    $mysql -uroot -p root 
    $use mysql
    $update user set Host='%' where Host='localhost'
    $flush privileges

3) Load the SQL file of the project

    $create database mydb
    $use mydb
    $source /home/john/mydb.sql

4) Configure MySQL self start
Since the MySQL in the image stops running every time a mirror is committed, MySQL inside the docker container needs to be started at the same time when the image is started. The dockerfile is used to build the image that automatically starts the internal services. The dockerfile instruction ignores case. It is recommended to use uppercase instead of uppercase# As a comment, each line only supports one instruction, and each instruction can carry multiple parameters. Create a dockerfile in any path

$vi dockerfile 
$# Pull image  
FROM portal:mysqlcommit
MAINTAINER wangyk
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh It is applicable to automatically run the script when running a docker image. At this time, the script needs to be created in the docker container to be submitted

$vi /entrypoint.sh
$#!/bin/sh -e
/etc/init.d/mysql start
bash

After creation, submit the modified docker image

$sudo docker commit - M "MySQL final version" - a "wyk1184" < containerid > 1 mysql:mysqlfinal
perhaps
$sudo docker commit - M = MySQL final - a = wyk1184 < containerid > 0 mysql:mysqlfinal

After creation, you can find the saved image in images. At this time, you need dockerfile to build the image and generate a new image

$sudo docker build -t mysql/mysqlfinal .

After creating the latest image, start the image

$sudo docker run -i -t <imagesId> /bin/bash

Gets the assigned IP address of the container

2、 Configure system services

1. First of all, configure Tomcat in the docker image. In the previous blog, how to configure Tomcat is described in detail. One of the things to pay attention to is the configuration of environment variables. In the previous blog, it was introduced that ~ /. Bashrc needs to be modified However, in the subsequent self starting service, I found that the script could not find the environment variable when starting the service. Therefore, it is recommended that the environment variable be found in the bin directory of Tomcat startup.sh As shown above, write the data of environment variables in the file, and assign them to the system environment variables when the script starts,

export JAVA_HOME=/usr/local/jdk1.8.0_60
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

It can solve the problem of missing environment variables (configure environment variables and modify the / etc / profile file, which will be sorted out later),
You also need to make one for this image entrypoint.sh Is used for self starting in this container

$vi /entrypoint.sh
$#!/bin/sh -e
sh /home/ai/tomcat/apache-tomcat-8-ai/bin/startup.sh
bash

Use commit to submit the image of the container and create the dockerfile

$vi dockerfile 
$# Pull image  
FROM portal:aivvvvv8
MAINTAINER wangyk
ENTRYPOINT ["/entrypoint.sh"]

Use build to create a new image

$sudo docker build -t portal/aivvvv8 .

Get a new image < ImageID >
Start a new Tomcat service

$sudo docker run -t -i -p 58081:8080 --add-host portalmysql.iscas-haiyun.com:172.17.0.5 1e3457670a0c /bin/bash

-P 58081: 8080 external mapping 58081 port
–add-host portalmysql.iscas – haiyun.com:172 . 17.0.5 add the address of the host
This will start the service

From Yakun