Dockerizing A Django Project
Docker containers share the host OS but have their own isolated process and memory space. Its main benefit is that it makes setting up environments that
closely match development and production much easier.
So let’s dockerize a django poject.
Create a folder in your OS and open terminal. Then clone an existing project to that folder.
Open the project in an IDE and in that case, I am using Pycharm. And Create a file name “Dockerfile” in the base directory in you project, where we going to identify some specifications that docker follows to create a container image.
Docker contains two stages:
- Build Stage: Create an image and run all the commands that are defined on the the image so that the image is prepared. It’s create an container image to run your application.
- Run stage
In your docker file, put these lines:
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
COPY . /code/
RUN pip install -r requirements.txt
- “FROM python:3”, is the base project set-up to install all the dependencies to run your application. We are using python as our base because django is a python project. 😛
- “ENV PYTHONUNBUFFERED 1”, To create environment variable and for standard STDS. Passing outputs to the terminal.
- “RUN mkdir /code”, To make a directory in the container. It will hold all the codes of our project.
- “WORKDIR /code”, To interact with docker container.
- “COPY . /code/”, To copy all the projects codes to that directory in the container because initially container doesn’t hold the project codes.
- “RUN pip install -r requirements.txt”, To install requirements files that are needed to run the project.
Next, create a file Named as “docker-compose.yml” in the same directory.
In your docker-compose.yml file, put these lines:
command: python manage.py runserver 0.0.0.0:8000
- “version: ‘3’”, Is to define the version of docker-compose. It usually means python version 3 is using in this project and this could any other version depend on the project.
- Next you have to define services for this project which follows as “services: web: build: .” It is used to build the container and “build: .” denoting the location of “Dockerfile” as it is in the same location.
- “command: python manage.py runserver 0.0.0.0:8000” is specifying localhost to run the container.
— .:/code”. The main task of the two lines to synchronize any changes in the project in real time. Any changes will be updated to docker container file.
— “8000:8000””, Defining the port to map it from docker to the local host.
Now, your docker configuration is ready. All you have to do is to migrate your project so that the project is ready to run. Use this line:
“docker-compose run web python manage.py migrate”
docker-compose run web python manage.py migrate
Step 1/6 : FROM python:3
Step 2/6 : ENV PYTHONUNBUFFERED 1
---> Using cache
Step 3/6 : RUN mkdir /code
---> Running in d29c6162dd2f
Removing intermediate container d29c6162dd2f
Step 4/6 : WORKDIR /code
Removing intermediate container daf2b6b16d28
Step 5/6 : COPY . /code/
Step 6/6 : RUN pip install -r requirements.txt
---> Running in 7116b1fbb263
Collecting certifi==2017.11.5 (from -r requirements.txt (line 1))
Downloading certifi-2017.11.5-py2.py3-none-any.whl (330kB)
Collecting chardet==3.0.4 (from -r requirements.txt (line 2))
Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting coreapi==2.3.3 (from -r requirements.txt (line 3))
Collecting coreschema==0.0.4 (from -r requirements.txt (line 4))
Collecting Django==1.11.7 (from -r requirements.txt (line 5))
Downloading Django-1.11.7-py2.py3-none-any.whl (6.9MB)
Collecting djangorestframework==3.7.3 (from -r requirements.txt (line 6))
Downloading djangorestframework-3.7.3-py2.py3-none-any.whl (1.5MB)
Collecting idna==2.6 (from -r requirements.txt (line 7))
Downloading idna-2.6-py2.py3-none-any.whl (56kB)
Collecting itypes==1.1.0 (from -r requirements.txt (line 8))
Collecting Jinja2==2.10 (from -r requirements.txt (line 9))
Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe==1.0 (from -r requirements.txt (line 10))
Collecting Pygments==2.2.0 (from -r requirements.txt (line 11))
Downloading Pygments-2.2.0-py2.py3-none-any.whl (841kB)
Collecting pytz==2017.3 (from -r requirements.txt (line 12))
Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
Collecting requests==2.18.4 (from -r requirements.txt (line 13))
Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting uritemplate==3.0.0 (from -r requirements.txt (line 14))
Collecting urllib3==1.22 (from -r requirements.txt (line 15))
Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
Building wheels for collected packages: coreschema, itypes, MarkupSafe
Running setup.py bdist_wheel for coreschema: started
Running setup.py bdist_wheel for coreschema: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/9a/b1/14/41609280544f21835616bdba5658a38742c6227669bdd9067b
Running setup.py bdist_wheel for itypes: started
Running setup.py bdist_wheel for itypes: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/ed/2e/73/c7defcaea08d1c241902054eecdd713211b7b1f8c394cbfd72
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
Successfully built coreschema itypes MarkupSafe
Installing collected packages: certifi, chardet, urllib3, idna, requests, MarkupSafe, Jinja2, coreschema, uritemplate, itypes, coreapi, pytz, Django, djangorestframework, Pygments
Successfully installed Django-1.11.7 Jinja2-2.10 MarkupSafe-1.0 Pygments-2.2.0 certifi-2017.11.5 chardet-3.0.4 coreapi-2.3.3 coreschema-0.0.4 djangorestframework-3.7.3 idna-2.6 itypes-1.1.0 pytz-2017.3 requests-2.18.4 uritemplate-3.0.0 urllib3-1.22
Removing intermediate container 7116b1fbb263
Successfully built f80bd3d60c22
Successfully tagged documentation_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Operations to perform:
Apply all migrations: admin, auth, contenttypes, main, sessions, snippets
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying main.0001_initial... OK
Applying main.0002_question_option_choice... OK
Applying sessions.0001_initial... OK
Applying snippets.0001_initial... OK
So you can see, this line migrate your project and install all the dependencies to run your project. You can also use manually this line to migrate:
It will do the same trick as well.
Next up is to run your project. So let’s run it.
Starting documentation_web_1 ...
Starting documentation_web_1 ... done
Attaching to documentation_web_1
web_1 | Performing system checks...
web_1 | System check identified no issues (0 silenced).
web_1 | December 03, 2017 - 08:42:34
web_1 | Django version 1.11.7, using settings 'Documentation.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
And you projects starts running the localhost using docker. 😍
That’s it. You can easily use docker to run project which is effective and efficient and less struggle. 😆
You can check this link how to install docker in your Linux based OS: Docker
If you do find any mistakes in the post, feel free to comment. I will appreciate it because I am also new in using docker. But I am having fun with that. ^_^