Unable to connect to pgsql in Laravel in Docker - laravel

I am developing a Web application using Laravel. I am an experienced Laravel developer. But, now I am trying to use Docker as my development environment. But I am so new to Docker. Now I am trying to connect to the Postgres database. I added the Postgres docker image in the docker-composer.yml as well. But when I run migration, I am getting error and it is not connecting to the database.
This is my docker-compose.xml file.
version: '2'
services:
web:
build:
context: ./
dockerfile: docker/web.docker
volumes:
- ./:/var/www
ports:
- "80:80"
- "443:443"
- "9000:9000"
links:
- app
app:
build:
context: ./
dockerfile: docker/app.docker
volumes:
- ./:/var/www
links:
- mysql
- redis
- beanstalk
- cache
environment:
- "DB_PORT=3306"
- "DB_HOST=mysql"
- "REDIS_PORT=6379"
- "REDIS_HOST=redis"
mysql:
image: mysql:5.7.18
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_DATABASE=docker"
ports:
- "3306:3306"
pgsql:
image: postgres:10.1
restart: always
environment:
- POSTGRES_DB=docker
- POSTGRES_USER=root
- POSTGRES_PASSWORD=secret
ports:
- 5431:5431
volumes:
- ./.docker/conf/postgres/:/docker-entrypoint-initdb.d/
redis:
image: redis:3.0
ports:
- "6379:6379"
beanstalk:
image: schickling/beanstalkd
ports:
- "11300:11300"
cache:
image: memcached:alpine
ports:
- "11211:11211"
I know I added the Mysql image as well. When I connect to the Mysql image, it was working. When I connect to the Postgres, I am getting error.
This is my database settings in env file for connecting to the Postgres.
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5431
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=secret
When I run migration in the terminal like this
docker-compose exec app php artisan migrate --seed
I got this error.
In Connection.php line 647:
could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)
In PDOConnection.php line 50:
could not find driver
In PDOConnection.php line 46:
could not find driver
What is wrong with my installation?

You didn't add your docker/app.docker details, but if this is PHP running, you need to make sure both php7-pgsql and php7-pdo are installed on this container. driver is usuallu JUST THAT phpX-mysql or phpX-pgsql. X is the optional version number, depending on which repo you're using for php. i.e with default ubuntu repo php-pgsql will be just fine, but with alpine images you have to use php7-pgsql.
with sury repo, you'll have to use specific version i.e php7.2-pgsql.

Related

Trouble with connecting PostgreSQL in docker-compose

