How to connect to host of docker container Laravel? - laravel

My service is using RabbitMQ. To work with it, I installed this package https://github.com/vyuldashev/laravel-queue-rabbitmq .
To work with this package, need to configure the config file
'hosts' => [
[
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
'port' => env('RABBITMQ_PORT', 5672),
'user' => env('RABBITMQ_USER', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', '/'),
],
],
As you can see, the ip is specified as 127.0.0.1, but my docker container has a dynamic ip, so it is updated every time and I have to manually fix the config
This is my docker-compose file
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- postgres
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- postgres
#Redis
redis:
image: 'redis:alpine'
ports:
- "6379:6379"
#PostgreSQL
postgres:
container_name: postgres_container
image: postgres
hostname: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- postgres
restart: unless-stopped
depends_on:
- postgres
#RabbitMQ
rabbit:
image: "rabbitmq:3-management"
hostname: "rabbit"
environment:
RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "rabbitmq"
RABBITMQ_DEFAULT_VHOST: "/"
ports:
- "15672:15672"
- "5672:5672"
labels:
NAME: "rabbitmq"
networks:
- postgres
#ElasticSearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
ports:
- "9200:9200"
- "9300:9300"
networks:
- postgres
#Docker Networks
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
How can I tell the rabbitmq container to connect to the external ip of the app container?

Sorry about the confusion, I somehow mixed up Redis and RabbitMQ. So here is a probably better matching answer:
You can connect to a service on your host from the Docker container even with a changing IP - there is script and a nice answer in this SO post.
Some excerpt:
If you are on Mac or Windows you can set
RABBITMQ_HOST=host.docker.internal
In general more easy I would consider a separate container running RabbitMQ and then using rabbitmq as hostname to connect to.
services:
# ... other services
rabbitmq:
image: rabbitmq:alpine
environment:
RABBITMQ_DEFAULT_USER: "defaultuser"
RABBITMQ_DEFAULT_PASS: "defaultpass"
ports:
- 5672:5672

Related

Laravel in docker - browserSync setup problems

I'm struggling for a few hours now and cannot proprly set webpack-mix browserSync functionality for docker development enviroment.
I'd be glad to get some help!
Thanks in advace.
My docker-compose.yml:
version: '3.7'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: vokl
container_name: vokl-app
ports:
- 80:80
- 3000:3000
- 3001:3001
volumes:
- ./:/var/www/html
networks:
- vokl
depends_on:
- mysql
mysql:
image: 'mariadb:latest'
container_name: vokl-db
restart: unless-stopped
ports:
- 3306:3306
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- ./.docker/dbdata:/var/lib/mysql
networks:
- vokl
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: vokl-phpmyadmin
environment:
- PMA_HOST=mysql
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=root
- MYSQL_PASSWORD=root
depends_on:
- mysql
ports:
- 8765:80
networks:
- vokl
networks:
vokl:
driver: bridge
webpack-mix:
mix.browserSync({
host: 'vokl-app.test',
proxy: 'app',
notify: false,
open: 'external'
});

Can Docker containers can be joined under one accessible hostname?

I started to develop with Laravel and since version 8, the Laravel development environment is shipped with docker and a tool named Sail (docker-compose).
When I configure my .env file I need to SSH the MySQL container and get its hostname, and then SSH the Mailhog container and do the same.
Besides the above when I want to see the database from a tool like TablePlus, I couldn’t make it access the MySQL container by hostname.
I did some searches and found that I can add the hostname directive, but I'm not sure if it's not disturbing the internal container's communication.
Can I use the hostname directive safety in my case? is there a way to join the containers under one hostname and make it accessible from my host?
I'm running Docker Desktop for a MAC - v20.10.2
The docker-compose for reference
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
# - pgsql
- redis
# - selenium
# selenium:
# image: 'selenium/standalone-chrome'
# volumes:
# - '/dev/shm:/dev/shm'
# networks:
# - sail
mysql:
hostname: 'beatsy-db'
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
# pgsql:
# image: postgres:13
# ports:
# - '${FORWARD_DB_PORT:-5432}:5432'
# environment:
# PGPASSWORD: '${DB_PASSWORD:-secret}'
# POSTGRES_DB: '${DB_DATABASE}'
# POSTGRES_USER: '${DB_USERNAME}'
# POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
# volumes:
# - 'sailpostgresql:/var/lib/postgresql/data'
# networks:
# - sail
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
# memcached:
# image: 'memcached:alpine'
# ports:
# - '11211:11211'
# networks:
# - sail
mailhog:
hostname: 'beatsy-mail'
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
# sailpostgresql:
# driver: local
sailredis:
driver: local
You have several services in your docker-compose.yml:
laravel.test
mysql
redis
and so on. Each of these is a resolavble name for containers in the same network. So instead of using mysql container hostname use its service name (just mysql in your case). You shouldn't use container hostnames because they're randomly generated.

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

Docker RabbitMQ and Spring doesn't create password with new user

I'm trying to deploy a spring app with rabbitmq. I get a Connection Refused Error When I use the default user:password (guest:guest) but when the user is created it makes it without password
Created new connection: rabbitConnectionFactory#60d8c0dc:5/SimpleConnection#358e0edb [delegate=amqp://guest#192.168.64.2:5672/, localPort= 53472]
Docker-compose.yml
version: '3'
services:
rabbitmq:
image: rabbitmq:management
environment:
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
ports:
- "5672:5672" #JMS Port
- "15672:15672" #Management Port - default user:pass = guest:guest
networks:
- rabbit_mq
db:
image: mysql:5.7.22
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "hospital"
MYSQL_PASSWORD: "root"
ports:
- "3306:3306"
networks:
- mysql_bridge
restart: always
springboot-docker-compose-app-container:
image: app-image
build:
context: ./
dockerfile: Dockerfile
environment: # Pass environment variables to the service
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/hospital?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
SPRING_RABBITMQ_HOST: rabbitmq
depends_on:
- rabbitmq
- db
volumes:
- /data/VerzorgerSOAP
ports:
- "8080:8080"
networks:
- mysql_bridge
- rabbit_mq
networks:
mysql_bridge:
rabbit_mq:
Spring Boot provides default values for some application properties:
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
In this case, it is only a coincidence.
Change these properties to something else and you will see that the auth will fail.

Docker Redis unable to connect with laravel and predis

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.

Resources