Skip to content

Instantly share code, notes, and snippets.

@kokovych
Last active February 20, 2022 22:20
Show Gist options
  • Save kokovych/94c54b59f6898ee71edd1c4dd2cae869 to your computer and use it in GitHub Desktop.
Save kokovych/94c54b59f6898ee71edd1c4dd2cae869 to your computer and use it in GitHub Desktop.
Only Django inside docker container and local database(postgres) is not in container

Create django project with localhost database

Acutally, this is example of my Dockerfile and docker-compose.yml. I worked on mac OS. I would like to run my django app inside docker container, but I didn't want to use container with postgres db inside docker. I have already installed and running postgres instance with all data I need. So django is in container, postgres is out.

  • My Dockerfile:
FROM python:3.8-alpine
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Nothing special here. Just create app, running requirements for environment.

  • My docker-compose.yml:
version: "3.8"

services:
  web:
    build: .
    command: >
      sh -c "python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8008:8000"
    environment:
      - DB_NAME=YOUR_DB_NAME_HERE
      - DB_USER=YOUR_DB_USER_HERE
      - DB_PASSWORD=YOUR_DB_PASSWORD_HERE
      - DB_HOST=docker.for.mac.localhost

For me all magic was in line with docker.for.mac.localhost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment