Home > Software engineering >  Dockerized Django app and MySQL with docker-compose using .env
Dockerized Django app and MySQL with docker-compose using .env

Time:10-14

I would to run my Django project into a Docker container with its Database on another Docker container inside a Bebian

When i run my docker container, I have some errors. Like : Lost connection to MySQL server during query ([Errno 104] Connection reset by peer). This command mysql > SET GLOBAL log_bin_trust_function_creators = 1 is very important because database's Django user create trigger. Morever, I use a .env file used same for create DB image to store DB user and password. This path is settings/.env.

My code: docker-compose.yml:

version: '3.3'

services:
  db:
    image: mysql:8.0.29
    container_name: db_mysql_container
    environment:
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
    command: ["--log_bin_trust_function_creators=1"]
    ports:
      - '3306:3306'
    expose:
      - '3306'
  api:
    build: .
    container_name: django_container
    command: bash -c "pip install -q -r requirements.txt &&
                      python manage.py migrate &&
                      python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/app
    ports:
      - '8000:8000'
    depends_on:
      - db

Dockerfile :

# syntax=docker/dockerfile:1
FROM python:3.9.14-buster
ENV PYTHONUNBUFFERED=1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/

How to start my Django project ? Is possible to start only the DB container ?

What command i need execute and what changes i need to make, I'm novice with Docker ! So if you help me, please explains your commands and actions !

You can find this project on my GitHub

Thank !

CodePudding user response:

To run dockerized django project.

Simply you can run below command:

docker-compose run projectname bash -c "python manage.py createsuperuser"

Above command is used for to create superuser

  • Related