2

I created a docker container using the standard "image: postgres:13", but inside the container it doesn't start postgresql because there is no cluster. What could be the problem? Thx for answers!

My docker-compose:

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - pgsql
    pgsql:
        image: 'postgres:13'
        ports:
            - '${FORWARD_DB_PORT:-5432}:5432'
        environment:
            PGPASSWORD: '${DB_PASSWORD:-secret}'
            POSTGRES_DB: '${DB_DATABASE}'
            POSTGRES_USER: '${DB_USERNAME}'
            POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
        volumes:
            - 'sailpgsql:/var/lib/postgresql/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailpgsql:
        driver: local

and I get an error when trying to contact the container:

SQLSTATE[08006] [7] 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?

and inside the container, when I try to start or restart postgres, I get this message:

[warn] No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).
AymDev
  • 6,626
  • 4
  • 29
  • 52
Archi
  • 27
  • 1
  • 6
  • The standard image starts correctly. You should provide your Docker Compose file and the commands you ran which make you think it does not start. – AymDev Jun 10 '21 at 11:23
  • added information to the question itself – Archi Jun 10 '21 at 11:37
  • No need to restart postgres. How does your Laravel application gets the database address ? Shouldn't you set an environment variable to your laravel container ? Because it tries to access it from `localhost` instead of the postgres container. – AymDev Jun 10 '21 at 11:45

1 Answers1

1

You should not connect through localhost but by the container name as host name.

So change your .env to contain

DB_CONNECTION=[what the name is in the config array]
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=[whatever you want]
DB_PASSWORD=[whatever you want]
online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • Using the **docker-compose.yml** would avoid defining environment variables in different places. – AymDev Jun 10 '21 at 11:53
  • @AymDev this is correct for the env variables like username, however I believe it's not possible for host – online Thomas Jun 10 '21 at 11:53
  • Why wouldn't it be possible ? Isn't it an environment variable like any other ? – AymDev Jun 10 '21 at 12:03
  • Thanks, because of carelessness, I got to look for the problem in the wrong place) – Archi Jun 10 '21 at 12:08
  • @AymDev it's possible by adding a `container_name` actually – online Thomas Jun 10 '21 at 13:33
  • @onlineThomas if Laravel treats the host environment variable as any other (no reason it would not), you can set it from the **docker-compose.yml** file and use the Docker Compose service name as the host name (here it is `pgsql`). No need for `container_name` here. – AymDev Jun 10 '21 at 13:38
  • @AymDev Thanks, always nice to learn something – online Thomas Jun 14 '21 at 12:50