There are a lot of irrelevant articles on the Internet, so I prepared this publication. Basically, when writing the article, I relied on the example of configuring docker for django in the docker documentation.
Here we will describe the docker setup for the django framework and the mysql database, which will be stored outside the container, so you don't have to worry about something happening to our container.
In writing this guide, the following were used:
- ubuntu 20.04
- docker 03/19/12
- docker-compose 1.26.2
- python 3.8
- mysql 5.7
- django 3.1.1
- mysqlclient 2.0.1
- django-mysql 3.8.1
Workspace setup
Before starting the setup, you need to prepare the workspace:
- Install docker and docker-compose . The installation process is detailed in the docker documentation.
- Create a directory where our project will be located (in this example, project). In this directory, create another one, which will store the configuration for running the python image (in this example, web).
./project
/web
This completes the preparation and you can start setting up our small project.
requirements.txt
In the web directory, create a file called requirements.txt.
In this file, we declare packages for our django application to work.
/project/web/requirements.txt
Django==3.1.1
mysqlclient==2.0.1
django-mysql==3.8.1
As you can see, django itself is registered here, as well as packages for communicating with the mysql database.
.
Dockerfile
web Dockerfile.
Dockerfile . .
:
/project/web/Dockerfile
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /web_django
WORKDIR /web_django
COPY requirements.txt /web_django/
RUN pip install --upgrade pip && pip install -r requirements.txt
ADD . /web_django/
python , , . web_django .
pip (--upgrade pip), .. , .
docker-compose.yml
docker-compose.yml.
. - . compose , Docker , , . , .
.
/project/docker-compose.yml
version: '3'
services:
web:
build: ./web
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/web_django
ports:
- '8000:8000'
depends_on:
- db
db:
image: mysql:5.7
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: 'db_django'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
restart: always
volumes:
- ./db_django:/var/lib/mysql
web/Dockerfile . django 8000 . mysql.
mysql. : , , , ( , , ) , . , , , , mysql .
. django .
django
(project/) . django web_django.
sudo docker-compose run web django-admin startproject web_django .
:
drwxr-xr-x 6 root root 4,0K 16 21:49 db_django
-rw-rw-r-- 1 kafka kafka 466 16 21:31 docker-compose.yml
-rwxr-xr-x 1 root root 666 16 21:34 manage.py
drwxrwxr-x 2 kafka kafka 4,0K 16 21:31 web
drwxr-xr-x 3 root root 4,0K 16 21:34 web_django
, .. root .
sudo chown -R $USER:$USER web_django
Open the project django settings file along the path web_django / settings.py
In this file, we will replace the database connection settings that we specified in docker-compose.yml.
/project/web_django/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_django',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '3306',
}
}
Now everything is ready and you can run docker-compose.
Launch of the project
To start containers, just run this command
docker-compose up
To run containers in the background, you need to specify an additional argument
docker-compose up -d
In order to rebuild images, you can run the command
docker-compose build
Database migration
In order to migrate the database, run the commands:
docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate