Docker for Django

docker --version

Case 1: I’m creating a Django project from scratch and I want to set up a development environment with Docker.

git clone https://github.com/joeymasip/docker-django.git
#cd to the location where you cloned the project
cd ~/Development/docker-django
#start the containers
docker-compose up -d
docker ps
docker-compose run django django-admin.py startproject project_name .
docker-compose run django python manage.py startapp app_name
docker-compose up -d
docker ps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'app_name',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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',
},
}
}
#app_name/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
pass
AUTH_USER_MODEL = 'app_name.User'
docker-compose exec django bash
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
http://127.0.0.1:8000/
http://127.0.0.1:8000/admin/

Case 2: I already have a Django project

git clone https://github.com/joeymasip/docker-django.git
docker-compose up -d
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',
},
}
}
docker-compose exec django bash
python manage.py migrate
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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store