Setting up Django in a Docker Container on Windows 10

Setting up Django in a docker container proved to be harder than I expected, so I am recording the steps here.


  1. Get Docker Desktop
  2. Get Ubuntu 20.04 on Windows Subsystem for Linux (WSL)
  3. Install Django
  4. Create Django project
  5. Install Docker inside Ubuntu
  6. Create Docker project
  7. Run Docker container

Get Docker Desktop

Install docker desktop from Docker desktop web site.

Get Ubuntu 20.04 on WSL

  1. Open command prompt.
  2. Run wsl --install -d Ubuntu-20.04. If you did not have any Linuxes installed, restart (it’s still Windows), the installation will continue after restart.
  3. Run wsl -l -v to ensure you have Ubuntu 20.04 and WSL 2.
  4. If you have more than one Linux installed, run wsl --set-default Ubuntu-20.04
  5. Run bash.
  6. Run lsb_release -a to check Ubuntu version.

Install Python and Django

In bash run the following:

python3 --version                  # ensure it's at least Python 3.8
sudo apt update                    # get available apt package versions
sudo apt -y install python3.8-venv # you don't want python3-venv, it leads to trouble
sudo apt -y install python3-pip    # install PIP
mkdir myproj && cd mproj           # create project dir
python -m venv .venv               # create a virtual environment
. .venv/bin/activate               # enter the virtual environment
pip install django                 # install Django into the venv

Create Django Project

Run in bash:

django-admin startproject django_web . # create Django project named django_web in the current dir
python3 migrate              # prepare the project
python3 runserver            # run server locally

Open localhost:8000/ in the browser and make sure Django start page appears.

Go back to bash and press Ctrl+C to stop Django server.

Install Docker inside Ubuntu

sudo apt -y install docker-compose

NOTE: I was not able to make Docker daemon run inside WSL. Instead, docker process running inside Ubuntu connects to the Docker Deskop running on the host Windows OS. It is still needed to install the docker packages inside Ubuntu.

Create docker project

Run in bash, while still in the venv:

pip freeze > requirements.txt

Create file named Dockerfile with the following contents:

# Pull base image
FROM ubuntu:20.04

# Set environment variables

RUN apt update
RUN apt -y install software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa -y
RUN apt update
RUN apt -y install python3.8 python3-pip 

# Set work directory

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

Create file named docker-compose.yml with the following contents:

version: "3."
    build: .
      - "8000:8000"
    command: python3 runserver
      - .:/code

Create file named .dockerignore with the folliwing contents:


Run Docker container

sudo docker-compose up

This should build the image and start the container. You may see Windows Defender warning about Docker trying to listen on port 8000. After the container has started, open the browser again on http://localhost:8000/, and you should see Django hello window.

1 Comment

  1. Thank you for this post Vanya! I had been running Ubuntu 20.04 as a VM, but due to HD size constraints, I removed VMWare and the VM. I decided that since I was only using the VM for Django, I can get the same functionality using WSL2 and Docker. Thank you again my friend!

    P.S. Website is coming soon!!


Leave a Reply

Your email address will not be published. Required fields are marked *