2

I've got the following docker-compose file and it serves up the application on port 80 fine.

version: '3'

services:
    backend:
        build: ./Django-Backend
        command: gunicorn testing.wsgi:application --bind 0.0.0.0:8000 --log-level debug
        expose:
            - "8000"
        volumes:
            - static:/code/backend/static
        env_file:
            - ./.env.prod

    nginx:
        build: ./nginx
        ports:
            - 80:80
        volumes:
            - static:/static
        depends_on:
            - backend

    db:
        image: postgres
        environment:
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
        volumes:
            - postgres_data:/var/lib/postgresql/data/

volumes:
    static:
    postgres_data:

Once in there I can log into the admin and add an extra user which gets saved to the database as I can reload the page and the user is still there. Once I stop the backend docker container however that user is gone. Given Postgres is operating in a different container and I'm not bringing it down I'm unsure how stopping the backend container and restarting it is causing the data not to be available.

Thanks in advance.

EDIT: I'm bringing up the docker container with the following command.

docker-compose -f docker-compose.prod.yml up -d

I'm bringing down the container by just using docker desktop and stopping the container.

I'm running DJANGO 3 for the backend and I've also tried adding a superuser in the terminal when the container is running:

# python manage.py createsuperuser 
Username (leave blank to use 'root'): mikey
Email address: 
Password: 
Password (again): 
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

Which works and the user appears while the container is running. However, once again when I shut the container down via docker desktop and then restart it that user that was just created is gone.

FURTHER EDIT:

settings.py using dotenv "from dotenv import load_dotenv"

DATABASES = {
    "default": {
        "ENGINE": os.getenv("SQL_ENGINE"),
        "NAME": os.getenv("SQL_DATABASE"),
        "USER": os.getenv("SQL_USER"),
        "PASSWORD": os.getenv("SQL_PASSWORD"),
        "HOST": os.getenv("SQL_HOST"),
        "PORT": os.getenv("SQL_PORT"),
    }
}

with the .env.prod file having the following values:

DEBUG=0
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=postgres
SQL_USER=postgres
SQL_PASSWORD=postgres
SQL_HOST=db
SQL_PORT=5432

SOLUTION:

Read the comments to see the diagnosis by other legends but updated docker-compose file looks like this. Note the "depends_on" block.

version: '3'

services:
    backend:
        build: ./Django-Backend
        command: gunicorn testing.wsgi:application --bind 0.0.0.0:8000 --log-level debug
        expose:
            - "8000"
        volumes:
            - static:/code/backend/static
        env_file:
            - ./.env.prod
        depends_on:
            - db

    nginx:
        build: ./nginx
        ports:
            - 80:80
        volumes:
            - static:/static
        depends_on:
            - backend

    db:
        image: postgres
        environment:
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
        volumes:
            - postgres_data:/var/lib/postgresql/data/
        expose:
            - "5432"

volumes:
    static:
    postgres_data:

FINAL EDIT:

Added the following code to my entrypoint.sh file to ensure Postgres is ready to accept connections by the backend container.

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi
8
  • Could you please add the command you use to bring down the backend container? generally, give us a bit more info on the exact process you follow Commented Apr 27, 2020 at 11:18
  • No worries added more detail. Hope that helps Commented Apr 27, 2020 at 11:30
  • How are you connecting to database in settings.py? Is it being connected to postgres itself or is it using the default sqlite3 db? Commented Apr 27, 2020 at 11:43
  • Added a further edit to help out. It's using Postgres database via .env.prod file. Commented Apr 27, 2020 at 11:49
  • 1
    Can you expose postgres port from db container and map it to some port of the host machine. Then connect to the database using pgAdmin or postico app if you are in mac and verify that indeed the data is created. I am skeptical because in docker compose file, backend does not have depends_on block and django might be falling back to sqlite3 Commented Apr 27, 2020 at 12:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.