0

I have a project in Django, I want to configure my docker with an existing postgres database, but the tutorials I see are from a database without data.

who could give me a guide, please

It's my set up

Docker-compose:

services:
  web:
    build: .
    command: python3 manage.py migrate
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    volumes:
      - .:/code
    depends_on:
      - db
  
  db:
    image: postgres

Docker file

FROM python:3.9

ENV PYTHONUNBUFFERED 1
RUN mkdir /code

WORKDIR /code

COPY . /code

RUN pip install -r requirements.txt

File setting in django project

DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql',
       'NAME':'postgres',
       'USER': 'postgres',
       'HOTS': 'db',
       'PORT': 5432,
   }
}
  • My database backup file has the following format backup.sql – Dicson Ferney Quimbayo Jan 11 '22 at 01:03
  • You could create a new database and import the data, like described here: https://docs.djangoproject.com/en/4.0/ref/django-admin/#loaddata or just import that file in your postgres container – DrummerMann Jan 11 '22 at 01:04
  • The import is to put it inside the postgres container and create the database from my backup? – Dicson Ferney Quimbayo Jan 11 '22 at 01:09
  • 1
    Yes, it is explained here how to do it https://stackoverflow.com/questions/34688465/how-do-i-run-a-sql-file-of-inserts-through-docker-run – DrummerMann Jan 11 '22 at 01:10
  • thank you, your response was very helpful – Dicson Ferney Quimbayo Jan 11 '22 at 01:16
  • No problem, here to help! – DrummerMann Jan 11 '22 at 01:16
  • Or if the database is on another system (maybe it's something managed by your cloud provider) you can set `HOST` to point at that, the same way as if it wasn't in Docker; or if the database is outside Docker on the same host see [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach). You don't have to move everything into containers if it's not convenient. – David Maze Jan 11 '22 at 01:51
  • It is an idea that had not occurred to me I will try it, thanks – Dicson Ferney Quimbayo Jan 11 '22 at 09:01

0 Answers0