I am trying to create a Laravel project on Docker with a PostgreSQL database locally. The structure of my project is described below:
nginx
conf.d
default.conf
php
Dockerfile
src
Laravel Project
docker-compose-yml
I can run the project successfully on the 8080 port, but I'm having trouble connecting to PostgreSQL.
Here is my docker-compose.yml:
version: '3.8'
networks:
laravel:
services:
nginx:
image: nginx:alpine
command: nginx -g "daemon off;"
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d
depends_on:
- php
networks:
- laravel
php:
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
db:
container_name: postgres
image: postgres
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=root
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_DB=expense
volumes:
- ./postgres:/var/lib/postgresql/data"
ports:
- "5446:5432"
restart: always
networks:
- laravel
In case you want to know, here is the Dockerfile for PHP:
FROM php:8.0.3-fpm
RUN docker-php-ext-install pdo
And also I want to mention the .env that is used by Laravel project:
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=postgres
with this configuration first I've run the docker-compose build and after running the docker-compose up I'm getting the below result for Postgres:
And when I want to reach 127.0.0.1:5432 there's nothing to show me. How can I solve this problem?
In your service db your POSTGRES_DB=expense set the default database name to expense (doc)
So your .env should use DB_DATABASE=expense and not postgres
And a last thing I am not sure about (because I can't find the documentation about it) is your DB_HOST: I don't know if you should use the container_name -> postgres or the service_name -> db, I think it's the service_name so db in your case

Laravel in Docker giving 'php_network_getaddresses: getaddrinfo failed: Name does not resolve'

I am having an issue with my Laravel in Docker.
Currently when I run php artisan migrate inside my container I get the error
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not
resolve (SQL: select * from information_schema.tables where table_schema =
app_database and table_name = migrations and table_type = 'BASE TABLE')
However, I am able to connect to the mysql using Sequel Pro and I am able to see the database created app_database
My docker_compose.yml is as below:
version: '3'
services:
nginx:
build:
context: .
dockerfile: Dockerfile_nginx
container_name: nginx_webserver
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
- laravel
database:
image: mysql:8.0
container_name: docker_database
environment:
- "MYSQL_DATABASE=app_database"
- "MYSQL_USER=app_db_user"
- "MYSQL_PASSWORD=app_db_password"
- "MYSQL_ROOT_PASSWORD=password"
volumes:
- ./mysql/db_data:/var/lib/mysql
ports:
- "3306:3306"
php:
build:
context: .
dockerfile: Dockerfile_php
container_name: my_app
volumes:
- ./src:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- "9000:9000"
networks:
- laravel
depends_on:
- database
networks:
laravel:
driver: bridge
The .env of my laravel app is
DB_CONNECTION=mysql
DB_HOST=docker_database
DB_PORT=3306
DB_DATABASE=app_database
DB_USERNAME=app_db_user
DB_PASSWORD=app_db_password
Can anybody share some insight?
I have tried everything online.
It looks like you need to add network to database container
networks:
- laravel
I got a similar error. In my case the mistake was using docker run which spun up a new container which was in it's own isolated network instead of using docker compose run which will run the command on one of the containers spun up after docker compose up which will have access to the other containers in the network

Laravel on Docker: [2002] Connection refused

I am trying to put a Laravel app up on Docker, but the database container is giving me trouble.
Specifically, I am getting this error when I try to open the app in the browser:
SQLSTATE[HY000] [2002] Connection refused
But, as far as I can see, all the user credentials are correct. Perhaps I am missing something? Please see below.
docker-compose.yml:
version: '3'
services:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./yoga/:/var/www
environment:
- "DB_PORT=33061"
- "DB_HOST=database"
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
ports:
- 8080:80
database:
image: mysql:5.7
container_name: database
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=yogadb"
- "MYSQL_USER=yogi"
- "MYSQL_PASSWORD=mypasshere"
- "MYSQL_ROOT_PASSWORD="
ports:
- "33061:3306"
volumes:
dbdata:
.env:
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=yogadb
DB_USERNAME=yogi
DB_PASSWORD=mypasshere
When I run the app outside docker, everything works correctly, I just replace DB_HOST=database with DB_HOST=127.0.0.1
What can I do to fix this?
docker ps output is:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2da7283f7a65 docker_app "docker-php-entrypoi…" 19 minutes ago Up 7 seconds 9000/tcp docker_app_1
4801fe3312c1 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 7 seconds 33060/tcp, 0.0.0.0:33061->3306/tcp 4801fe3312c1_database
ab370ae1d155 docker_web "nginx -g 'daemon of…" 25 hours ago Up 7 seconds 443/tcp, 0.0.0.0:8080->80/tcp docker_web_1
As #prd mentioned, you need to create bridged network for the containers [1], then add containers to the network [2].
Hostname of container is determined by name of the service in docker-compose.yml. In your case, if app service will connect to database service at hostname database & port 3306.
So docker-compose.yml becomes:
version: '3'
services:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./yoga/:/var/www
environment:
- "DB_PORT=3306" # Port of database container is 3306
- "DB_HOST=database"
networks:
- name_of_network # [2] add container to network
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
ports:
- 8080:80
database: # Name of service, which determines hostname of container
image: mysql:5.7
container_name: database
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=yogadb"
- "MYSQL_USER=yogi"
- "MYSQL_PASSWORD=mypasshere"
- "MYSQL_ROOT_PASSWORD="
ports:
- "33061:3306"
networks:
- name_of_network # [2] add container to network
volumes:
dbdata:
networks:
name_of_network: # [1] create bridged network

How to run database in localhost using pgAdmin with docker instance?

This is my docker-compose, and a want to open database in localhost with pgAdmin (I'm using MacOS), someone can help-me?
version: '2'
services:
db:
image: mdillon/postgis:9.4
environment:
- POSTGRES_DB=mytestapp_development
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
dbtest:
image: mdillon/postgis:9.4
environment:
- POSTGRES_DB=mytestapp_test
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
web:
depends_on:
- db
- dbtest
build: .
command: bin/rails s --port 3000 --binding 0.0.0.0
volumes:
- .:/app
I don't know about Mac, but on linux, I need to edit the file config.inc.php, on /etc/phpmyadmin, then change the
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'your ip docker container';
Then restart phpmyadmin

Redis + Docker: PDOException: could not find driver

I'm new to using Redis. I'm running Laravel, MariaDB, and Redis in Docker. I can't seem to get redis to work properly. I get the following error in Laravel Horizon:
PDOException: could not find driver in /var/www/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:46
My guess is that the code is being executed inside the redis container, that has no access to the PHP container.
This is my docker-compose.yml:
# Web server
nginx:
image: nginx:latest
restart: always
links:
- socketio-server
ports:
- "3000:3001"
- "8081:80"
volumes:
- ./api:/var/www/api
- ./docker/nginx/conf.d/:/etc/nginx/conf.d
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
links:
- php
# PHP
php:
build: ./docker/php-fpm
volumes:
- ./api:/var/www/api
links:
- mariadb
# Redis
redis:
image: redis:latest
depends_on:
- php
expose:
- "6379"
# Database
mariadb:
image: mariadb:latest
restart: always
ports:
- "3306:3306"
volumes:
- ./database/mariadb/:/var/lib/mysql
# PHP workers
php-worker:
build:
context: ./docker/php-worker
args:
- PHP_VERSION=7.2
- INSTALL_PGSQL=false
volumes:
- ./:/var/www
- ./docker/php-worker/supervisor.d:/etc/supervisor.d
extra_hosts:
- "dockerhost:10.0.75.1"
links:
- redis
Anyone any ideas?
Your assumption that the containers don't have access to each other is correct.
Your PHP container executes the PHP code, so it must have access to the redis container and the mariadb container in order to use them. You do this by adding them to the links array. I see you have already done this for mariadb, but you should add redis as well.
# PHP
php:
build: ./docker/php-fpm
volumes:
- ./api:/var/www/api
links:
- mariadb
- redis
By adding redis to the links array, you can access it in your PHP container with the hostname redis.
I turned out to be a problem in the 'php-worker' container. I hadn't installed pdo_mysql here. Now everything works fine!

Resources