docker-compose postgres定制数据库详细信息

问题描述 投票:0回答:2

我正在尝试使用docker和docker-compose将我现有的django应用程序停靠。如果使用默认的postgres db,我想创建一个带有新名称,用户ID和密码的数据库。

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

$ 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;之后

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

$:/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文件配置新的数据库详细信息,还是应该使用此repo中显示的安装后脚本的方法?

https://github.com/tutumcloud/postgresql

django postgresql docker docker-compose
2个回答
3
投票

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

尽管docker-compose提供了command指令,但最好的方法是从基础postgresql映像创建自己的映像,并将其用作组件配置中的目标(或者使用docker文件配置映像)。

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


0
投票

回答上述问题的可能解决方案 - Docker compose postgresql service - can't create user and database during build?

在docker-compose上从sql文件创建用户将是在不同操作系统(尤其是Windows)中构建docker的好方法。

© www.soinside.com 2019 - 2024. All rights reserved.