![django docker tutorial django docker tutorial](https://miro.medium.com/max/1000/1*R2Wu9DhUbA-W9LBU0fwefQ.png)
Lets set the docker-compose settings and get the system running. Note: I have hard coded the database engine, this will work fine because you won't be changing it often as other parameters but you can pass it via environment variable too.
#Django docker tutorial code
These are the necessary setting for this starter app, you might need to move other variables like the Queue services credentials, Memcache credentials which will be set in the container service so they cannot be hard coded and every authentication key out of the code so they don't get leaked.
![django docker tutorial django docker tutorial](https://storage.caktusgroup.com/media/blog-images/Docker_Image.png)
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'PASSWORD': os.environ.get('DATABASE_PASSWORD'), 'ENGINE': 'django.db.backends.postgresql_psycopg2',
![django docker tutorial django docker tutorial](https://i.ytimg.com/vi/q_cZO1WBWCw/maxresdefault.jpg)
# its good idea to set default as False and over ride to True in testing or development. SECRET_KEY = os.environ.get('SECRET_KEY', Debug These are the variables you need to change in settings.py file, you can replace this with the existing ones. Os.environ.get() is what I prefer because it allows you to set a fall back value in case environment variable is absent. It's done using docker-compose.yml.īefore we proceed further we need to make a few changes to the settings.py so it can take parameters like Database username, password, Secret Key and other credentials set in ENVIRONMENT rather than hard coding them into the code We have the Django application in a container now lets add the required services and get them running together. Hit ctrl+c it will close the Django server and also the container along with it. Note: You will not be able to access the server from or because your system is not yet lined to the container. If you add RUN python manage.py runserver you can see a Django server running, but don't add it here we will be starting the Django server from docker compose. you will will see a container being started, followed by packages being installed and close. Navigate to the app directory (do not activate virtualenv) and if you execute docker build. You can use any image of your choice if required. In here python:2.7 is the base image for the container from Official Python Repository on docker hub. # copy the contents of entire directory to the directory in the container # ( use any name, make sure to change it in below lines as well ) This container will have the Django application ( code ) and the dependencies only service like database (MySQL, PostgreSQL, etc), cache (memcahce, reddis) or queue service (rabbitmq ) etc will not be in this container it will be run in a parallel container. We need to get the Django project into a container, to do that a Dockerfile is used to define the specifications for the container. No changes have been done to this project that means if you run this you will get a "welcome to Django page" and the app still uses the sqlite database. But you can use the below steps and apply it to an existing project as well. If you don't have this file go to your root director of app and run the following pip freeze > requirements.txt ( if you use virtualenv make sure your virtual env is active before you run this).įor demo I'm using a standard Django app created with django-admin startproject app.