Docker Redis unable to connect with laravel and predis - laravel

I'm using docker with laravel project but im struggling to it to connect to the laravel container
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
redis:
image: redis:alpine
container_name: my-asset-management-redis
mysql:
image: mysql:8.0
container_name: my-asset-management-mysql
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=app
- MYSQL_USER=user
- MYSQL_PASSWORD=pass
ports:
- "8085:3306"
webserver:
image: nginx:alpine
container_name: my-asset-management-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8083:80"
php-fpm:
build: phpdocker/php-fpm
container_name: my-asset-management-php-fpm
working_dir: /application
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-extras.ini:/etc/php/7.4/fpm/conf.d/99-extras.ini
And I have this as my REDIS_HOST=my-asset-management-redis in my env
But i keep getting this: Predis\Connection\ConnectionException : php_network_getaddresses: getaddrinfo failed: No such host is known. [tcp://my-asset-management-redis:6379]
I have the redis password set as NULL for redis in env as well.

Related

Trying to run Vite inside Docker container

I'm new in Docker and JS technologies so don't judje too hard. I'm trying to run npm run dev from node.js container (called npm). It goes right and shows that css and scripts are available at localhost:3009, but they're not.
Am I right that the problem is with node.js container, which is trying to restart all the time and doesn't exposes any ports? If yes, how can I fix it? Thank you in advance.
Here is my docker-compose.yml and vite.config.js
version: '3'
networks:
laravel:
services:
site:
build:
context: ./dockerfiles
dockerfile: nginx.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: nginx
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- redis
- mysql
- mailhog
networks:
- laravel
mysql:
image: mariadb:10.6
container_name: mysql
restart: unless-stopped
tty: true
volumes:
- ./db-data:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: php
restart: unless-stopped
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
redis:
image: redis:alpine
container_name: redis
restart: unless-stopped
ports:
- 6379:6379
networks:
- laravel
composer:
build:
context: ./dockerfiles
dockerfile: composer.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
restart: always
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
- 3009:3009
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- laravel
vite.config.js
import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
server: {
port: 3009
},
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
});
You should use the "--host" key starting your Vite

How To Fix SQLSTATE[HY000] [2002] Connection refused Laravel Docker

When I try to run the command php artisan migrate, an error occurs:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = docker and table_name = migrations and table_type = 'BASE TABLE')
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql mysqli
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8088:80"
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8.0
container_name: db
ports:
- "3307:3306"
expose:
- "3306"
volumes:
- ./cache/mysql:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: docker
MYSQL_USER: root
MYSQL_PASSWORD: root
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
depends_on:
- nginx
- php
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
.env:
DB_CONNECTION=mysql
DB_HOST="db"
DB_PORT=3306
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=root
I try too:
DB_HOST=127.0.0.1
DB_HOST=localhost
thank you very good
In docker-compose.yml
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
php:
..
..
environment:
DB_HOST: mysql
DB_NAME: db_name
DB_USERNAME: root
DB_PASSWORD: root
networks:
- laravel
// map volume
volumes:
db_mysql:
In MySQL Dockerfile
FROM mysql:8.0
please chnage the ports in docker-compose.yml File , it works for me
ports:
- 8889:3306
----My-> docker-compose.yml File ---
version: '3.8'
services:
Web Server Service
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- app
- db
#Database Service
db:
image : mysql
container_name : mysql
restart: unless-stopped
tty: true
ports:
- 8889:3306
volumes:
# - ./mysql/data:/var/lib/mysql
environment:
# MYSQL_DATABASE: laravel_blogs
## MYSQL_ROOT_PASSWORD: password
# MYSQL_ROOT_HOST: localhost
# SERVICE_NAME: mysql
# SERVICE_TAGS: dev
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ~/mysql:/var/lib/mysql
app:
container_name: app
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
working_dir: /var/www
phpMyAdmin:
# phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
- "3400:80"
depends_on:
- db
links:
- db
#pgadmin:
#image: dpage/pgadmin4
#container_name: pgAdmin
#ports:
# - "5050:80"
#depends_on:
# - db
#environment:
# PGADMIN_DEFAULT_EMAIL: hanieasemi76#gmail.com
PGADMIN_DEFAULT_PASSWORD: password

Laravel with docker on RPI - Failed to open stream: Permission denied

