Henry Lynx
Henry Lynx

Reputation: 1129

Cannot connect to postgres db using docker build

I am trying to build an image and deploy it to a VPS.

I am running the app successfully with

docker-compose up

Then I build it with

docker build -t mystore .

When I try to run it for a test locally or on the VPS trough docker cloud:

docker run -p 4000:8000 mystore 

The container works fine, but when I hit http://0.0.0.0:4000/

I am getting:

OperationalError at / could not translate host name "db" to address: Name or service not known

I have changed the postgresql.conf listen_addresses to "*", nothing changes. The posgresql logs are empty. I am running MacOS.

Here is my DATABASE config:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': '5432',
    }
}

This is the Dockerfile

FROM python:3.5
ENV PYTHONUNBUFFERED 1

RUN \
  apt-get -y update && \
  apt-get install -y gettext && \
  apt-get clean

ADD requirements.txt /app/
RUN pip install -r /app/requirements.txt

ADD . /app
WORKDIR /app

EXPOSE 8000
ENV PORT 8000

CMD ["uwsgi", "/app/saleor/wsgi/uwsgi.ini"]

This is the docker-compose.yml file:

version: '2'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
    ports:
      - '5432:5432'
  redis:
    image: redis
    ports:
      - '6379:6379'
  celery:
    build:
      context: .
      dockerfile: Dockerfile
    env_file: common.env
    command: celery -A saleor worker --app=saleor.celeryconf:app --loglevel=info
    volumes:
      - .:/app:Z
    links:
      - redis
    depends_on:
      - redis
  search:
    image: elasticsearch:5.4.3
    mem_limit: 512m
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - '127.0.0.1:9200:9200'
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    env_file: common.env
    depends_on:
      - db
      - redis
      - search
    ports:
      - '8000:8000'
    volumes:
      - .:/app:Z
  makemigrations:
    build: .
    command: python manage.py makemigrations --noinput
    volumes:
      - .:/app:Z
  migration:
    build: .
    command: python manage.py migrate --noinput
    volumes:
      - .:/app:Z

Upvotes: 2

Views: 1761

Answers (3)

lvthillo
lvthillo

Reputation: 30811

Check the available networks. There are 3 by default:

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
db07e84f27a1        bridge              bridge              local
6a1bf8c2d8e2        host                host                local
d8c3c61003f1        none                null                local

I've a simplified setup of your docker compose. Only postgres:

version: '2'
services:
  postgres:
    image: postgres
    name: postgres
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
    ports:
      - '5432:5432'
    networks:
      random:

  networks:
    random:

I gave the postgres container the name postgres and called the service postgres, I created a network called 'random' (last commands), and I've added the service postgres to the network random. If you don't specify a network you will see that docker-compose creates its a selfnamed network. After starting docker-compose, you will have 4 networks. A new bridge network called random.

Check in which network your docker compose environment is created by inspecting for example your postgres container: Mine is created in the network 'random':

$ docker inspect postgres

It's in the network 'random'.

 "Networks": {
                "random": {..

Now start your mystore container in the same network:

$ docker run -p 4000:8000 --network=random mystore 

You can check again with docker inspect. To be sure you can exec inside your mystore container and try to ping postgres. They are deployed inside the same network so this should be possible and your container should be able to translate the name postgres to an address.

Upvotes: 1

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15400

You forgot to add links to your web image

web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    env_file: common.env
    depends_on:
      - db
      - redis
      - search
    links: # <- here
      - db
      - redis
      - search
    ports:
      - '8000:8000'
    volumes:
      - .:/app:Z

Upvotes: 1

saj
saj

Reputation: 681

in your docker-compose.yml, add a network and add your containers to it like so:

to each container definition add:

networks:
    - mynetwork

and then, at the end of the file, add:

networks:
    mynetwork:

Upvotes: 0

Related Questions