我有这个 bash 脚本:
#!/bin/bash
docker-compose up -d
# wait for the database to come up
until mysqlshow --user=root -h localhost -P 3306 --protocol=tcp > /dev/null 2>&1; do
echo "Waiting for mysql container..."
sleep 0.5
done
RESULT=`mysqlshow --user=root -h localhost -P 3306 --protocol=tcp payments| grep -o payments`
if ! [[ $RESULT = *"payments"* ]]; then
./node_modules/.bin/sequelize db:create
fi
./node_modules/.bin/sequelize db:migrate
yarn start
以上确实有效,但它需要mysql
在本地安装,这违背了使用 docker 的目的。
有没有更好的办法:
- 我可以在不使用 mysql 的情况下检查数据库是否已启动。
- 如果它不存在,我可以在开始创建数据库时在容器中运行创建脚本。