docker-compose postgres自定义数据库的详细信息

我正在尝试使用docker和docker-composedocker化现有的django应用程序。 使用默认的postgres数据库的Insead,我想创build一个新的名称,用户名和密码的数据库。

这是我正在使用的configuration。 当我附加到数据库容器,我仍然只看到默认帐户,用户和密码。

$ cat .env # Add Environment Variables DB_NAME=postgres_2 DB_USER=postgres_2 DB_PASS=postgres_2 DB_SERVICE=postgres_2 DB_PORT=5432 $ cat docker-compose.yml web: restart: always build: ./web expose: - "8000" links: - db:db - redis:redis volumes: - /usr/src/app/static command: /usr/local/bin/gunicorn django_project.wsgi:application -w 2 -b :8000 nginx: restart: always build: ./nginx/ ports: - "80:80" volumes: - /www/static volumes_from: - web links: - web:web db: restart: always image: postgres:latest volumes_from: - data #env_file: .env env_file: .env ports: - "5432:5432" redis: restart: always image: redis:latest ports: - "6379:6379" data: restart: always image: postgres:latest volumes: - /var/lib/postgresql command: "true" 

docker-compose build; docker-compose up -d; docker ps; docker-compose build; docker-compose up -d; docker ps;

当我连接到数据库容器..

 $:/home/django# docker exec -it 41de5b474b88 /bin/bash root@41de5b474b88:/# su - postgres No directory, logging in with HOME=/ $ psql postgres_1 psql: FATAL: database "postgres_1" does not exist $ psql postgres psql (9.4.4) Type "help" for help. postgres=# 

我可以从env文件中configuration新的数据库详细信息,还是应该使用本回购库中显示的安装后脚本的方法?

https://github.com/tutumcloud/postgresql

只是添加环境文件不会创build数据库,您必须在容器运行后运行引导脚本。

尽pipedocker-compose提供了命令指令,但最好的方法是从基本postgresql映像创build自己的映像,并将其用作组件configuration中的目标(或者使用docker文件configuration映像)。

在您链接的存储库中,密码的设置由modify_postgress_pass.sh脚本完成 ,您可以使用类似的方法。