Ask Your Question
1

How can I use Docker Compose to establish a setup for running Django in Docker?

asked 2023-07-05 00:25:02 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-05 00:39:01 +0000

devzero gravatar image

To establish a setup for running Django in Docker using Docker Compose, you can follow these steps:

  1. Create a new Django project.
$ django-admin startproject myproject
  1. Create a Dockerfile in the project's root directory.
FROM python:3.7

ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
  1. Create a docker-compose.yml file in the project's root directory.
version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: myprojectuser
      POSTGRES_PASSWORD: myprojectpassword
      POSTGRES_DB: myprojectdb
    volumes:
      - postgres_data:/var/lib/postgresql/data/

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

volumes:
  postgres_data:
  1. Create a requirements.txt file in the project's root directory.
Django==2.2
psycopg2==2.8.5
  1. Start the containers.
$ docker-compose up

Once the containers start, you can access the Django project at http://localhost:8000.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-05 00:25:02 +0000

Seen: 13 times

Last updated: Jul 05 '23