1

Not sure if my title is accurate, but here's my issue. I am running a basic laravel site on Docker and cannot get the site itself to connect to the PostgreSQL service. I will post my docker-compose.yml below. When i run php artisan migrate i get no errors and it all works. I can even use my Postico PostgreSQL client to connect to the DB and run queries. But, when i try and connect to the DB from the site, it errors out saying this:

SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5433?

Here are my PostgreSQL client settings (which DO work):

Host: 127.0.0.1
Port: 5433
User: homestead
Password: homestead
Database: homestead

I have been messing around with different settings and things so here is my docker-compose.yml, although i'm sure there are things i don't need in there:

version: '2'

services:
  php:
    image: jguyomard/laravel-php:7.2
    build:
      context: .
      dockerfile: infrastructure/php/Dockerfile
    volumes:
      - ./:/var/www/
      - $HOME/.composer/:$HOME/.composer/
    networks:
      - default
    links:
      - postgresql
      - redis

  nginx:
    image: jguyomard/laravel-nginx:1.13
    build:
      context: .
      dockerfile: infrastructure/nginx/Dockerfile
    ports:
      - 81:80
    networks:
      - default
    links:
      - postgresql
      - redis

  postgresql:
    image: postgres:9.6-alpine
    volumes:
      - pgsqldata:/var/lib/postgresql/data
    environment:
      - "POSTGRES_DB=homestead"
      - "POSTGRES_USER=homestead"
      - "POSTGRES_PASSWORD=homestead"
    ports:
      - "5433:5432"
    networks:
      - default

  redis:
    image: redis:4.0-alpine
    command: redis-server --appendonly yes
    ports:
      - "6379:6379"
    networks:
      - default

#  elastic:
#    image: elasticsearch:5.5-alpine
#    ports:
#        - "9200:9200"

volumes:
  pgsqldata:

networks:
  default:

Any thoughts on why the site can't connect to the DB?

My docker network ls output:

NETWORK ID      NAME               DRIVER        SCOPE
2bf85424f466    bridge             bridge        local
c29d413f768e    host               host          local
0bdf9db30cd8    none               null          local
f3d9cb028ae3    my-app_default     bridge        local
ryanpitts1
  • 862
  • 1
  • 16
  • 33
  • Possible duplicate of [Changing a postgres containers server port in Docker Compose](https://stackoverflow.com/questions/37775702/changing-a-postgres-containers-server-port-in-docker-compose) – Martin Zeitler Jan 01 '19 at 17:20
  • I believe my issue is a separate one, as i am already using the SO suggested answer of `ports: - "5433:5432"` – ryanpitts1 Jan 01 '19 at 17:50
  • From where did you use Postico PostgreSQL client to connect to db? Inside the same PostgreSQL container or host machine? – Hansika Weerasena Jan 01 '19 at 19:31
  • @ryanpitts1 you probably don't... because the connection parameters don't match. it would have to listen to `:5432` and not to `:5433`. else it maps a port, which is not listening. – Martin Zeitler Jan 01 '19 at 21:50

1 Answers1

2

The error message ask Is the server running on host "127.0.0.1" but in your case PostgreSQL is running on a different docker container which is not 127.0.0.1 reference to php app so, change the server host to postgresql inside your php application.

And for the modified error, it is because that you have used port 5433 inside the php application which is the port of host machine which is for use outside the docker container (for host machine, that's why your Postico PostgreSQL client worked). But the port you have to use inside the docker network is 5432 change the server port to 5432 inside your php application.

And you have made the compose file complex by defining network in each host as default network. (You can follow this link for more details) If you don't have a requirement for that you don't need to do that as docker-compose will deploy all containers in single network.

And you don't need to use links they are deprecated. When multiple containers are in one docker-compose.yml file they are automatically deployed in a same single network.

So this simplified compose file will be recommended.

version: '2'

services:
  php:
    image: jguyomard/laravel-php:7.2
    build:
      context: .
      dockerfile: infrastructure/php/Dockerfile
    volumes:
      - ./:/var/www/
      - $HOME/.composer/:$HOME/.composer/

  nginx:
    image: jguyomard/laravel-nginx:1.13
    build:
      context: .
      dockerfile: infrastructure/nginx/Dockerfile
    ports:
      - 81:80

  postgresql:
    image: postgres:9.6-alpine
    volumes:
      - pgsqldata:/var/lib/postgresql/data
    environment:
      - "POSTGRES_DB=homestead"
      - "POSTGRES_USER=homestead"
      - "POSTGRES_PASSWORD=homestead"
    ports:
      - "5433:5432"

  redis:
    image: redis:4.0-alpine
    command: redis-server --appendonly yes
    ports:
      - "6379:6379"

#  elastic:
#    image: elasticsearch:5.5-alpine
#    ports:
#        - "9200:9200"

volumes:
  pgsqldata:
Hansika Weerasena
  • 3,046
  • 1
  • 13
  • 22
  • Changing the `DB_HOST` environment variable to point to `postgresql` gives me this now: `SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "postgresql" (172.28.0.2) and accepting TCP/IP connections on port 5433?` Looks like a similar issue. Is my network setup right? You mentioned stuff with the ports, don't i already have that setup right in my `docker-compose.yml` file? – ryanpitts1 Jan 01 '19 at 19:40
  • Yes port mapping is okay. I think there is a issue with the docker network config. Can you do a docker network ls in your docker host and show the output. – Hansika Weerasena Jan 01 '19 at 19:53
  • Thanks! This all helped but the crux of the issue was the port i had set in PHP. – ryanpitts1 Jan 01 '19 at 22:46