Deploy Django App With Docker and Gunicorn in 3 Easy Steps
In our last article, we showed you how to create a simple Django app in 5 steps. If you haven’t read it yet, go ahead and check it out!
Create a simple Django Backend App in 5 Steps
Django is an incredibly powerful and popular web framework with tons of plugins available and a large community. It can…
Starting the server with “python manage.py runserver” is only suitable for development purposes and not ideal for production. In this article, we will demonstrate how to deploy a Django app using Docker containers and Gunicorn (a python WSGI HTTP server for UNIX) in 3 easy steps.
Step 1: Create a Dockerfile
In the project folder, create a file called Dockerfile (without an extension). This file will be used to create a docker image in the next step.
What it does is very simple: it takes “python” as base image and installs Django and Gunicorn in the container. Then it copies all the code from the current folder to the working directory of the container.
Lastly, it starts an Http server using Gunicorn on port 8000.
WORKDIR /codeRUN pip install Django gunicornCOPY . .ENTRYPOINT [“gunicorn”, “awesome_project.wsgi:application”, “--bind”, “0.0.0.0:8000”]
Step 2: Build docker image
In this step, you will build a docker image from the Dockerfile we created in step 1 and tag it as “awesome-app:latest”
docker build -f Dockerfile -t awesome-app:latest .
Step 3: Run docker image
After the docker image has been created successfully, run a docker container with the image. In order to make it accessible from outside the container, map the port to your local machine using the -p flag.
docker run --rm -p 8000:8000 awesome-app
the server should now be running in the container and accessible at localhost:8000 on your machine.
Let’s check if the server is running and if we can access it. As we did in the last article, we use CURL to send a request to the server.
curl http://127.0.0.1:8000/hello/Hello! This is awesome!
You should see the message “Hello! This is awesome!” on the console if you finished the steps correctly!
If you like my stories, don’t forget to give them a clap 👏 and share them with your friends! Cheers! 😉
If you want to revisit my previous article where we created a simple Django app, follow this link: