The method of using Mysql in Tomcat containers under Docker

  • 2020-12-07 04:35:22
  • OfStack

Here we use the Tomcat container to run the war package, but as a website and a program, how do we connect to Mysql in the Tomcat container if we need to use the database?

pull already has tomcat and mysql mirroring. You can use docker's connection system (link) to connect multiple containers instead of using mysql in tomcat containers


[root@izbp1b5k5bjps0dw8owk7tz ~]# docker images
REPOSITORY      TAG         IMAGE ID      CREATED       SIZE
tomcat        latest       2d084b11164d    7 days ago     463MB
mysql        5.7.22       66bc0f66b7af    2 weeks ago     372MB
[root@izbp1b5k5bjps0dw8owk7tz ~]# docker ps
CONTAINER ID    IMAGE        COMMAND       CREATED       STATUS       PORTS        NAMES
[root@izbp1b5k5bjps0dw8owk7tz ~]# 

Start the mysql


docker run --name mysql -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.221

Command description:

The & # 8226; The alias is name: Custom alias
The & # 8226; -ES25en 3306:3306: Map port 3306 of the container to port 3306 of the host
The & # 8226; - v/home/mysql/conf: / etc mysql/conf d: will host/home mysql/conf/my cnf mounted to the container/etc/mysql/my cnf
The & # 8226; - v/home/mysql data: / var/lib/mysql: will host/home mysql/data directory mounted to the container/var lib/mysql
The & # 8226; -ES59en MYSQL_ROOT_PASSWORD=123456: Initializes the password for root user
The & # 8226; -ES65en: Run as a daemon

Note: Run mysql exposed port (-ES69en 3306:3306), if in production environment, the port can also not be exposed for safety, after running tomcat container can use mysql by specifying container (--link)

Run tomcat


docker run --name tomcat -p 8080:8080 -v /etc/localtime:/etc/localtime:ro -v /home/tomcat/logs:/usr/local/tomcat/logs -d --link mysql:db tomcat1

Command description:

The & # 8226; The alias is name: Custom alias
The & # 8226; -ES83en 8080:8080: Maps port 8080 of the container to port 8080 of the host
The & # 8226; - v/etc/localtime: / etc/localtime: ro: mount localtime files into the container, and ensure that both of the time zone is 1
The & # 8226; - v/home/tomcat/logs: / usr local/tomcat/logs: will host/home tomcat/logs directory mounted to the container/usr local/tomcat/logs
The & # 8226; -ES109en: Run as a daemon
The & # 8226; link mysql:db: Tells the current container that it needs to use the mysql container and names it db

This allows access to mysql from db in the tomcat container

Attachment: Database connection reference statement in Java:


jdbc_url=jdbc:mysql://db:3306/databasename?useUnicode=true&characterEncoding=utf8
[root@izbp1b5k5bjps0dw8owk7tz mysql]# docker run --name mysql -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.22
c7378e84c19b0baa448d687c089ef8cc0ce058f71b2f79d8801fc9be7bd2c5b4
[root@izbp1b5k5bjps0dw8owk7tz mysql]# docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
c7378e84c19b    mysql:5.7.22    "docker-entrypoint.s ... "  6 seconds ago    Up 5 seconds    0.0.0.0:3306->3306/tcp  mysql
[root@izbp1b5k5bjps0dw8owk7tz logs]# docker run --name tomcat -p 8080:8080 -v /etc/localtime:/etc/localtime:ro -v /home/tomcat/logs:/usr/local/tomcat/logs -d --link mysql:db tomcat
1458f532ef36e12ad49b4a5d90ff9b38abed00986094225354594a5fe7591362
[root@izbp1b5k5bjps0dw8owk7tz logs]# docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
1458f532ef36    tomcat       "catalina.sh run"    3 seconds ago    Up 2 seconds    0.0.0.0:8080->8080/tcp  tomcat
c7378e84c19b    mysql:5.7.22    "docker-entrypoint.s ... "  About a minute ago  Up About a minute  0.0.0.0:3306->3306/tcp  mysql

conclusion


Related articles: