Skip to content

Instantly share code, notes, and snippets.

@ttonyh
Last active July 11, 2021 20:03
Show Gist options
  • Select an option

  • Save ttonyh/f3a929720eac8570afae1661b5ed8d69 to your computer and use it in GitHub Desktop.

Select an option

Save ttonyh/f3a929720eac8570afae1661b5ed8d69 to your computer and use it in GitHub Desktop.
Quick-start Django project using Docker
#!/usr/bin/env bash
# REF: https://docs.docker.com/samples/django/
DIR=$1
PROJ_NAME=$2
USER_ID="$(id -u)"
GROUP_ID="$(id -g)"
CURRENT_USER="$USER_ID:$GROUP_ID"
# Make initial dir
mkdir -p ./$DIR/data/db
# Create Dockerfile
tee -a ./$DIR/Dockerfile > /dev/null <<EOT
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
EOT
# Create requirements file
tee -a ./$DIR/requirements.txt > /dev/null <<EOT
Django>=3.0,<4.0
psycopg2-binary>=2.8
EOT
# Create Docker Compose file
tee -a ./$DIR/docker-compose.yml > /dev/null <<EOT
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
user: $CURRENT_USER
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
EOT
# Init Django project
(cd $DIR; docker-compose run web django-admin startproject $PROJ_NAME .)
# As root, reset permissions
sudo chown -R $USER:users ./$DIR/*
echo ""
echo "***"
echo "RUN: docker-compose up"
echo "***"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment