简体   繁体   English

docker-compose:执行数据库脚本

[英]docker-compose: execute database scripts

I am setting up a local mssql database using docker-compose and a dockerfile. 我正在使用docker-compose和dockerfile设置本地mssql数据库。

I was able to use the example here: 我可以在这里使用示例:

https://github.com/mcmoe/mssqldocker

which executes a database script to create the database upon startup. 它在启动时执行数据库脚本以创建数据库。 I am now running a couple of sql scripts to setup the database. 我现在正在运行几个sql脚本来设置数据库。

So I can run: 这样我就可以运行:

docker-compose build

then... 然后...

docker-compose up

And docker will start mssql and run the scripts to create my database, schemas with tables, views and pre-populated data. 泊坞窗将启动mssql并运行脚本以创建我的数据库,具有表,视图和预填充数据的架构。

However, if I run: 但是,如果我运行:

docker-compose stop

then run: 然后运行:

docker-compose up

It tries to run all the scripts again which is not good (lots of errors). 它试图再次运行所有的脚本,这是不好的(大量的错误)。 Ideally I only ever want this to happen once. 理想情况下,我只希望这种情况发生一次。 Is there a way to tell docker not to run the scripts if they have already been ran? 有没有办法告诉docker如果脚本已经运行,不要运行它们?

I really want to reduce setup of a local db to really one thing: 我真的想将本地数据库的设置减少为一件事:

docker-compose up - should do everything. docker-compose up应该做的一切。

EDIT: 编辑:

I tried wrapping the SQL scripts using: 我尝试使用以下方法包装SQL脚本:

if [ ! -d "/var/lib/mssql/data" ]; then 
  # scripts in here
fi

but it always skips over them! 但它总是跳过它们!

I'd move any "one-time" scripts you need to provision the container into a Dockerfile. 我将需要将容器调配到Dockerfile中的所有“一次性”脚本。 They will only get run once during docker-compose build . 它们只会在docker-compose build期间运行一次。 That way docker-compose just needs to care about starting and stopping the service. 这样, docker-compose只需关心启动和停止服务。

To expand on @Twig2let's point , the setup.sql is not being run in the Dockerfile. 为了扩展@ Twig2let的观点 ,setup.sql 在Dockerfile中运行。 setup.sql is run from configure-db.sh, which is run from entrypoint.sh, which is the ENTRYPOINT in the Dockerfile, which means it runs every time the container starts. setup.sql是从configure-db.sh运行的,configure-db.sh是从entrypoint.sh运行的,entrypoint.sh是Dockerfile中的ENTRYPOINT,这意味着它每次容器启动时都会运行。

If you want to install the schema during the build stage, you have to execute it with a RUN directive in the Dockerfile somehow. 如果要在构建阶段安装模式,则必须以某种方式在Dockerfile中使用RUN指令执行它。 I suspect you'll find that tricky because you'll probably need to get the server process up and running during the build and then shut it back down. 我怀疑您会发现这很棘手,因为您可能需要在构建过程中启动并运行服务器进程,然后将其关闭。

I suspect you're on a better path with adding your check to see if the initialization has already happened. 我怀疑您通过添加检查以查看初始化是否已经发生而走上了更好的道路。 You'll just need to make the detection more accurate. 您只需要使检测更加准确即可。 For example, try to find some way to check if $(MSSQL_DB) already exists, or modify the sql statements with IF NOT EXISTS type logic, if that's supported for all the initialization statements. 例如,尝试找到某种方法来检查$(MSSQL_DB)已经存在,或者使用IF NOT EXISTS类型逻辑修改sql语句,如果所有初始化语句都支持该逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM