Redis + Docker: PDOException: could not find driver - laravel

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!

Related

docker application not communicate with docker mysql container

I just encountered a problem. I am dockerizing a springboot application with MySQL as a database it is perfectly working in a local setup. But when I try to dockerize the application using docker-compose, MySQL container is working fine and is accessible in my workbench but my application is not able to access it throwing the communication link failure.
This is the compose file I am using:
version: "3.8"
services:
mysqldb:
image: mysql:5.7
restart:unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=baskartest
ports:
- 3307:3306
volumes:
- db:/var/lib/mysql
app:
depends_on:
- mysqldb
build: ./bezkoder-app
restart:on-failure
env_file: ./.env
ports:
- 8084:8080
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:3306/baskartest?useSSL=false",
"spring.datasource.username" : "root",
"spring.datasource.password" : "root",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
MySQL is working fine but my app in services is not able to communicate with it.
Here is what I see:
Any help would be appreciated!
I have done small changes in docker-compose.yml file, please use this one. It is working fine.
version: "3.8"
services:
mysqldb:
image: mysql:5.7
container_name: MYSQL_DB
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=baskartest
ports:
- 3307:3306
app:
build: ./bezkoder-app
restart: on-failure
ports:
- 8084:8080
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://MYSQL_DB:3306/baskartest
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
depends_on:
- mysqldb
In my short experience with Docker and containers communicating with eachother, I've found that localhost configuration URL's aren't working. Because one container can't communicatie with another container using just localhost:[PORT].
A lazy way for me to fix this is to work out a static IP address of the machine the containers are running on. Then use this (local) IP address instead of localhost to define the endpoint of the database.
In my situation with Redis I'm doing it like this:
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("192.168.1.201", 6379));
In your situation, you might want to use a Datasource URL like this:
- SPRING_DATASOURCE_URL=jdbc:mysql://[STATIC_IP_OF_MACHINE]:3306/baskartest
*i.e. ...192.168.1.100:3306/baskartest...*

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

Docker-compose for production running laravel with nginx on azure

I have an app that is working but I am getting problems to make it run on Azure.
I have the next docker-compose
version: "3.6"
services:
nginx:
image: nginx:alpine
volumes:
- ./:/var/www/
- ./setup/azure/nginx/conf.d/:/etc/nginx/template
environment:
PORT: ${PORT}
command: /bin/sh -c "envsubst '$${PORT}' < /etc/nginx/template/nginx.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
networks:
- mynet
depends_on:
- app
- worker
app:
image: myimage:latest
build:
context: .
dockerfile: ./setup/azure/Dockerfile
restart: unless-stopped
tty: true
expose:
- 9000
volumes:
- uploads:/var/www/simple/public/uploads
- logos:/var/www/simple/public/logos
networks:
- mynet
worker:
image: my_image:latest
command: bash -c "/usr/local/bin/php artisan queue:work --timeout=0"
depends_on:
- app
networks:
- mynet
volumes:
uploads:
logos:
networks:
mynet:
I am unsure if the volumes in nginx ok, I think that perhaps I should create a new Dockerfile to copy the files. However, this would increase a lot the size of the project.
When using App Services on azure the development is made assigning a randomly port, that's wgy i have the envsubst instruction in command. I appreciate any other suggestion to make it run this project on Azure
I'm assuming you're trying to persist the storage in your app to a volume. Check out this doc issue. Now I don't think you need
volumes:
- ./:/var/www/
- ./setup/azure/nginx/conf.d/:/etc/nginx/template
but for
volumes:
- uploads:/var/www/simple/public/uploads
- logos:/var/www/simple/public/logos
you can create a storage account, mount it to your linux app plan (it's not available for Windows app plans yet), and mount the relative path /var/www/simple/public/uploads to the file path of the storage container.

Unable to connect to pgsql in Laravel in Docker

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.

Resources