docker问题一次编写初始数据库设置 [英] Issue with docker compose initial DB setup once

查看:44
本文介绍了docker问题一次编写初始数据库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了如下所示的Docker文件

I have created Docker file which looks like this

db:
image: postgres:10.3
ports:
  - "5432:5432"
environment:
  POSTGRES_DB: test
  POSTGRES_USER: test
  POSTGRES_PASSWORD: changeme
restart: always
volumes:
  - ./srv/postgres:/var/lib/postgresql/data
  - ./init/:/docker-entrypoint-initdb.d/

我在./init路径中有一个sql脚本,这些脚本具有创建数据库和用户的sql命令.我看到在postgres容器中创建的sql文件,但不知何故它没有执行.我已经遵循了docker hub上的文档.我不确定这里出什么问题了吗?

I have a sql script in ./init path which have sql commands to create database and user. I see the sql file created in the postgres container but somehow it is not executing. I have followed the documentation on the docker hub. I am not sure what is wrong here?

推荐答案

我也经历了这一点.问题在于,只有在尚未初始化数据库的情况下,才会运行/docker-entrypoint-initdb.d/中的脚本.

I have experienced this as well. The problem is that the scripts inside of /docker-entrypoint-initdb.d/ will only be ran if the database has not already been initialized.

如果停止容器,请删除 ./srv/postgres/的内容,然后在内的 *.sql 脚本中重新创建容器.初始化将运行.

If you stop the container, delete the contents of ./srv/postgres/ and then recreate the container your *.sql scripts inside of ./init will be ran.

但是这样做会导致您的数据丢失.

However doing this will cause your data to be lost.

有关更多信息,请参见此处.

See here for a bit more info.

这篇关于docker问题一次编写初始数据库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