如何在docker中为postgres运行sql脚本
我正在关注这个post来运行SQL脚本来创build默认表。
这是我的docker撰写文件
version: "3" services: web: build: . volumes: - ./:/usr/src/app/ - /usr/src/app/node_modules ports: - “3000:3000” depends_on: - postgres postgres: container_name: postgres build: context: . dockerfile: pgDockerfile environment: POSTGRES_PASSWORD: test POSTGRES_USER: test ports: - 5432:5432
这是我的pgDockerfile
FROM postgres:9.6-alpine # copy init sql ADD 00-initial.sql /docker-entrypoint-initdb.d/
这是我的SQL脚本
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TABLE IF NOT EXISTS test ( id text NOT NULL, title varchar(200) NOT NULL );
我可以构build并运行docker-compose up
,并看到以下消息:
postgres | CREATE DATABASE postgres | postgres | CREATE ROLE postgres | postgres | postgres | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/00-initial-data.sql postgres | CREATE EXTENSION postgres | CREATE TABLE postgres | postgres | postgres | LOG: received fast shutdown request postgres | LOG: aborting any active transactions postgres | waiting for server to shut down....LOG: autovacuum launcher shutting down postgres | LOG: shutting down postgres | LOG: database system is shut down postgres | done postgres | server stopped postgres | postgres | PostgreSQL init process complete; ready for start up. postgres | postgres | LOG: database system was shut down at 2017-03-22 21:36:16 UTC postgres | LOG: MultiXact member wraparound protections are now enabled postgres | LOG: database system is ready to accept connections postgres | LOG: autovacuum launcher started
数据库似乎在表创build后closures。 我认为这是postgres docker的标准过程,但是当我login到postgres时,我没有看到我的表应该在那里。
我通过login
docker exec -it $(my postgres container id) sh #su - postgres #psql # \d => No relations found.
我不确定这是否是为postgres创build默认数据的正确方法。 任何人都可以帮助我吗? 非常感谢!
- 将Docker容器中的rails应用程序通过URL连接到外部Postgresql数据库
- 如何docker exec -ti CONTAINER_NAME / bin / bash在部署的docker栈上?
- 如何找出docker运行最多消耗的RAM数量?
- 在特定位置创buildDocker卷
- 将docker运行的dynamic值传递到tomcat jsp页面的策略
- Docker:不同的容器可以使用不同的基础图像吗?
- 当我closuresiptables时,为什么docker容器仍然可以与外部进行通信
- docker run with cd command failed with executable file not found in $ PATH
- 我如何使用另一个目录保存构build的Docker容器?
- 在Docker中使用Data Volume Container作为单一数据库“后端”有什么好处?
- Dockerfile ADD https:// zzzz x509:由未知权限签名的证书