2

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
8
  • seems like pg and your app are not on the same network. can you post your compose file? Commented Jan 17, 2018 at 14:36
  • could you clarify what "it" refers to in your statement: When I try to run it for a test locally or on the VPS trough docker cloud? Commented Jan 17, 2018 at 15:06
  • After the build I am pushing it to docker cloud and then to the VPS. The app runs and when I enter the url in browser I get the error message. Same scenario on my local machine since it's docker right Commented Jan 17, 2018 at 15:10
  • which container are you running with the docker run -p 4000:8000 [name]? what is [name] here? Commented Jan 17, 2018 at 15:15
  • Just a placeholder for the question. I will edit the question. Commented Jan 17, 2018 at 15:18

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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:

7 Comments

I am getting ERROR: In file './docker-compose.yml', service 'networks' must be a mapping not a string. However i am looking into networks now :)
oh, i'm sorry, forgot to put the colon at the end of the network name. fixed now.
with the suggested changes concerning network, what is the output of docker-compose build && docker-compose up?
docker-compose up works fine. docker build -t mystore . is OK docker run -p 4000:8000 mystore is OK. But when I try to reach 0.0.0.0:4000 I am still getting "could not translate host name "db" to address: Name or service not known"
I can make the docker cloud repo public for you to try?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.