简体   繁体   中英

Linking my Django docker container to a postgres container

I create a django docker application image. in my django app, in settings.py DATABASE entry is:

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

well, at this point i create my docker image using docker build command; all done. Before running my django app docker image i run:

docker run -it postgres

image is downloaded and container start correctly

在此处输入图像描述

but when i run my django app

docker run -it cath2019/cathedral_studio:latest

but wher python manage.py runserver command into my Dockerfile start i get this error:

conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: could not translate host name "postgres" to address: Name does not resolve

here my app Dockerfile:

FROM python:3.6-alpine
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
#RUN apk update && apk add build-essential libssl-dev libffi-dev
RUN mkdir /Code
WORKDIR /Code
COPY ./requirements.txt .

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1

COPY . /Code/

ENTRYPOINT python /Code/core/manage.py runserver 0.0.0.0:8000

How can i connect into my django settings DATABASE to running postgres container?

So many thanks in advance

You can use two methods to solve this:

1. User defined networks

The default bridge network is present on all Docker hosts. If you do not specify a different network, new containers are automatically connected to the default bridge network.

Link

You can get IP address of containers using docker network inspect bridge . Then you can use the IP address from containers attribute.

This is not recommended as changes in container might change the ip address.

Docker does not support automatic service discovery on the default bridge network. If you want containers to be able to resolve IP addresses by container name, you should use user-defined networks instead. You can link two containers together using the legacy docker run --link option, but this is not recommended in most cases.

To Create a network run docker network create --driver bridge django_network .

Then attach both the containers to same network using docker run -it postgres --network=django_network and docker run -it cath2019/cathedral_studio:latest --network=django_network .

Now you can connect to postgres database from your django app using hostname.

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

}

2. Docker-compose

Have a look at official documentation Link to dockerize a django app.

version: '3'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Using only Docker

You would need to link the Django container using: --link postgres:postgres

Checkout this answer

Using Docker Compose

version: '3'

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

Source: https://docs.docker.com/compose/django/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM