docker-compose run: django and postgres "strange" behavior

When I run the following command:

docker-compose run django python manage.py startapp hello

OR

docker-compose run django python manage.py createsuperuser

Instead of starting a new container, it opens a Postgres shell.

enter image description here

However, when I’m using exec, like so:

docker-compose exec django python manage.py createsuperuser

Then it works as expected.

enter image description here

Here is my docker-compose.yml file:

version: '3.7'

services:
  postgres:
    image: 'postgres:9.6.9-alpine'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    env_file:
      - ./.env.dev

  django:
    image: django_dev
    build:
      context: .
      dockerfile: ./app/docker/django/Dockerfile
    volumes:
      - ./app/:/usr/src/app/
    command: /usr/src/app/docker/django/start_dev
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev
    depends_on:
      - postgres

  node:
    image: node_dev
    build:
      context: .
      dockerfile: ./app/docker/node/Dockerfile
    depends_on:
      - django
    volumes:
      - ./app/:/usr/src/app/
      - /usr/src/app/node_modules
    command: npm run dev
    ports:
      - '3000:3000'
      - "3001:3001"

volumes:
  postgres_data:


Dockerfile:

FROM python:3.9.5-slim-buster

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt update 
    && apt install -y curl 
    && curl -sL https://deb.nodesource.com/setup_lts.x | bash - 
    && apt-get -y install libpq-dev gcc 
    && pip install psycopg2 
    && pip install psycopg2-binary

RUN pip install --upgrade pip 
  && pip install pipenv

COPY ./app/Pipfile /usr/src/app/Pipfile

RUN pipenv install --skip-lock --system --dev

RUN apt-get update && apt-get install -y libglu1

COPY ./app/docker/django/entrypoint /usr/src/app/docker/django/entrypoint

COPY ./app/docker/django/start_dev /usr/src/app/docker/django/start_dev
RUN sed -i 's/r$//g' /usr/src/app/docker/django/start_dev
RUN chmod +x /usr/src/app/docker/django/start_dev

COPY . /usr/src/app/

ENTRYPOINT ["/usr/src/app/docker/django/entrypoint"]

entrypoint:

#!/bin/sh

bash | set -o errexit
bash | set +o pipefail
bash | set -o nounset


if [ -z "${POSTGRES_USER}" ]; then
    base_postgres_image_default_user='postgres'
    export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

postgres_ready() {
python << END
import sys

import psycopg2

try:
    psycopg2.connect(
        dbname="${POSTGRES_DB}",
        user="${POSTGRES_USER}",
        password="${POSTGRES_PASSWORD}",
        host="${POSTGRES_HOST}",
        port="${POSTGRES_PORT}",
    )
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)

END
}
until postgres_ready; do
  >&2 echo 'Waiting for PostgreSQL to become available...'
  sleep 1
done
>&2 echo 'PostgreSQL is available'

exec "[email protected]"

start_dev:

#!/bin/sh

bash | set -o errexit
bash | set +o pipefail
bash | set -o nounset


python manage.py flush --no-input
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser --noinput
python manage.py runserver 0.0.0.0:8000

I racked my brains for a few days, but couldn’t find an explanation for this behavior.

Source: Docker Questions

LEAVE A COMMENT