Docker study notes five: advanced combat -- mysql+tomcat

1. mysql

Reference: https://hub.docker.com/_/mysql/

 

1. Download the mysql image

sudo docker pull mysql

 

2. Start the mysql container

sudo docker run -d -p 3306:3306 --name dbmysql -e MYSQL_ROOT_PASSWORD=password -v /mysql/datadir:/var/lib/mysql -v /mysql/conf:/etc/mysql/conf.d  docker.io/mysql:latest
-e MYSQL_ROOT_PASSWORD=password : Specify the root password
-v /mysql/datadir:/var/lib/mysql : Specify the local storage path of the database. If the system does not close SELinux, it will fail to start because the local directory is not allowed to be mounted to the container. You need to execute chcon -Rt svirt_sandbox_file_t /mysql first /datadir
-v /mysql/conf:/etc/mysql/conf.d : Specifies to use a custom mysql configuration file to start the database, such as creating a my-config.cnf in this path

vi my-config.cnf
[mysqld]
port=3306
character-set-server=utf8
wait_timeout=288000 # Link timeout, default is 8 hours, in seconds
lower_case_table_names=1 # Do not desensitize case

 

 

3. docker-compose method:

go to root user

mkdir -p /docker-compose-dir/mysql

cd /docker-compose-dir/mysql

vi docker-compose.yml

dbmysql:
  image: docker.io/mysql:latest
  ports:
    - 3306:3306
  environment:
    MYSQL_ROOT_PASSWORD: password
  volumes:
    - /mysql/datadir:/var/lib/mysql
    - /mysql/conf:/etc/mysql/conf.d:ro

 

 

 
[root@centos-linux-agent mysql]# docker-compose up -d
Creating mysql_dbmysql_1
[root@centos-linux-agent mysql]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
087f4e32cd29        docker.io/mysql:latest   "docker-entrypoint.sh"   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp   mysql_dbmysql_1
 
 

4. Database script initialization

Sometimes, the database initialization script needs to be executed before the project container can be run, which can be done as follows:
Script path: /mysql_sql/mysql.sql
sudo docker run -it --rm --link=mysql_dbmysql_1:db  -v /mysql_sql:/mysql_sql mysql:latest mysql -hdb -uroot -ppassword -Ddbname
 
Note: If the script contains a library building statement, you can remove the -D parameter
After entering the container execute:
mysql> source /mysql_sql/mysql.sql;
mysql> exit
After exiting the container, the container is automatically deleted.

 

 

Second, tomcat

Reference: https://hub.docker.com/_/tomcat/

 

 

Download mirror:

sudo docker pull tomcat

Currently the latest version is 8.0.X, you can find the required tag version of jre+tomcat through the official address above

 

Start the container:

sudo docker run -d -p 8080:8080 -v /usr/local/tomcat  tomcat:latest

 

View the path where the tomcat directory in the container is mounted to this machine:

sudo docker ps 

 

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef993d0f48c1 tomcat:latest "catalina.sh run"22 minutes ago Up9 minutes 0.0.0.0:8080->8080/tcp goofy_blackwell

 

 

 

sudo docker inspect -f "{{.Mounts}}" ef9
[{bbc41e0bbc97ae32839e876f291eb60173b6b1fc2867dd11a5381f85a85b8a59 /var/lib/docker/volumes/bbc41e0bbc97ae32839e876f291eb60173b6b1fc2867dd11a5381f85a85b8a59/_data /usr/local/tomcat local true }]
 
can be seen:
/var/lib/docker/volumes/bbc41e0bbc97ae32839e876f291eb60173b6b1fc2867dd11a5381f85a85b8a59/_data
然后可以修改conf下的配置文件,将war包拷贝到webapps下,等等,修改后要重启容器:sudo docker restart ef9
 
也可以直接映射宿主机上的war包路径:
sudo docker run -d -p 8080:8080 -v /tomcat/webapps:/usr/local/tomcat/webapps  tomcat:latest
这样,只需要将war包拷贝到宿主机的/tomcat/webapps下即可。
 
这样做和在本机使用tomcat并没有实际的区别,它的优势就是宿主机不需要安装jdk和tomcat。
 
docker-compose:
可以使用docker-compose一次启动多个tomcat容器,并建立起他们之间的关联关系。

mkdir -p /docker-compose-dir/tomcat

cd /docker-compose-dir/tomcat

vi docker-compose.yml

version: '2'
services:
   db:
     image: docker.io/mysql:latest
     volumes:
       - /mysql/datadir_tomcat:/var/lib/mysql
       - /mysql/conf:/etc/mysql/conf.d:ro
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: password
       
   tomcat01:
     depends_on:
       - db
     image: docker.io/tomcat:latest
     volumes:
       - /tomcat01/webapps:/usr/local/tomcat/webapps
     links:
       - db:db
     ports:
       - "8081:8080"
     restart: always

   tomcat02:
     depends_on:
       - db
     image: docker.io/tomcat:latest
     volumes:
       - /tomcat02/webapps:/usr/local/tomcat/webapps
     links:
       - db:db
       - tomcat01:tomcat01
     ports:
       - "8082:8080"
     restart: always

 

 
docker-compose up -d
 
上面这种方式,随便进入任何一个容器,执行ping命令,都可以互相ping通
ping tomcat01
ping db
但是查看各自的/etc/hosts,却看不到相应的配置,就是这么神奇。
 
所以war里使用上面的链接别名配置好互相要访问的地址,然后拷贝到对应的部署路径下,并重启。
docker-compose restart
 
Dockerfile:
也可以使用Dockerfile,将war包等直接封装为一个新的镜像。
sudo mkdir /dockerfile
sudo vi Dockerfile
FROM tomcat:latest
MAINTAINER "hanqunfeng <[email protected]>"
ADD web.war /usr/local/tomcat/webapps/
 
 
将web.war拷贝到当前路径下
生成镜像:sudo docker build -t web/tomcat8 .  :注意最后面那个点,代表当前路径
 
启动:sudo run -p 8080:8080 -d web/tomcat8 
 
docker-compose:
tomcat01:
     depends_on:
       - db
     build: /dockerfile
     links:
       - db:db
     ports:
       - "8081:8080"
     restart: always
 
 
docker-compose up -d :第一次执行会自动创建一个镜像,并启动容器
如果该镜像已经被创建了,再次执行时要加上--build:docker-compose up --build -d,此时会重新创建该镜像。
 
 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327103813&siteId=291194637