0

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 "$@"

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.

xshapira
  • 355
  • 4
  • 13
  • What are the three `bash` lines at the start of the entrypoint script? (If you type `exit` three times, does your `docker-compose run` container do what you expect?) – David Maze Jul 23 '21 at 10:40
  • I commented out the three ```bash``` lines. And now it exits automatically. ```docker compose run django ./manage.py startapp createsuperuser [+] Running 1/1 ⠿ Container django-tailwind-docker_postgres_1 Recreated 10.3s [+] Running 1/1 ⠿ Container django-tailwind-docker_postgres_1 Started 0.6s PostgreSQL is available ~/Dev/p/django-tailwind-docker ``` https://ibb.co/k3Qb0LK – xshapira Jul 23 '21 at 11:48

1 Answers1

0

Actually it is not opening postgres shell. It said that depended cotainer with postgres is running. A path that showed is /usr/src/app/ as in your django component. You can also checked hash of your django app and hash in bash line '7fbcde....'

Averin Maxim
  • 373
  • 4
  • 19
  • Thanks for the response. I'm relatively new to Docker and its configurations. How can I achieve the same result with ```run``` as I'm getting with ```exec```? – xshapira Jul 23 '21 at 10:35
  • Type docker-compose run django and only then: python manage.py createsuperuser – Averin Maxim Jul 23 '21 at 19:49
  • When I use ```docker-compose run django```, it executes the 'entrypoint' and starts the development environment on port 8000. From there I can only exit. If I open a new terminal and try ```python manage.py createsuperuser``` it won't be valid anymore. – xshapira Jul 25 '21 at 05:15