Creating a docker container for postgresql with laravel sail

Archi

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).
online Thomas

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]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why everytime I run Sail Up it is creating a new container in docker?

How to get laravel sail docker container running on a different computer?

Docker port binding for Redis with Laravel Sail

why is Laravel Sail docker not running on localhost

Docker is creating random container

Kubernetes not creating docker container

Connect from laravel app in docker container to Postgresql in host machine?

PostgreSQL docker container on Windows

PHPMyAdmin on Docker with Laravel Sail POST Content-Length exceeds the limit

Error while installing Laravel in docker #RUN groupadd --force -g sail

How to add phpmyadmin to laravel 8 sail docker-compose.yml

creating docker container to host website

Docker Container not creating tables in the database

Docker container called in Laravel

Error in creating a docker container from docker image

Connecting to Postgresql in a docker container with a GUI

Postgresql docker container connection failure

Cron in postgresql:alpine docker container

Connect to a PostgreSQL database on a Docker container

Slow postgresql startup in docker container

Installing PostgreSQL within a docker container

access logs on laravel sail

Docker postgresql container with external data container

Docker: Springboot container cannot connect to PostgreSql Container

Docker Compose : connecting python container with postgresql container

Error 'could not translate host name' running Laravel migration in Docker PostgreSQL container

How to connect Docker web app container to Docker PostgreSQL container?

Laravel CLI 'sail artisan' returns 'no such service' after updating 'service name' in docker-compose.yml

docker_ is being appended when creating container