I am working on a project using Django and am trying to load an .env file to the PostgreSQL image in my docker-compose.yml script. However, for some reason, I can't load them.
After I run the 'docker-compose up' command, one of the warnings I get from db_1 is as follows:
.
.
.
db_1 | ****************************************************
db_1 | WARNING: No password has been set for the database.
db_1 | This will allow anyone with access to the
db_1 | Postgres port to access your database. In
db_1 | Docker's default configuration, this is
db_1 | effectively any other container on the same
db_1 | system.
db_1 |
db_1 | Use "-e POSTGRES_PASSWORD=password" to set
db_1 | it in "docker run".
db_1 | ****************************************************
.
.
.
To help reproduce the problem, here's my folder structure:
My project structure:
├── config/
│ ├── .env
├── src/
│ ├── manage.py
│ └── core
│ | ├── __init__.py
│ | ├── settings.py
│ | ├── urls.py
│ | ├── wsgi.py
├── docker-compose.yml
├── Dockerfile
└── requirements.txt
My Dockerfile:
FROM python:3.7.0
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
My docker-compose.yml:
version: '2'
services:
db:
image: postgres
restart: always
env_file:
- ./config/.env
web:
build: .
stdin_open: true
tty: true
command: python src/manage.py runserver 0.0.0.0:8000
restart: always
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
My .env file:
PGUSER=admin
PGDATABASE=db
PGPASSWORD=s3cr3t
PGHOST=h0st
PGPORT=5432
An interesting point is that I was able to load environment variables into settings.py using the dotenv library, the problem is actually setting the environment variables in the PostgreSQL image.
I am following this logic of environment variables specified by PostgreSQL itself: https://www.postgresql.org/docs/10/libpq-envars.html
1.POSTGRES_PASSWORD is environment variable use by docker engine to set up POSTGRES image in newly created container. It must be set in .env file or in docker-compose.yml file.
Other variable used by docker engine can be found here:
https://github.com/docker-library/docs/tree/master/postgres
2.PGPASSWORD is environment variable use by postgres engine. It is used with psql command.
export PGPASSWORD='password'
psql -h 'server name' -U 'user name' -d 'base name' \
-c 'command'
Other variable used by postgres engine can be found here:
https://www.postgresql.org/docs/current/libpq-envars.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With