Django CMS on Docker
You don’t want to clean your OS each time when you run something. I need to support Django CMS, frontend side, maybe even backend side and like I said, I don’t want to move everything into my OS: virtualenv, pip, packages, etc. Especially if I need to support projects with different python versions.
Here comes a life saver — Docker. Easy to use but needs some time to play with configuration to get what you want.
Let’s start with your docker file:
Specify python version; disable virtual environment for pip, you don’t need virtualization inside virtualization. During image cooking we will copy requirements and install them. Expose any port you like (8090), switch to working directory and run the CMS server with specific docker configuration.
Cooking image is not the final step. You need to control the flow that’s where the make will help you.
Step by step
Build an image and call it cms-python:
docker build . -f python.dockerfile -t cms-python
Run cms-mysql container with specific password and mysql version:
docker run --name cms-mysql -e MYSQL_ROOT_PASSWORD=secretpassword -d mysql/mysql-server:5.7
Create a database for your application, maybe it was created earlier so || : will allow us to go next if this step fails:
docker exec cms-mysql bash -c "mysql -u'root' -p'secretpassword' -e 'CREATE DATABASE cms'" || :
Run cms-python container; link it to the cms-mysql container with mysql alias for the host; mount a source folder using absolute path; publish application port (8090) and give cms-web name to container:
docker run --name cms-web --link cms-mysql:mysql -v "${current_dir}/src:/cms/src/" -p 0.0.0.0:8090:8090 -d cms-pythonTime to fill the database using django migrations:
docker exec cms-web bash -c "python manage.py migrate --settings=config.settings.docker"
Last step, create an admin user for the app:
docker exec -it cms-web bash -c "python manage.py createsuperuser --settings=config.settings.docker"
How the mysql settings looks like?
WholeMakefile as a summary for better docker management.