Start Django project with Docker
Steps to start Django projects using Docker as a part of the workflow. We use
projectname to refer the project name.
- Having Docker installed. If you are using on Mac OS X or Windows you should install Docker Toolbox.
You can download the source code using this link.
You can consult deployment diagrams related to this post in this link.
Step One - Setup the Structure.
Download the project structure
The project containing the general structure is discharged.
git clone https://github.com/mmorejon/docker-django.git projectname
Remove Git folder
.git is removed in order to create a new repository.
cd projectname rm -rf .git/
Initialize new repository inside the project
Initialize a new version control over the folder to track all changes.
Step Two - Create Docker Image
Create Docker image
You must create the Docker image for the project. The image contains the installation requirements established into
requirements.txt include basic requirements to start and deploy our Django application. If you need to add new elements now is a good moment. Use the following command to create the image:
docker build -t projectname:1.0 .
Every time that you modify the elements inside
requirements.txt file you should repeat this step.
Docker Compose configuration
In the file
docker-compose.yml is set the image name that will be used. The image name was setting in the previous step. In the file change the following line:
Step Three - Create Django Project
The project is created using the same commands described into Django website.
docker-compose run web django-admin startproject projectname .
Test the system
Start the system typing this:
The system can be tested writing in the browser the following:
http://<machine-ip:8000>. The port can be changed in the
Stop the system
The system is stopped to keep on the configurations typing this:
Step Four - Create Application
Use the following command to create an application inside Django project.
docker-compose run web python manage.py startapp app
Step Five - Create User
The users can be created using the same command line explained in Django documentation.
docker-compose run web python manage.py createsuperuser
Step Six - Production Environment
If you desire to take advantage of the application like production environment, you just need to configure the following files:
Add the this line at the end of the
STATIC_ROOT = './static/'
Add this line
command: ./run-production.sh in
docker-compose.yml file. The file should look like this:
web: image: projectname:1.0 command: ./run-production.sh volumes: - .:/code ports: - "8000:80"
run-production.sh include the necessaries configurations to run your Django application like a production environment.