I am testing deployment with docker on RPI but getting "Failed to open stream: Permission denied"
I tried few solutions but none of them worked for me
Permission Denied Error using Laravel & Docker
Laravel & Docker: The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
my Dockerfile:
FROM php:8.0-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN chown -R www-data:www-data /var/www
RUN chmod -R 755 .
and docker-compose.yml
version: "3.7"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
restart: unless-stopped
container_name: nginx
ports:
- "80:80"
volumes:
- ./:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mariadb
networks:
- laravel
mariadb:
image: yobasystems/alpine-mariadb:latest
restart: unless-stopped
container_name: mariadb
tty: true
ports:
- "4306:3306"
volumes:
- ./mariadb:/var/lib/mariadb
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: laravel_dev
MYSQL_USER: laravel_dev
MYSQL_PASSWORD: laravel_dev
SERVICE_TAGS: dev
SERVICE_NAME: mariadb
networks:
- laravel
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
ports:
- 4380:80
environment:
PMA_ARBITRARY: 1
PMA_PORT: 3306
PMA_HOST: mariadb
PMA_USER: laravel_dev
PMA_PASSWORD: laravel_dev
depends_on:
- mariadb
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./:/var/www/html
working_dir: /var/www/html
networks:
- laravel
npm:
image: node:current-alpine3.14
container_name: npm
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: .
dockerfile: Dockerfile
container_name: artisan
volumes:
- ./:/var/www/html
depends_on:
- mariadb
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
Only thing that worked was chmod 777 on project root folder but that is not the solution i would be comfortable with using.
So i found a solution on reddit
https://www.reddit.com/r/docker/comments/hjsipd/permission_denied_with_volumes/fwoixqe/
I had to sign project folder to container user, which i found with
less /etc/passwd. In this case name of user was www-data with UID 82. And command chown -R 82:82 .

Problems dockerizing Laravel application - database hosts array is empty

I'm trying to dockerize existing Laravel + Vue.js application, however, when trying to access database getting an error:
Database hosts array is empty.
It looks like that connection fails from PHP to MYSQL (but MySQL image is running and I can connect to it from terminal). Not sure how to proceed with this. It doesn't look to me a misconfiguration. Is there a way to debug it?
Here is what I have:
docker-compose.yml:
version: '3'
networks:
backoffice:
services:
backoffice_nginx:
image: nginx:stable
container_name: backoffice_nginx
restart: unless-stopped
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_mysql:
image: mysql:8.0.13
container_name: backoffice_mysql
restart: unless-stopped
tty: true
ports:
- "33067:3306"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password']
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: dbuser
MYSQL_PASSWORD: mypass
MYSQL_ROOT_PASSWORD: myrootpass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql/db:/var/lib/mysql
- ./mysql/conf.d:/etc/mysql/conf.d
networks:
- backoffice
backoffice_php:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_php
restart: unless-stopped
volumes:
- ./src:/var/www/html
networks:
- backoffice
backoffice_composer:
image: composer:latest
container_name: backoffice_composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- backoffice
php.dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
RUN docker-php-ext-install bcmath pdo pdo_mysql
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
DB configs part in the .env:
DB_CONNECTION=mysql
DB_HOST=backoffice_mysql
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=myrootpass
Here are running containers:
Any ideas?
Thanks.
You are not sharing your .env file with your container. Map your whole . Volume to the container instead of only your ./src directory.

Why docker sync files with map folder extremely slow? (Ubuntu)

