- Django Docker Setup Free
- Django Docker Settings.py
- Django Docker Setup Tutorial
- Django Docker Setup Windows 10
Requeriments
Django, described as the The web framework for perfectionists with deadlines is a high-level Python Web framework that encourages rapid development and clean design. In this post I will show you how to get Django up and running in a docker container. The first thing you will need to do is install docker compose.
You should have python3 installed. To check which version of python do you have installed, just run:
- Set up S3 and configure Django to store static and media files in a bucket, outside of the Docker volume; Set up a CI/CD pipeline on GitLab; Configure a containerized Django app running on an EC2 instance to send logs to Amazon CloudWatch; Happy deploying! Django on Docker Series: Dockerizing Django with Postgres, Gunicorn, and Nginx.
- Docker python3 deployment fail 6th October 2021 django, docker, docker-compose, dockerfile, python I deployed a docker container with this Dockerfile for my Django-rest-application.
Step 1 - Set Up Your Development Environment
Whenever you are starting a new web development project, it’s a good idea to first set up your development environment. So, let's create a new directory for your project to live in, and move into it:
Once your inside the main directory, it’s a good idea to create a virtual environment to manage dependencies. There are many different ways to set up virtual environments, but here you’re going to use venv
:
This command will create a folder venv in your working directory. Inside this directory, you’ll find several files including a copy of the Python standard library. Later, when you install new dependencies, they will also be stored in this directory. Next, you need to activate the virtual environment by running the following command:
You’ll know that your virtual environment has been activated, because your console prompt in the terminal will change. Itß should look something like this:
Step 2 - Create a Django Project
Once you have a python virtual environment enable, you need to add the standard Python project dependencies file which is usually named requirements.txt
, and the Django dependency to it. Good, once you have created and added the dependencies, the file should look like this:
With the Django dependency added, you can then install Django using the following command:
Once installed, you will find that you now have access to the django-admin command line tool, which you can use to generate the project files and directory structure needed for the simple “Hello, World!” application.
Let’s take a look at the project structure the tool has just created for you:
You can control the application for development purposes using the manage.py
file, which allows you to start the development test web server for example:
Then, in your browser go to http://127.0.0.1:8000/, and you should see the following:
Congratulations, you’ve created a Django site! The next step is to create apps so that you can add views and functionality to your site.
Step 3 - Create a Django Application
To create the app, run the following command:
This will create another directory called hello_world with several files:
__init__.py
tells Python to treat the directory as a Python package.admin.py
contains settings for the Django admin pages.apps.py
contains settings for the application configuration.models.py
contains a series of classes that Django’s ORM converts to database tables.tests.py
contains test classes.views.py
contains functions and classes that handle what data is displayed in the HTML templates.
Once you’ve created the app, you need to install it in your project. In mysite/settings.py
, add the following line of code under INSTALLED_APPS
:
That line of code means that your project now knows that the app you just created exists. The next step is to create a view so that you can display something to a user.
Create a View
Navigate to the views.py
file in the hello_world
directory. There’s already a line of code in there that imports render(). Add the following code:
You’ve defined a view function called helloworld(). When this function is called, it will render an HTML file called hello_world.html
. That file doesn’t exist yet, so let's create it. Create that directory and subsequently a file named helloworld.html inside it:
Add the following lines of HTML to your file:
You’ve now created a function to handle your views and templates to display to the user. The final step is to hook up your URLs so that you can visit the page you’ve just created. Your project has a module called urls.py
in which you need to include a URL configuration for the hello_world app. Inside mysite/urls.py
, add the following:
This looks for a module called urls.py
inside the hello_world
application and registers any URLs defined there. Whenever you visit the root path of your URL (localhost:8000), the hello_world
application’s URLs will be registered. The hello_world.urls
module doesn’t exist yet, so you’ll need to create it:
Now, when you restart the server and visit localhost:8000, you should be able to see the HTML template you created:
Good job!!! You’ve created your first Django app and hooked it up to your project. Now, let's run inside docker container.
Step 4: Dockerize the Application
Setup Docker
Before creating a container for the Django application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions;
Make the docker App image
The next stage is to add a Dockerfile
to your project. The structure of a Dockerfile
can be considered a series of instructions on how to build your container/image.
Start the Dockerfile by creating an empty file named Dockerfile
in the root of your project. Then, complete each instruction according to the following example:
The first directive in the Dockerfile, FROM python:3.7
tells Docker which image to base our container on. We use the official Python image from Dockerhub that comes with Python and Linux setup for you, ready for use in a python project.
You are now ready to build the container image, and then run it to see it all working together.
Building and Running the Container
Building the container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile
and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile
and leave you with a container that is ready to start.
To build your container, you will use the docker build
command and provide a tag or a name for the container, so you can reference it later when you want to run it. The final part of the command tells Docker which directory to build from.
The final step is to run the container you have just built using Docker:
The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine. After you run this command, you should be able to visit http://localhost:8000 in your browser to see the “Hello, World!” response.
You can see the Docker containers that are currently running on your system (along with their Container IDs) with:
To turn off your Docker container, run:
Step 5: Push to cloud
1. Create your app
In order to install the django docker container, create a new app via cli or admin panel and set a port to 8000.
2. Push your docker container
Before push the container you need to enable the URL into the django ALLOWED_HOST. So go on mysite/setting.py
:
Now just build and push the container:
3. Set up resources
5. Logs and app status
6. Release
Now you can deploy your django app without a massive build time.
Bonus 1: SSL certificate for HTTPS
It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.
Django Docker Setup Free
Bonus 2: Autoscaling

