Reputation: 18818
I am trying to dockerize my existing django application using docker and docker-compose. Insead of using the default postgres db, I want to create a db with a new name, user id and password.
Here is the config that I am using. When I attach to the db container, I still see only the default account, user and password.
$ 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"
After docker-compose build; docker-compose up -d; docker ps;
When I connect to the db container..
$:/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=#
Can I configure the new database details from the env file or should I use the approach of a post-installation script as shown in this repo?
https://github.com/tutumcloud/postgresql
Upvotes: 0
Views: 1619
Reputation: 905
A possible solution for the above question is answered- Docker compose postgresql service - can't create user and database during build?.
Creating a user from the sql file at docker-compose up would be the good solution for building up docker in different OS(especially windows).
Upvotes: 0
Reputation: 174748
Just adding the environment file doesn't create the databases, you have to run a bootstrap script once the container is running.
Although docker-compose provides the command directive, the best way to do this is to create your own image from the base postgresql image, and use that as the target in your componse configuration (or alternatively, configure the image with a docker file).
In the repository you linked, the setting of the password is done by the modify_postgress_pass.sh
script and you can use a similar approach.
Upvotes: 3