Simple Steps to Dockerize a python django web application
Create a file named requirements.txt
pip freeze > requirements.txt
Create a file named Dockerfile
A Dockerfile is like a blueprint for building docker image.
Dockerfile
# pull the official base image
FROM python:3.8.3
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
COPY ./requirements.txt /usr/src/app
# Install the required libarires
RUN pip install -r requirements.txt --no-cache-dir
# copy project
COPY . /usr/src/app
# Expose the dev port for access
EXPOSE 8000
# Start the server
CMD ["python", "manage.py", "runserver", "8000"]Build the docker image
docker build -t app-name-here .[+] Building 2.3s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 532B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 51B 0.0s
...
...Run the dockerized image of the app
docker run -p 8000:8000 -t app-name-hereWatching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Django version 4.2.2, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.Navigate to your browser with http://localhost:8000 and see if you could see any logs on your app.
If get an error like
Bind for 0.0.0.0:8000 failed: port is already allocated.
This means there is another application running on that port, please close the application and run the command again.
Want to learn more about docker?
What is Docker and how to use it?
Docker is a software framework used for packaging, running, deploying and managing multiple apps on a server. Docker delivers OS-level virtualization to package a software called a container. It’s an open-source and the most popular containerization platform.