ibrahimadlani / twitch-recap

Web application made to get the bests of your twitch videos. (chats, audience activity, custom videos)
1 stars 0 forks source link

Dockerize Django Project "back" and Create .dockerignore and docker-compose.yml #4

Closed ibrahimadlani closed 3 months ago

ibrahimadlani commented 3 months ago

Description

This task involves dockerizing the Django project named "back" by creating a Dockerfile and setting up the necessary configurations. Additionally, create a .dockerignore file to optimize the Docker build process and a docker-compose.yml file to manage multi-container Docker applications.

Steps

  1. Create Dockerfile:

    • Create a Dockerfile in the root of the project with the following content:

      FROM python:3.11.5-alpine3.18
      
      LABEL maintainer="ibrahimadlani"
      
      ENV PYTHONUNBUFFERED 1
      
      COPY ./requirements.txt /tmp/requirements.txt
      COPY . /app
      WORKDIR /app
      
      RUN python -m venv /py && \
        /py/bin/pip install --upgrade pip && \
        apk add --update --no-cache postgresql-client && \
        apk add --update --no-cache --virtual .tmp-build-deps \
        build-base postgresql-dev musl-dev && \
        /py/bin/pip install -r /tmp/requirements.txt && \
        rm -rf /tmp && \
        apk del .tmp-build-deps && \
        adduser \
            --disabled-password \
            --no-create-home \
            django-user && \
        chmod +x entrypoint.sh
      
      ENV PATH="/py/bin:$PATH"
      
      USER django-user
  2. Create .dockerignore File:

    • Create a .dockerignore file in the root of the project with the following content:
      __pycache__
      *.pyc
      .git
      .venv
      .env
      .mypy_cache
      .pytest_cache
      .pylint.d
      node_modules
      build
      dist
  3. Create entrypoint.sh File:

    • Create an entrypoint.sh file in the root of the project with the following content:

      #!/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 "$@"
    • Make sure the entrypoint.sh file is executable:
      chmod +x entrypoint.sh
  4. Update settings.py:

    • Update the DATABASES setting in back/settings.py to use environment variables:

      import os
      
      DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': os.getenv('POSTGRES_DB'),
            'USER': os.getenv('POSTGRES_USER'),
            'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
            'HOST': os.getenv('SQL_HOST'),
            'PORT': os.getenv('SQL_PORT'),
        }
      }
  5. Create docker-compose.yml File:

    • Create a docker-compose.yml file in the root of the project with the following content:

      version: '3.8'
      
      services:
      db:
        image: postgres:13
        volumes:
          - postgres_data:/var/lib/postgresql/data
        environment:
          POSTGRES_DB: your_db
          POSTGRES_USER: your_user
          POSTGRES_PASSWORD: your_password
      
      web:
        build: .
        command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
        volumes:
          - .:/app
        ports:
          - "8000:8000"
        environment:
          DATABASE: postgres
          SQL_HOST: db
          SQL_PORT: 5432
          POSTGRES_DB: your_db
          POSTGRES_USER: your_user
          POSTGRES_PASSWORD: your_password
        depends_on:
          - db
      
      volumes:
      postgres_data:
  6. Build and Run Docker Containers:

    • Build and start the Docker containers using docker-compose:
      docker-compose up --build

Resources

Priority