Dockerize Django application | extrovert.dev -->

Dockerize Django application

Dockerize Django application
Tuesday, August 11, 2020
Hey there! I am very excited to write on one of my personal favourites ie: docker. Nowadays there is a huge buzz for docker among the developers because it eased the production of the software we build. For this post, I will be using the music api which was built with Django, Django REST Framework(DRF) in our previous posts.
Dockerize Django application

Why docker?

Docker maintains standardization of the deployment environment. Software today adopting complex configurations with frameworks and architectures. Your deployment environment may be unfriendly due to its mismatch with the dependencies that are well suited to your development machine. Simply a thing that works with Ubuntu may not work with Arch.

Docker vs Virtual Machines

Virtual machines run your software like a separate computer whereas docker shares the host environment. The image below provides a better understanding of docker and virtual machine.
Dockerize Django application
credits: docker

Now let's Dockerize

We need to specify steps to build our image using Dockerfile. It is a simple set of commands organised to perform our building. In your root folder where manage.py exists add this Dockerfile
# official Python runtime as a parent image
FROM python:3.6
# enviroment variables
ENV PYTHONUNBUFFERED 1
# create root directory
RUN mkdir /music_api
# Set the working directory
WORKDIR /music_api
# Copy the current directory contents into the container at /music_api
ADD . /music_api/
# Install packages specified in requirements.txt
RUN pip install -r requirements.txt
view raw Dockerfile hosted with ❤ by GitHub

Now we will run our containerized application using docker-compose. Docker-compose is configured using .yml file
#docker version
version: '3'
services:
web:
#this sets build directory
build: .
#making migrations and fire up server
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
container_name: music_api
#mount source to container
volumes:
- .:/music_api
ports:
- "8000:8000"

Now run your application using docker-compose up (127.0.0.1:8080). This aggregates all the running containers.

Thank you for reading!



wb_incandescent You may like

0 Response to Dockerize Django application

Comments are personally moderated by our team. Promotions are not encouraged.

Post a Comment