3

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).
3
  • 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. Commented Jun 10, 2021 at 11:23
  • added information to the question itself Commented Jun 10, 2021 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. Commented Jun 10, 2021 at 11:45

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

7 Comments

Using the docker-compose.yml would avoid defining environment variables in different places.
@AymDev this is correct for the env variables like username, however I believe it's not possible for host
Why wouldn't it be possible ? Isn't it an environment variable like any other ?
Thanks, because of carelessness, I got to look for the problem in the wrong place)
@AymDev it's possible by adding a container_name actually
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.