Sitemap

Docker for Django(Nginx and MySQL)

Ken Kono
2 min readJun 18, 2019

Environment

  • MacOS Mojave
    Please kindly install docker for mac before.

Directory structure

The structures are divided by python , nginx , mysql .

django
├── docker-compose.yml
├── mysql
├── sql
├── nginx
│ ├── conf
│ │ └── app_nginx.conf
│ └── uwsgi_params
└── python
├── Dockerfile
└── requirements.txt

Setting for Django

Make Dockerfile .

Then, we make requirements.txt
Version is changed by your environment.
- uwsgiis used for linking the Nginx.
- The PyMySQL is used for linking the MySQL.

Next, we write docker-compose.yml .
- About the command , open the port 8001 to link the Nginx.
- app.wsgi ‘s app is a Django project name.
- --py-autoreload 1 is to reload the file when you change something at Django.
Ref: https://stackoverflow.com/questions/8831748/how-to-set-up-autoreload-with-flaskuwsgi/15864997#15864997
- --logto /tmp/mylog.log is the description for log.

Setting for MySQL

Noted that after stopping your container, the data at container is terminated.
To keep the data, we should synchronize at mysqlfolder at local.
The command is to set the character code (default is latin1).
You can modify the environmet space for your situation.

The SQL sentence mounted at docker-entrypoint-initdb.d is executed when the container starts the first time.
When you want to do the test at Django, we need to give authentication to make test DB.
Setting by hands is very boring. So let’s make a file sql/init.sql to automate.

Setting for uWSGI

Below is the setting file uwsgi-paramsfor Nginx’s uWSGI module.

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;

uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;

uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

Setting for Nginx

For the security aspect, we should write server-tokens off; to hide Nginx’s version.

Final docker-compose.yml

Start the Django project

docker-compose run python django-admin.py startproject app .

After finishing the above command, you can see below the structure of directory.

django
├── docker-compose.yml
├── mysql
├── sql
├── nginx
│ ├── conf
│ │ └── app_nginx.conf
│ └── uwsgi_params
├── python
│ ├── Dockerfile
│ └── requirements.txt
├── src
│ ├── app
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ └── manage.py
└── static

Connection to MySQL

We modify the settings.py .
The NAME is DB name of MySQL.

Run the migrate and make superuser.

docker-compose run python ./manage.py migrate
docker-compose run python ./manage.py createsuperuser

Due to Nginx’s settings, staticfile is stored at the Nginx server not Django by itself.
To reflect this setting, add below sentences at settings.py .

STATIC_URL = '/static/'
STATIC_ROOT = '/static'

Then, run below command.

docker-compose run python ./manage.py collectstatic

We finished all the settings. Finally, we will launch Django project.

docker-compose up -d

Notice

Maybe, we manage the above project by git. It is not good for security to open our MySQL data.
So below is a good option to hide the data at public.
Add at .gitignore .

mysql/*

--

--

Ken Kono
Ken Kono

Written by Ken Kono

A hired gun in the IT software industry. ServiceNow

Responses (2)