1

I am successfully able to connect the servicebot service to the postgresql running within the docker container but I want to connect the servicebot to the postgresql running in instance ie not inside docker container.

I have installed the postgresql successfully. I have set the environment variables related to postgrsql in the docker-compose.yml as bellow. How can I make the docker-compose.yml connect to the

docker-compose.yml

version: '2'
services:
  servicebot:
    image: servicebot/servicebot
    environment:
      POSTGRES_DB_PORT : "5432"
      POSTGRES_DB_HOST : "localhost"
      POSTGRES_DB_USER : "servicebot_user"
      POSTGRES_DB_PASSWORD : "servicebot_pass"
      POSTGRES_DB_NAME : "servicebot_user"
      PORT : "3000"
    volumes:
      - upload-data:/usr/src/app/uploads
      - environment-file:/usr/src/app/env
      - db-data:/var/lib/postgresql/data
#    links:
#      - db
    ports:
      - "80:3000"
      - "443:3001"
    command: ["sh", "-c", "node /usr/src/app/bin/wait-for-it.js db 5432  && npm run-script start"]

volumes:
  upload-data:
  environment-file:
  db-data:

Previously I had a service named db for postgresql and connected to it with links as you can see, I have commented that out now.

I am very new to postgresql and not able to figure out the right way. I have tried few ways but nothing came to my success.

Tried:

  • Adding extra_hosts to the ip if the instance

  • Adding host.docker.internal instead of localhost

Error Logs:

On docker logs servicename It does not show anything. The service stops after 29 30 seconds.

2 Answers 2

5

Your problem is POSTGRES_DB_HOST pointing to "localhost", as "localhost" will be the container running your current service.

If you want to connect to a postgre running in your host (localhost) I think you can use this special value host.docker.internal.

Sign up to request clarification or add additional context in comments.

7 Comments

so you mean i need to replace localhost with host.docker.internal
Yes you have to replace localhost with a valid value, that I think it's host.docker.internal for your purpouses.
the change is not working, same issue the container shuts down.
It may depend on your docker version. Have a look to this question's answers stackoverflow.com/questions/28056522/…
its exactly same for my docker version as you recommended but I can see people recommending to run it as --net=host so I tried adding the network_mode: "host" to my docker-compose.yml but same not issue.
|
2

Just a heads up that if you're running Docker on a MAC (macOS), you need to use: docker.for.mac.host.internal instead.

Comments

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.