testdrivenio/django-on-docker

unable to create migration file

abhimal opened this issue · 2 comments

hello, I have followed your django-on-docker series, but when I Deploy Django to AWS with Docker and Let's Encrypt I'm unable to create a migration file whenever I add new field in my django model and run docker-compose -f docker-compose.prod.yml exec web python manage.py makemigrations --noinput unable to create migration file .

docker-compose.prod.yml

version: '3.7'

services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile.prod
    image: <aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com/django-ec2:web
    command: gunicorn djnago_on_docker.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    expose:
      - 8000
    env_file:
      - .env.prod
    depends_on:
      - redis
  celery:
    build:
      context: ./app
      dockerfile: Dockerfile.prod
    image: <aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com/django-ec2:celery
    command: celery -A djnago_on_docker worker -l INFO
    env_file:
      - .env.prod
    depends_on:
      - web
      - redis
  nginx-proxy:
    container_name: nginx-proxy
    build: nginx
    image:<aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com/django-ec2:nginx-proxy
    restart: always
    ports:
      - 443:443
      - 80:80
    volumes:
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
      - certs:/etc/nginx/certs
      - html:/usr/share/nginx/html
      - vhost:/etc/nginx/vhost.d
      - /var/run/docker.sock:/tmp/docker.sock:ro
    depends_on:
      - web
  nginx-proxy-letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    env_file:
      - .env.prod.proxy-companion
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - certs:/etc/nginx/certs
      - html:/usr/share/nginx/html
      - vhost:/etc/nginx/vhost.d
    depends_on:
      - nginx-proxy
  redis:
    image: redis:5-alpine

volumes:
  static_volume:
  media_volume:
  certs:
  html:
  vhost:

Note: I hide my aws-account-id and region. Image properties to use images from ECR, i also removed the db service (and related volume) since i'll use RDS rather than managing Postgres in a container.

Dockerfiel.prod

###########
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc libxslt-dev libxml2-dev python3-dev musl-dev jpeg-dev zlib-dev 

# lint
RUN pip install --upgrade pip
RUN pip install flake8
COPY . .
RUN flake8 --ignore=E501,F401 ./djnago_on_docker

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
RUN mkdir $APP_HOME/mediafiles
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
RUN apk add libjpeg libxslt-dev
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*

# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

RUN chmod +x entrypoint.prod.sh

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

entrypoint.prod.sh

#!/bin/sh

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

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

    echo "PostgreSQL started"
fi

exec "$@"

Note: I remove migrate commands in the entrypoint.prod.sh script so they don't run on every container start or re-start. Instead, i can run them manually, after the containers spin up, like so:

docker-compose -f docker-compose.prod.yml exec web python manage.py makemigrations --noinput
docker-compose -f docker-compose.prod.yml exec web python manage.py migrate --noinput

Please help me. @jangia

Saw this only now. I'll take a look.

Oh, I see it's closed.