With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.
Now you can deploy your django app without a massive build time.
Use the right tools can make the difference and save time. Due to that, the initial steps are very important when starting a new project from scratch. Then, I am going to show you how to set up in Windows 10, a Django project with Docker in Visual Studio Code where you can debug your project an easy way.
If you want to see the code more in details, You can find the final repository for the project on my Github.
Below you can find the links to download the required software.
Once you have installed the required software, we can start to set up our environment for Django.
Table of Contents
1. Installing extensions
2. Cloning the repository
3. Setting up Django with Docker in Visual Studio Code
4. Setting up Django database
5. Setting up Debug mode
6. Setting up Linting Python in Visual Studio
1. Installing extensions
Let’s install the required extensions in Visual Studio Code for our environment.
We have to install the following extensions as I show you below
- Docker
- Remote – Containers
- Python
[Back to Top ↑]
2. Cloning the repository
As I told you before, if you want to see the code more in details, you can find the final repository for the project on my Github.
So, in my case I am going to clone the repository from Github.
In the branch”django-setup-project-docker” you can see the code.
And start a new branch as “initial”.
[Back to Top ↑]
3. Setting up Django with Docker in Visual Studio Code
Open the folder in Visual Studio Code.
Press F1 and search “Add Development Container Configuration Files” followed by search “Python 3 & PostgreSQL”
At this point, you can notice some files that was added in your project. These files contain the Python and PostgreSQL configuration for Docker. The following step is re-open the project in a container as I show you in the image below.
Now, let’s modify files a bit. Let’s start with “devcontainer.json” as I show you below.
We continue with “docker-compose.yml”.
And finish with “Dockerfile”.
To continue and according to our configuration in “Dockerfile”, we must create a folder called “requirements” which will have several packages based on whether our need is to debug. For that, there will be the following files:
The first file called “requirements.txt” which is the core of our environment.
And the second file called “development.txt” to debug.
Now, we rebuild the container.
At this point, if you want to check that everything is ok, you can run the command below in a bash console (Terminal in VSC) getting the following result.
Now, let’s create the project based on Django
If everything is ok, you should be seeing a file structure like the following.
[Back to Top ↑]
4. Setting up Django database
So now it is time to set up the Django database. We are close to finishing our environment.
Let’s do some changes in “settings.py”. First of all remove the string connection for sqlite3

Then, let’s add the following code
Set up the database through pgAdmin based on the previous configuration.
So now it is time to run the server.
Surely, you got the following output.
Right now you are able to go to http://127.0.0.1:8000/ and get the image below.
Let’s continue with the migrations.
Run the code below.
After that, run the following code to create the super user. You can check the correct creation of the super user through pgAdmin.
Django Docker Settings.py
[Back to Top ↑]
5. Setting up Debug mode
Finally, we have arrived at the end of this post. The set up of debug mode of Django project with Docker in Visual Studio Code. So, let’s do it.
Go to a debug configuration as I show you in the image below.
Create a “launch.json” file.
Press “Django” and select it.
Change the code base on the code below.
Django Docker Setup Tutorial
Now, you can run Debug mode. Consider that the port will be change for Debug mode. So, to access it, I suggest you “Follow Link (ctrl + click) as the image below shows.
[Back to Top ↑]
6. Setting up Linting Python in Visual Studio Code.
Linting allows you to identify problems in your code. It highlights syntactical and stylistic problems in your Python source code
To enable linters other than the default PyLint, open the Command Palette (Ctrl+Shift+P) and select the “Python: Select Linter command” and then “Pylint”.
Django Docker Setup Windows 10
This command adds a file with code below.
[Back to Top ↑]
Final Thoughts
So now, you are free to add new configurations or even apps to the project and develop as normally. I hope this tutorial was helpful for you. Let me know any doubts about it and good luck!