前言
version: '3'
services:
mysql:
container_name: blog-mysql
image: mysql/mysql-server:5.7
environment:
MYSQL_DATABASE: blog
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: '%'
ports:
- "3306:3306"
restart: always
blog:
container_name: blog
restart: always
build: .
working_dir: /blog
volumes:
- .:/blog
- ~/.m2:/root/.m2
expose:
- "8080"
depends_on:
- mysql
command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker
然后遇到一个问题,我需要在运行容器的时候运行初始化数据库的脚本才行,不然项目将无法使用,然后就开始在网上找方法。
Docker MySQL启动时自动执行初始建表脚本
通过查找资料,了解到:在mysql官方镜像中提供了容器启动时自动执行/docker-entrypoint-initdb.d文件夹下的脚本的功能(包括shell脚本和sql脚本) docker-entrypoint.sh中下面这段代码就是干这事儿的。
ls /docker-entrypoint-initdb.d/ > /dev/null
for f in /docker-entrypoint-initdb.d/*; do
process_init_file "$f" "${mysql[@]}"
done
#usage: process_init_file FILENAME MYSQLCOMMAND...
#ie: process_init_file foo.sh mysql -uroot
#(process a single initializer file, based on its extension. we define this
#function here, so that initializer scripts (*.sh) can use the same logic,
#potentially recursively, or override the logic used in subsequent calls)
<