On my local machine (Ubuntu 18.04, 8GB RAM, i5, HDD) I have two docker-compose files with my laravel project
docker-compose.yml
version: '3.7'
networks:
backend-network:
driver: bridge
frontend-network:
driver: bridge
services:
&app-service app: &app-service-template
container_name: k4fntr_app
build:
context: ./docker/php-fpm
args:
UID: ${UID?Use your user ID}
GID: ${GID?Use your group ID}
USER: ${USER?Use your user name}
user: "${UID}:${GID}"
hostname: *app-service
volumes:
- /etc/passwd/:/etc/passwd:ro
- /etc/group/:/etc/group:ro
- ./:/var/www/k4fntr
environment:
APP_ENV: "${APP_ENV}"
CONTAINER_ROLE: app
FPM_PORT: &php-fpm-port 9000
FPM_USER: "${UID:-1000}"
FPM_GROUP: "${GID:-1000}"
networks:
- backend-network
&queue-service queue:
<<: *app-service-template
container_name: k4fntr_queue
restart: always
hostname: *queue-service
depends_on:
- app
environment:
CONTAINER_ROLE: queue
&schedule-service schedule:
<<: *app-service-template
container_name: k4fntr_schedule
restart: always
hostname: *schedule-service
depends_on:
- app
environment:
CONTAINER_ROLE: scheduler
&sportlevel-listener sportlevel_listener:
<<: *app-service-template
container_name: k4fntr_sl_listener
restart: always
hostname: *sportlevel-listener
ports:
- "${SPORTLEVEL_LISTEN_PORT}:${SPORTLEVEL_LISTEN_PORT}"
depends_on:
- app
environment:
CONTAINER_ROLE: sl_listener
&php-fpm-service php-fpm:
<<: *app-service-template
container_name: k4fntr_php-fpm
user: 'root:root'
restart: always
hostname: *php-fpm-service
ports: [*php-fpm-port]
entrypoint: /fpm-entrypoint.sh
command: php-fpm --nodaemonize
networks:
- backend-network
- frontend-network
echo-server:
container_name: k4fntr_echo
image: oanhnn/laravel-echo-server
volumes:
- ./:/app
environment:
GENERATE_CONFIG: "false"
depends_on:
- app
ports:
- "6001:6001"
networks:
- backend-network
- frontend-network
redis:
container_name: k4fntr_redis
image: redis
restart: always
command: redis-server
volumes:
- ./docker/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./docker/redis/redis-data:/data:rw
ports:
- "16379:6379"
networks:
- backend-network
and docker-compose-dev.yml
version: '3.7'
volumes:
redis-data:
pg-data:
k4fntr_sync:
external: true
services:
&app-service app: &app-service-template
container_name: k4fntr_app
build:
context: ./docker/php-fpm
args:
UID: ${UID?Use your user ID}
GID: ${GID?Use your group ID}
USER: ${USER?Use your user name}
user: "${UID}:${GID}"
hostname: *app-service
volumes:
- /etc/passwd/:/etc/passwd:ro
- /etc/group/:/etc/group:ro
- k4fntr_sync:/var/www/k4fntr:nocopy
environment:
APP_ENV: "${APP_ENV}"
CONTAINER_ROLE: app
FPM_PORT: &php-fpm-port 9000
FPM_USER: "${UID:-1000}"
FPM_GROUP: "${GID:-1000}"
networks:
- backend-network
&php-fpm-service php-fpm:
<<: *app-service-template
container_name: k4fntr_php-fpm
user: 'root:root'
restart: always
hostname: *php-fpm-service
ports: [*php-fpm-port]
entrypoint: /fpm-entrypoint.sh
command: php-fpm --nodaemonize -d "opcache.enable=0" -d "display_startup_errors=On" -d "display_errors=On" -d "error_reporting=E_ALL"
networks:
- backend-network
- frontend-network
mail:
container_name: k4fntr_mail
image: mailhog/mailhog
ports:
- "1025:1025"
- "8025:8025"
networks:
- backend-network
nginx:
container_name: k4fntr_nginx
image: nginx
volumes:
- ./docker/nginx/config/default:/etc/nginx/conf.d
- k4fntr_sync:/var/www/k4fntr:nocopy
depends_on:
- *php-fpm-service
ports:
- "${NGINX_LISTEN_PORT}:80"
networks:
- frontend-network
database:
container_name: k4fntr_database
build: ./docker/postgres
restart: always
environment:
ENV: ${APP_ENV}
TESTING_DB: ${DB_DATABASE_TESTING}
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
ports:
- "15432:5432"
volumes:
- ./docker/postgres/prod/:/prod
- ./docker/postgres/pg-data:/var/lib/postgresql/data:rw
networks:
- backend-network
The problem is the fact that when I change some files in my project I have to wait a lot of time. From 15 to 40 seconds. It is impossible for local development. How can I solve this problem?
I learned some information with similar problems with other OS such as Mac or Windows, but I can't found the same problems with Linux.
The problem was that I thought that second file (docker-compose-dev.yml) overrided first file. I mean php-fpm section. If you look at docker-compose-dev you can see that there is the command
command: php-fpm --nodaemonize -d "opcache.enable=0" -d "display_startup_errors=On" -d "display_errors=On" -d "error_reporting=E_ALL"
Actually I used first file (what is very strongely, because I used the command
docker-compose -f docker-compose-dev.yml -f docker-compose.yml up
) and my opcache was cached. This was the main reason why I had to wait so long

Resources