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.

Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76

2 Answers2

3

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.

Ander2
  • 5,569
  • 2
  • 23
  • 42
  • so you mean i need to replace `localhost` with `host.docker.internal` – Tara Prasad Gurung Oct 29 '18 at 11:02
  • Yes you have to replace `localhost` with a valid value, that I think it's `host.docker.internal` for your purpouses. – Ander2 Oct 29 '18 at 11:03
  • the change is not working, same issue the container shuts down. – Tara Prasad Gurung Oct 29 '18 at 11:08
  • It may depend on your docker version. Have a look to this question's answers https://stackoverflow.com/questions/28056522/access-host-database-from-a-docker-container – Ander2 Oct 29 '18 at 11:10
  • 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. – Tara Prasad Gurung Oct 29 '18 at 11:22
  • Run `docker inspect ` and search for any error – Ander2 Oct 29 '18 at 11:51
  • i don't think docker inspect will help me search for any error. Is that even possible? I am not able to check the logs as its not showing on running the logs command @Ander2 – Tara Prasad Gurung Oct 30 '18 at 04:55
1

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

pamit
  • 326
  • 2
  • 10