Django crontab can’t connect database(Postgresql) with docker; no such table err

I am using django and postgresql. I am using django-crontab to change the data.

It runs well in the local environment, but we use docker to deploy and watch, and I confirmed that when cron runs, we refer to sqlite3.

I also made a separate cron container in docker composite and ran it, I am using it incorrectly because I am a beginner. Help me

#goods/cron.py
from goods.models import Goods
def test():
    print(Goods.objects.all())

./docker-compose.yml

version: '3.8'

volumes:
  postgres: {} 
  django_media: {}
  django_static: {}
  static_volume: {}

services:

  postgres:
    container_name: postgres
    image: postgres:14.5
    volumes:
      - postgres:/var/lib/postgresql/data/
    environment: 
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB
    restart: always

  nginx:
    container_name: nginx
    image: nginx:1.23.2
    ports:
      - "80:80" 
      - "443:443" 
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - django_media:/media/ 
      - django_static:/static/ 
    depends_on:
      - asgiserver
      - backend
    restart: always
django_backend:/app/media
  backend:  host:container
    container_name: django_backend
    build: .
    entrypoint: sh -c "python manage.py migrate && gunicorn handsup.wsgi --workers=5 -b 0.0.0.0:8000"
    restart: always
    volumes:
      - ./:/app/
      - /etc/localtime:/etc/localtime:ro
      - django_media:/app/media/ 
      - django_static:/app/static/ 
    environment: #
      - DEBUG
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_HOST
      - POSTGRES_PORT
    depends_on:
      - postgres
  redis:
    image: redis:5

  asgiserver:
    build: .
    command: daphne -b 0.0.0.0 -p 8080 handsup.asgi:application

    volumes:
      - ./:/app/
    restart: always
    environment: 
      - DEBUG
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_HOST
      - POSTGRES_PORT
    depends_on:
      - redis
      - postgres

  cron:
    build: .     
    restart: always
    volumes:
      - ./:/app/
    depends_on:
      - postgres
      - backend
    environment: #
      - DEBUG
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_HOST
      - POSTGRES_PORT
    command: cron -f  # as a long-running foreground process

./Dockerfile

FROM python:3.10.8

ENV PYTHONDONTWRITEBYTECODE 1

ENV PYTHONUNBUFFERED 1

RUN mkdir /app/

WORKDIR /app/

RUN apt-get update -y
RUN apt-get install -y cron

COPY ./requirements.txt .

COPY ./ /app/

RUN pip install --no-cache-dir -r requirements.txt

# RUN service cron start
ENTRYPOINT ["./docker-entrypoint.sh"]

RUN pip install gunicorn psycopg2

./docker-entrypoint.sh


# If this is going to be a cron container, set up the crontab.
if [ "$1" = cron ]; then
  ./manage.py crontab add
fi

# Launch the main container command passed as arguments.
exec "$@"

I referred to the contents below.

How to make django-crontab execute commands in Docker container?

Back to Top