PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] Cannot run laravel migration within docker compose

 February 12, 2022     docker, docker-compose, laravel, php, postgresql     No comments   

Issue

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 !!!


Solution

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


Answered By - Sufyan Khalid
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing