We are going to see how to create a django development environment with docker-compose.
Why? because in that way we don't need to install dependencies, databases and other stuff in our own machine.
Well, that's not completely true, not all IDEs have support for docker environments, in that case, you'll need to use a local virtual environment where to install all the dependencies and avoid some disturbing errors in your IDE, but that' isn't a big deal since is really easy to create a virtual environment for that concrete problem (or you can pay a PyCharm professional licence and you will have an IDE with docker environment support).
To avoid to repeat contents, just follow the official manual:
https://docs.docker.com/install/linux/docker-ce/ubuntu/
Once installed, it's a good idea to do some extra steps from that other manual:
https://docs.docker.com/install/linux/linux-postinstall/
The steps I followed, and I recommend are:
Manage Docker as a non-root user
Configure remote access with systemd unit file
Managing docker as non-root user avoid us to preface all the docker commands with sudo
Configuring the remote access, will allow us to link our docker applications with the PyCharm Community Edition to enjoy some features.
As in the previous step, we'll follow the official documentation to install docker-compose:
https://docs.docker.com/compose/install/
Once we have installed our the docker and docker-compose requirements, we are going to configure our Django project to be launched into a docker container.
We need to add some requirements to our django project to create make it able to communicate with our MySql server.
Django==3.0
mysqlclient==1.4.6
Create a Dockerfile in your's project root path:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . /code/
Explanation:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
With that lines we specify what python interpreter to use (python 3.6), and we create a environment variable to make python to be unbuffered (read that python documentation page for more information)
RUN mkdir /code
WORKDIR /code
With that lines we create the folder where our project code will be, and we move to that directory.
COPY requirements.txt /code/
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . /code/
We copy our project requirements file, upgrade pip and install our django requirements.
To finalize we copy our code into the container.
Here someone can ask: And why not to use ADD if it have the same functionality? Well, for that just read that docker best practices.
version: '3'
services:
db:
container_name: db
image: mysql/mysql-server:5.7
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: 'your_project_database'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
web:
container_name: web
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
In that file you just need to modify your database.
The docker-compose.yml defines to services, a service called db, our database, and another service called web, our project service.
The web service have an options depends_on, that makes our service web to wait for the service db.
The first time you run your docker-compose you will receive an error from the web service (in logs or from the running app inside) saying that is unable to connect with the database server.
Don't worry, have an explanation but it's not needed to do anything more, second time you run the docker-compose services that error will disappear (or you can use some script like wait-for-it).
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'project_database_name',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': 3306,
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
In bash, we need to execute that commands to build the docker images, and create and start the containers.
docker-compose build
docker-compose up -d
The -d option of the up command makes all to run in background.
To see the logs:
docker-compose logs [-f] [service]
The -f option makes to follow the logs in console. You can specify the containers to log (db, web in that case). If no services are specified show all the containers log.
As extra step, we can connect PyCharm CE with docker in (limited features, more features with the professional edition).
To do that you need:
To connect with the container open File > Settings > Build, Execution, Deployment > Docker and add a configuration like that:
January 31 2020 *Last updated May 17 2020
#docker #development #docker-compose #python #django #mysql