Jonas
Jonas

Reputation: 185

Cannot run laravel migration within docker compose

Sorry if this is obvious, I am quite new to docker...

I just want to connect a container with a laravel application to a postgres container

The Dockerfile of my laravel application is :

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y libpq-dev
RUN docker-php-ext-install pdo pdo_pgsql pgsql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /app
COPY . /app
RUN composer install

CMD php artisan serve --host=0.0.0.0 --port=8181

EXPOSE 8181

No issues with this container that runs well

My docker-compose.yml goes like this :

version: '3'
services:
    laravel:
        container_name: app
        build:
            context: .
            dockerfile: Dockerfile
        environment:
            - DB_CONNECTION=pgsql
            - DB_HOST=localhost
            - DB_PORT=5432
            - DB_DATABASE=postgres
            - DB_USERNAME=postgres
            - DB_PASSWORD=postgres
        volumes:
            - .:/var/www/app
            - ./vendor:/var/www/app/vendor
        ports:
            - '8181:8181'
        command: bash -c "php artisan migrate"
        depends_on:
            - db
        links:
            - db
    db:
        image: postgres
        container_name: postgres_database
        restart: always
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
            - POSTGRES_DB=postgres
        ports:
            - '5432:5432'
        volumes:
            - db:/var/lib/postgresql/data
volumes:
  db:
    driver: local

When I run docker-compose up I have an issue when running the migration command : I get a QueryException: could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')

If I remove the command: bash -c "php artisan migrate" line in the docker-compose file, everything works fine and I can connect to my database using a database client

Does somebody know what is happening ?

Thanks a lot !!!

Upvotes: 2

Views: 2691

Answers (3)

Sufyan Khalid
Sufyan Khalid

Reputation: 66

If the Migration is not Running Successfully after the Application build then probably Database is not created Since in .env file Postgresql Database and User credentials are need to be filled. Also please try to install the following Extenstion.

docker-php-ext-install pdo_pgsql pgsql

Upvotes: 2

jpso
jpso

Reputation: 21

As I would recurrently stumble upon these minor config problems as my project grew, I opted on installing Laradock to do the job for me. So I strongly recommend it.

https://laradock.io/getting-started/

Upvotes: 0

Martin Zeitler
Martin Zeitler

Reputation: 76579

File app/config/database.php might still reference the wrong one (default) MySQL driver.
I'd suggest to use sed, in order to edit the file, before attempting to run the migration.

Upvotes: 0

Related Questions