使用 Docker Compose 部署 MySQL & Tomcat

Posted by 彭超 on 2019-07-21
Estimated Reading Time 1 Minutes
Words 232 In Total
Viewed Times

容器编排模板文件

创建 docker-compose.yml 模板文件

1
$ sudo mkdir docker-compose.yml

文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
version: '3.1'
services:
tomcat:
restart: always
image: tomcat
container_name: tomcat
ports:
- 8080:8080
volumes:
- /usr/local/docker/tomcat/webapps:/usr/local/tomcat/webapps
environment:
TZ: Asia/Shanghai
mysql:
restart: always
image: mysql:5.7.22
container_name: mysql
ports:
- 3306:3306
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 123456
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
volumes:
- /usr/local/docker/mysql:/var/lib/mysql

文件模板节点详解

文件节点 说明
version 指定 Docker Compose 版本
services 所有容器服务的父节点
tomcat / mysql 容器服务名
restart 开机是否自动启动
image 指定镜像名
container_name 声明容器名
ports 端口开放映射
volumes 数据卷(宿主机路径 : 容器内路径)
environment 环境变量
command 其它配置

docker-compose.yml 文件所在目录下启动容器服务

1
$ docker-compose up -d

列出正在运行的容器

1
$ docker container ps

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !