4

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

2 Answers 2

4

The environment file is not part of the build process, it is used when running the container.

You need to use build-args. In docker-compose you can specify build args in the file:

build:
  context: .
  dockerfile: Dockerfile-db
  args:
    DB_NAME: some_name
    DB_USER: postgress
    ...

This might not be a good idea if you want to publish the composefile, as you are storing credentials in it. You can explicitly build and pass --build-arg

docker-compose build --build-arg DB_NAME= some_name ...

And when running specify no build in docker-compose run --no-build

Update:

As suggested by @gonczor, a shorter and cleaner syntax to use pass the env file as build args is:

docker-compose build --build-args $(cat envfile)
Sign up to request clarification or add additional context in comments.

3 Comments

I keep getting this message as if the --build-arg was not supported. Can it be due to docker-compose version? (I'm running 1.8) Usage: build [options] [SERVICE...] Options: --force-rm Always remove intermediate containers. --no-cache Do not use cache when building the image. --pull Always attempt to pull a newer version of the image.
Yes possibly. If docker-compose build --help is not showing --build-arg option, you need to update docker compose
Thanks for help. It worked. And I've come up with a workaround. I've just added --build-args $(cat env) to the command. If you were so kind to add it to the answer I would be grateful.
0

Your configuration is correct, it seems like you may not be passing the actual path or the name of the .env file. Have you tried the following (assuming your .env file is in the same directory).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.