0

My project is a basic API using Django and PostgreSQL. I've built Docker images before but I can not find any information on this, all I see is how to download an image with PostgreSQL but not how to build an image of my own project that uses a database. I previously tried to build an image but I got an error:

Is the server running on that host and accepting TCP/IP connections?
2023-07-18 18:22:33 connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address

What kind of configuring do I need to build this image? A resource with examples would be helpful as I can't seem to find anything.

Here is my Dockerfile:

FROM python:3

ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

ENV PORT=8000

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Here is my docker-compose.yml file, this is the same one that worked on a previous project but I assume I need to add PostgreSQL to it but I am unsure what specifically needs to be added:

services:
  web: 
    build:
      context: app
      target: builder
    ports: 
      - '8000:8000'
alecnazzy
  • 43
  • 3
  • There are many blog articles already online showing how to do this. You can find them by googling "django postgres docker" or something similar. The TLDR is that you need a separate container that runs an image with postgres. That container will have a volume for the database files. – Code-Apprentice Jul 18 '23 at 22:51
  • Here is one answer on stackoverflow. [Linking my Django docker container to a postgres container](https://stackoverflow.com/questions/58522952/linking-my-django-docker-container-to-a-postgres-container) – Code-Apprentice Jul 18 '23 at 22:52

1 Answers1

0

You have to create it and expose the port. Something like this:

  db:
image: postgres:13
environment:
  - POSTGRES_DB=db
  - POSTGRES_USER=postgres
  - POSTGRES_PASSWORD=123456
volumes:
  - ~/.local_user/pg_data:/var/lib/postgresql/data
ports:
  - "5432:5432"