Skip to Content

Django Rest Project with Docker on Linux 20.04 Ubuntu

August 12, 2023 by
Django Rest Project with Docker on Linux 20.04 Ubuntu
COD Crafters, Usman Ghias
| No comments yet

A Comprehensive Guide to Setting Up Docker with Django Rest Framework on Linux Ubuntu 20.04 in VS Code

Meta Description (140 characters): Learn how to efficiently set up Docker with Django Rest Framework on Linux Ubuntu 20.04 using VS Code. A step-by-step guide for seamless integration.


          Our Social Media Application BestWordz

Docker and Django

Are you ready to supercharge your Django development process? Docker is the perfect tool to streamline your workflow and ensure consistent development environments. In this comprehensive guide, we'll walk you through the step-by-step process of setting up Docker with Django Rest Framework on a Linux Ubuntu 20.04 system using the powerful VS Code editor.

Step 1: Installing Docker

Begin your journey by installing Docker on your Linux Ubuntu 20.04 system. Open your terminal and follow these commands:

Update package index
sudo apt update

# Install required packages
sudo apt install apt-transport-https ca-certificates curl software-properties-common

# Add Docker repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker engine
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

# Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker

# Verify Docker installation
sudo docker --version

Step 2: Setting Up Your Django Project

Navigate to your desired project directory and create a virtual environment:

# Create a virtual environment
python3 -m venv venv

# Activate the virtual environment
source venv/bin/activate

# Install Django and required packages
pip install django djangorestframework

Step 3: Dockerizing Your Django Application

Now, let's create Docker configurations for your Django project. In your project's root directory, create a Dockerfile with the following content:

# Use the official Python image
FROM python:3.9

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory
WORKDIR /app

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

# Copy project files
COPY . /app/

Step 4: Docker Compose for Seamless Integration

Docker Compose allows you to define and manage multi-container Docker applications. Create a docker-compose.yml file in your project's root directory:

version: "3.9"


services:
  app:
    build:
      context: .
      args:
        - DEV=true
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python https://manage.py/ wait_for_db &&
             python https://manage.py/ migrate &&
             python https://manage.py/ runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=devdb
      - DB_USER=devuser
      - DB_PASS=changeme
    depends_on:
               - db


  db:
    image: postgres:13-alpine
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=devdb
      - POSTGRES_USER=devuser
      - POSTGRES_PASSWORD=changeme

Step 5: Running Your Dockerized Django App

Execute the following commands to build and run your Dockerized Django application:

# Build the Docker images
sudo docker-compose build

# Start the application
sudo docker-compose up

FAQs

Q1: What is Docker? A1: Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers.

Q2: Why use Docker with Django? A2: Docker ensures consistent development and production environments, making collaboration and deployment more efficient.

Congratulations: 

Congratulations! You've successfully set up Docker with Django Rest Framework on Linux Ubuntu 20.04 using VS Code. This powerful combination will significantly enhance your development process by providing isolated environments and seamless deployment options. Embrace the world of Docker and take your Django projects to new heights!

In this guide, we covered the essential steps, from installing Docker on your Linux system to dockerizing your Django application and utilizing Docker Compose for managing multi-container setups. By following these steps, you've paved the way for smoother collaboration, consistent environments, and efficient deployment.

As you continue your Django development journey, remember that Docker's benefits extend beyond just the scope of this guide. You'll find that it simplifies scaling, testing, and maintaining your applications, allowing you to focus more on coding and less on configuration.

So, what are you waiting for? Dive into the world of Docker and elevate your Django projects to a new level of efficiency and professionalism. Happy coding!


Don't forget to write comment

Django Rest Project with Docker on Linux 20.04 Ubuntu
COD Crafters, Usman Ghias August 12, 2023
Share this post
Archive
Sign in to leave a comment