Docker for Django
This blog post is a small guide for getting started with your Django environment with Docker. Since I got very positive feedback from the blog post about Docker and Symfony4, I decided to do the same with Docker and Django.
In this example we’re going to work with Django 1.11 (LTS), Python3 and MySql 5.6
Before we start, you’ll need to install Docker in your machine. You can download it from the official website.
Once Docker is installed, I strongly recommend playing with the getting started guide. Here the guide for macs and here the guide for windows. However, if you’re lazy like me, just use this command to make sure it’s installed.
docker --version
Once all of that is out of the way, we can start with the Django project and the Docker environment that will run it.
We have two cases I’m going to tap on.
Case 1: I’m creating a Django project from scratch and I want to set up a development environment with Docker.
Step1: Clone the docker-django repository which has the docker configuration files.
git clone https://github.com/joeymasip/docker-django.git
Step2: Create the Django project.
First off, let’s start docker containters with the project we just downloaded.
#cd to the location where you cloned the project
cd ~/Development/docker-django
#start the containers
docker-compose up -d
This command starts the containers. The parameter -d makes them run in the background. If you omit the -d you’ll see the log.
Docker should start building (if it’s the first time for these images) and running with the containers in the background.
If you now try to run
docker ps
in your console, you’ll see that MySql container is running, but Django’s is not. This is normal, as we haven’t installed Django yet in our project.
Now we’ll create the django project with the following command (replace project_name for your project name)
docker-compose run django django-admin.py startproject project_name .
Note: Do not forget the . in the end
Step3: Create the Django application.
Now we’ll create the django application with the following command (replace app_name for your app’s name)
docker-compose run django python manage.py startapp app_name
Now, try running the same
docker-compose up -d
Now, if you run the docker ps
docker ps
This time, Django’s container will have been started.
So right now, if you just open your browser and type http://127.0.0.1:8000/ you should see it working, so you’re already set up to develop!
First off though, let’s update settings.py so we can use our mysql container instead of Django’s sqlite.
Step4: Update settings.
First let’s add your app_name in the installed apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',
]
Now let’s configure your settings.py so the database settings points to the database service from docker.
Change:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
To:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'docker_django_db',
'USER': 'dbuser',
'PASSWORD': 'dbpw',
'HOST': 'mysql',
'PORT': '3306',
'TEST': {
'NAME': 'docker_django_db_test',
},
}
}
Step5: Create the User model.
Under your app_name/models.py file, just create a User model that extends from Django’s Auth model.
#app_name/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
Once this is done, just tell your project that we’ll be using our User model. This is best practices in case you ever need to make changes to the User model. So in your project_name settings.py, add this line:
AUTH_USER_MODEL = 'app_name.User'
Now our User is plugged in.
Step6: Run migrations.
To run migrations, we need to first enter the python django bash.
docker-compose exec django bash
Once in, we can make the migrations if it’s the first time we create the app, as we won’t have any.
python manage.py makemigrations
And also run them
python manage.py migrate
If you want to create an admin user to log in into Django’s admin panel,
python manage.py createsuperuser
That’s it!
Now just open your browser and type
http://127.0.0.1:8000/
http://127.0.0.1:8000/admin/
You’re already set up to develop, so happy coding with docker!
Case 2: I already have a Django project
Step1: Clone the docker-django repository which has the docker configuration files.
git clone https://github.com/joeymasip/docker-django.git
Step2: Move all files to your already created Django project.
1. The docker folder containing python + django and a MySQL container config for it.
2. The docker-compose.yml file
3. The .env file
Step3: Start the docker images inside your Django project folder.
cd into your Django project folder and type the following command
docker-compose up -d
This command starts the containers. The parameter -d makes them run in the background. If you omit the -d you’ll see the log.
Docker should start building (if it’s the first time for these images) and running with the containers in the background.
Step4: Update database settings.
So if you’re using the docker-compose.yml out of the box, your database name, user and pw need updating in your project’s settings.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'docker_django_db',
'USER': 'dbuser',
'PASSWORD': 'dbpw',
'HOST': 'mysql',
'PORT': '3306',
'TEST': {
'NAME': 'docker_django_db_test',
},
}
}
Step5: Run migrations.
docker-compose exec django bash
Once in, we can run the migrations.
python manage.py migrate
Now, open a new chrome tab and type the following URL. The port is the one we set up in the docker-compose.yml
http://127.0.0.1:8000/
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/whatever-slug-you-want-from-your-project
You should see it working.
You’re already set up to develop, so happy coding with docker!
Originally published at Joey’s blog.