How do I connect to the host postgres service from the docker container under test

How do I connect to the host postgres service from the docker container under test?

I don’t really want to use Compose and duplicate the postgres service that is already running on the host as this is not how my container runs in production.

I think I really just need to know the host, username and password

According to the doc it doesn’t come installed, but can be set up with the following code:

dependencies:
  pre:
    - sudo service postgresql stop && sudo apt-get remove -y
      postgresql-9.4 && sudo apt-get update; sudo apt-get install -y
      postgresql-9.3 postgresql-contrib-9.3
    - sudo sed -i "s/\port = 5433/port = 5432/"
      /etc/postgresql/9.3/main/postgresql.conf
    - sudo cp /etc/postgresql/9.4/main/pg_hba.conf
      /etc/postgresql/9.3/main/pg_hba.conf
    - sudo service postgresql restart
    - sudo -u postgres createuser ubuntu -d --superuser
    - createdb circle_test
    - createdb ubuntu

However, I also found this:

Hope either of these help.

To make sure the instance of Postgres running in the build container is available inside the Docker containers, you will need to make it listen on the interface where the Docker containers will be connecting to it. The simplest way to achieve that is to make Postgres listen on 0.0.0.0, as described here:

test:
  pre:
    - sudo bash -c "echo \"listen_addresses = '*'\" >>
      /etc/postgresql/9.4/main/postgresql.conf"
    - sudo bash -c "echo \"host all all 0.0.0.0/0 trust\" >>
      /etc/postgresql/9.4/main/pg_hba.conf"
    - sudo /etc/init.d/postgresql restart
1 Like

This can be done pretty easily but there is no real documentation for it. All you have to do is run the containers with --net=host and then set app to use localhost to talk to services.

machine:
  services:
    - postgres

...

test:
  override:
    - docker run --net=host -p 8080:8080 --env POSTGRES_HOST=localhost my/container

Hope this helps!

3 Likes

Can you tell me how to use this command - docker run --net=host -p 8080:8080 --env POSTGRES_HOST=localhost my/container in circle 2.0?

Have you installed Docker, or selected an image with Docker already installed?

Have you pulled the required image from Docker Hub or another registry?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.