Search Posts

Install django+openlayer+postgres+postgis in docker

Below is a step-by-step guide to install and dockerize Django with OpenLayer, PostgreSQL, PostGIS, and the specified Python libraries on Ubuntu 22.04. This guide includes all the necessary Dockerfiles, scripts, and requirements files to help you set up the environment.

Step 1: Install Docker and Docker Compose

First, ensure that Docker and Docker Compose are installed on your Ubuntu machine:

sudo apt-get update
sudo apt-get install -y docker.io docker-compose

Step 2: Create Project Directory

Create a directory for your project:

mkdir django_openlayer_project
cd django_openlayer_project

Step 3: Create Dockerfile for Django Application

Create a Dockerfile in the project directory:

# Dockerfile

FROM python:3.9-slim

ENV PYTHONUNBUFFERED 1

# Install dependencies
RUN apt-get update && apt-get install -y \
    binutils \
    libproj-dev \
    gdal-bin \
    && rm -rf /var/lib/apt/lists/*

# Set work directory
WORKDIR /app

# Install Python dependencies
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy project
COPY . /app

Step 4: Create requirements.txt

Create a requirements.txt file with the required Python libraries:

# requirements.txt

Django
psycopg2-binary
django-simple-captcha
ghasedakpack
requests
numpy
networkx
pandas

Step 5: Create Docker Compose File

Create a docker-compose.yml file to define the services (Django, PostgreSQL, and PostGIS):

# docker-compose.yml

version: '3.9'

services:
  db:
    image: postgis/postgis
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_HOST: db
      POSTGRES_PORT: 5432

volumes:
  postgres_data:

Step 6: Create Django Settings

In your Django project, modify the settings for the PostgreSQL database. Update settings.py as follows:

# settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'db',
        'PORT': '5432',
    }
}

Step 7: Initialize Django Project (Optional)

If you haven't created a Django project yet, you can do so by running:

docker-compose run web django-admin startproject myproject .

Step 8: Run Docker Compose

To build and start the containers, run:

docker-compose up --build

This command will build the Docker image, start the PostgreSQL/PostGIS container, and start the Django application.

Step 9: Create a Script for Offline Mode Installation

To enable offline installation on another machine, follow these steps:

  1. Download all necessary Python packages:

    mkdir wheelhouse
    pip download -d wheelhouse -r requirements.txt
  2. Create a script to install everything offline:

    touch offline_install.sh
    chmod +x offline_install.sh

    Add the following content to offline_install.sh:

    #!/bin/bash
    
    # Install Docker
    sudo apt-get update
    sudo apt-get install -y docker.io docker-compose
    
    # Copy wheelhouse to target machine
    scp -r wheelhouse target_machine:/path/to/django_openlayer_project/
    
    # Build Docker image
    docker-compose build --no-cache
  3. Transfer Project to Another Machine:

    Copy the entire project directory (including the wheelhouse) to the target machine. You can use tools like scp or rsync for this purpose.

  4. Run the Script on the Target Machine:

    ./offline_install.sh