Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto reloading Django server on Docker

I am learning to use Docker and I have been having a problem since yesterday (before I resorted to asking, I started to investigate but I could not solve the problem), my problem is that I have a Django project in my local machine, I also have the same project with Docker, but when I change my local project, it is not reflected in the container that the project is running. I would be very grateful if you could help me with this please. Thank you.

Dockerfile

FROM python:3.7-alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code

RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add postgresql-dev \
    && pip install psycopg2 \
    && apk del build-deps

RUN pip install --upgrade pip
RUN pip install pipenv

COPY ./Pipfile /code
RUN pipenv install --skip-lock --system --dev

COPY ./entrypoint.sh /code

COPY . /code

ENTRYPOINT [ "/code/entrypoint.sh" ]

docker-compose.yml

# version de docker-compose con la que trabajaremos
version: '3'

# definiendo los servicios que correran en nuestro contenedor
services:
  web:
    restart: always
    build: .
    command: gunicorn app.wsgi:application --bind 0.0.0.0:8000 #python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
      - static_volume:/code/staticfiles
      - media_volume:/code/mediafiles
    expose:
      - 8000
    environment:
      - SQL_ENGINE=django.db.backends.postgresql
      - SQL_DATABASE=postgres
      - SQL_USER=postgres
      - SQL_PASSWORD=postgres
      - SQL_HOST=db
      - SQL_PORT=5432
      - DATABASE=postgres
    depends_on:
      - db
    env_file: .env
  db:
    restart: always
    image: postgres:10.5-alpine
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
  nginx:
    restart: always
    build: ./nginx
    volumes:
      - static_volume:/code/staticfiles
      - media_volume:/code/mediafiles
    ports:
      - 1337:80
    depends_on:
      - web

volumes:
  static_volume:
  media_volume:

And a little doubt here, is it a good practice to store the environment variables in Dockerfile or docker-compose ?, I use .env but I have seen in many places that they store the variables in docker-compose, as shown in the code of above

I hope you can help me, any recommendation about my project, is very well received, as I comment, I'm new to Docker but I really like it a lot and I would like to learn more about it.

like image 799
Miguel Toledano Avatar asked Jan 27 '23 19:01

Miguel Toledano


1 Answers

How people usually approach this is to have a separate docker-compose configurations for development and production environment, e.g. local.yml and production.yml. That way you can use runserver while developing (which you'll probably find more suitable since you'll get a lot of debug information) and gunicorn on production.

I'd recommend looking into https://github.com/pydanny/cookiecutter-django project which has a lot of Django good practices integrated as well as a good out of the box Docker configuration. You can create a test project using the cookiecutter and then inspect how they do the Docker setup, including environment variables.

like image 106
bonidjukic Avatar answered Jan 31 '23 08:01

bonidjukic