Skip to content

This repo is for practicing run django service with PostgreSQL in docker container

Notifications You must be signed in to change notification settings

PolunLin/Docker-with-Django-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker with Django example

| This repo is for practicing run django service with PostgreSQL in docker container

Table of contents

How to Create

1. Create a docker file

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

2. Create requirements.txt file

Django>=2.0,<3.0
psycopg2>=2.7,<3.0

3. create docker-compose.yml

version: "3"
services:

  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: 'postgres'

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"

Run the application

docker-compose run web django-admin startproject django_example .

And we will create

    Django with docker
    │  docker-compose.yml
    │  Dockerfile
    │  manage.py
    │  READ.md
    │  requirements.txt
    │
    └─django_example
        │  settings.py
        │  urls.py
        │  wsgi.py
        │  __init__.py

If your OS is linux, modify the file permission

$ sudo chown -R $USER:$USER .

Set DB connection information

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
        'PASSWORD': 'postgres',
    }
}

Run docker-compose up

docker-compose up
## docker-compose run web python manage.py syncdb

Reference

https://yeasy.gitbook.io/docker_practice/compose/django

About

This repo is for practicing run django service with PostgreSQL in docker container

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published