I am trying to create a service consisting of a web server and database all in docker containers. Currently I am trying to create same environment file for both of them that would contain database credentials. Unfortunatelly, when I try to build database with it, it turns out that they are empty. How can I create a project with single environment file for both components? Here is my docker-compose.yml:
version: '2'
services:
db:
build:
context: .
dockerfile: Dockerfile-db
ports:
- '5433:5432'
env_file:
- env
web:
build: .
ports:
- '8000:8000'
command: venv/bin/python manage.py runserver 0.0.0.0:8000
depends_on:
- db
env_file:
- env
Here is part of my Dockerfile-db file responsible for creating database:
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y postgresql-9.5-postgis-2.2
USER postgres
ARG DB_PASSWORD
ARG DB_NAME
RUN echo $DB_PASSWORD
RUN /etc/init.d/postgresql start && \
psql --command "ALTER USER postgres WITH PASSWORD '$DB_PASSWORD';" && \
psql --command "CREATE DATABASE $DB_NAME;" && \
psql --command "\\c $DB_NAME" && \
psql --command "CREATE EXTENSION postgis;" && \
psql --command "CREATE EXTENSION postgis_topology;"
And my env file has following structure:
DB_NAME=some_name
DB_USER=postgres
DB_PASSWORD=some_password
DB_HOST=db
DB_PORT=5432