Anthony
Anthony

Reputation: 3218

Postgresql via Docker - postgres is not running automatically

The main problem is that i cannot run postgresql even on vm with the error:

root@a2c8a58d4e0e:/# psql -h localhost -U psqluser -W
Password for user psqluser:
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

For that purpose i run this commands inside VM:

pg_createcluster 9.6 main --start
/etc/init.d/postgresql start

And then it works properly on VM. But that's manually.

I configured everything by official docker repo docs.

This is my docker compose file:

version: "3.3"

services:
  postgresql:
    build:
      context: .
      dockerfile: postgresql
    container_name: Postgres
    restart: always
    ports:
    - "5432:5432"
    environment:
      POSTGRES_DB: 'psqldb'
      POSTGRES_USER: 'psqluser'
      POSTGRES_PASSWORD: 'temp123'
    volumes:
    - /home/VOLUMES/DB/Postgresql:/var/lib/postgresql

I did inheritance from original repo as i want to run postgresql service automatically. Otherwise it's not running.

postgresql file:

FROM postgres:9.6.11
RUN pg_createcluster 9.6 main --start
RUN /etc/init.d/postgresql start

It does not run Postgres as well. Only manually inside VM.

What's wrong?

Upvotes: 2

Views: 2911

Answers (1)

Simon Boudrias
Simon Boudrias

Reputation: 44589

Within docker compose, ports aren't exposed on the host by default. https://docs.docker.com/compose/compose-file/

ports is virtual within the docker compose network. If you want to expose them to the host machine, you can use the expose option instead of ports.

Alternatively, you can also run docker-compose run with the --service-ports flag which will automatically expose the ports to the host when running.

docker-compose run --service-ports postgresql (see doc)

Upvotes: 1

Related Questions