RabbitMQ configuration problems - php

I am having a problem with how to use the following rabbit environment configuration.
rabbit_mq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
hostname: rabbitmq
ports:
- 5672:5672
- 15672:15672
volumes:
- ./docker/rabbitmq/data/:/var/lib/rabbitmq/
- ./docker/rabbitmq/log/:/var/log/rabbitmq
- ./docker/rabbitmq/conf/:/var/conf/rabbitmq
environment:
#(I comment this now but before I used this)
# RABBITMQ_HOST: rabbitmq
# RABBITMQ_PORT: 15672
# RABBITMQ_VHOST: /
RABBITMQ_DEFAULT_USER: guest
#(I also tried to use RABBITMQ_USER)
RABBITMQ_DEFAULT_PASS: guest
#(and I also tried to use RABBITMQ_PASS & RABBITMQ_PASSWORD)
I need to use environment configuration to connect to the server. There is my PHP code:
$connection = new AMQPStreamConnection($_ENV['RABBITMQ_HOST'], $_ENV['RABBITMQ_PORT'], $_ENV['RABBITMQ_DEFAULT_USER'], $_ENV['RABBITMQ_DEFAULT_PASS']);
I tried a lot of things, for example, I created a rabbitmq.env file, and write the config down there, but it's not working
Also, there is an error-
Fatal error: Uncaught PhpAmqpLib\Exception\AMQPIOException: stream_socket_client(): unable to connect to tcp://: (Failed to parse address ":") in /app/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:108
How to resolve this issue?

Okey, there is the answer: i had to write this config in php container, not in rabbitmq container, so there is the docker-compose code:
send:
build:
context: './docker'
dockerfile: Dockerfile
image: php:7.4.cli
container_name: send
ports:
- 8000:8000
volumes:
- ./:/app
environment:
RABBITMQ_HOST: '${RABBIT_HOST}'
RABBITMQ_PORT: '${RABBIT_PORT}'
RABBITMQ_USERNAME: '${RABBIT_USERNAME}'
RABBITMQ_PASSWORD: '${RABBIT_PASSWORD}'
depends_on:
- rabbit_mq
command: ["php", "./send.php"]
And also add .env file in the root directoria:
RABBIT_HOST=rabbitmq
RABBIT_PORT=5672
RABBIT_USERNAME=guest
RABBIT_PASSWORD=guest
My PHP code:
$connection = new AMQPStreamConnection($_ENV['RABBITMQ_HOST'], $_ENV['RABBITMQ_PORT'], $_ENV['RABBITMQ_USERNAME'], $_ENV['RABBITMQ_PASSWORD']);

Related

Mysql - Docker: Connection issue

My docker-compose.yml looks like this:
version: '3.3'
services:
frontend:
build: frontend
container_name: yii-frontend
ports:
- 20080:80
volumes:
# Re-use local composer cache via host-volume
- ~/.composer-docker/cache:/root/.composer/cache:delegated
# Mount source-code for development
- ./:/app
networks:
- my-marian-net
backend:
build: backend
container_name: yii-backend
ports:
- 21080:80
volumes:
# Re-use local composer cache via host-volume
- ~/.composer-docker/cache:/root/.composer/cache:delegated
# Mount source-code for development
- ./:/app
networks:
- my-marian-net
db:
image: mysql:8.0
container_name: mysql8
command: --user=root --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=verysecret
- MYSQL_DATABASE=yii2advanced
- MYSQL_USER=yii2advanced
- MYSQL_PASSWORD=secret
ports:
- 6033:3306
networks:
- my-marian-net
networks:
my-marian-net:
driver: bridge
I get an error message:
'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:
nodename nor servname provided, or not known'
Solutions I have tried:
Ping from backend and frontend container to mysql8: docker exec -ti yii-frontend ping mysql8 (It works!)
Manually connect each container to the same network (my-marian-net) docker network connect my-marian-net mysql8 (same for each container)
I have connected into the mysql container and test connection there, it works.
I have connected from containers to mysql8 container MySQL service and it works.
So far no luck getting connected. In my code, I am using "db" as hostname since I am using bridge mode.
After trying different solutions, I have isolated the issue to a connection from
outside docker, Any suggestion?
I am using macOS Mojave. Docker version 19.03
The issue was simpler than I thought, from outside container "db" service does not exist, so no possibility to get connected.
In the future when I run console commands from local, I will change host in database connection to use localhost instead of "db" or get connected to the container itself and run them from inside.
Thanks to #Smankusors for the help.

Can't connect to between mysql container and php in docker

Trying to connect to mysql 8 (mysql:latest) from a php:latest image (without apache or nginx) and I'm getting this annoying error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /src/conn.php:5
Stack trace:
#0 /src/conn.php(5): PDO->__construct('mysql:host=data...', 'root', '12345678')
#1 {main}
thrown in /src/conn.php on line 5
I've looked for +20 solutions here on stackoverflow but none of them was able fix my problem. I can connect without a problem using mysql workbench or DataGrid using the following credentials:
server: localhost user: root pass: 12345678
But any configuration I use on the php image I can't make them connect with each other.
My docker-compose file:
version: '3'
services:
composer:
container_name: composer
networks:
- backend
image: composer
volumes:
- .:/app
command: install
database:
container_name: mysql
networks:
- backend
image: mysql:latest
volumes:
- ./database/mysqldata:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 12345678
MYSQL_DATABASE: testdb
ports:
- "3306:3306"
php:
build:
context: .
container_name: php
networks:
- backend
image: php:apache
links:
- database
env_file:
- .env
volumes:
- .:/
command:
php /src/conn.php
depends_on:
- composer
- database
networks:
backend:
My conn.php:
<?php
$db = new PDO('mysql:host=database;dbname=testdb', 'root', '12345678');
What I did until now to try to fix the problem:
Change conn.php host to 127.0.0.1
Change conn.php host to localhost
Tried to use mysqli (got also connection refused)
Tried another php images (with apache, nginx etc)
Tried to use mysql as a host
PS: I'm using docker for mac
After 5 hours of research I really don't know what else to do and as I'm not a docker expert, would appreciate any help to point me the direction of the fix.
Thank you!
In your case, you can't connect to mysql via localhost or 127.0.0.1. If you connect like this. It call to localhost inside php container. But there is no mysql install in your php container.
You must connetc via container name. Or docker inspect you mysql container and get the container IP. For example with your docker-compose
database:
container_name: mysql
Now your mysql database host is mysql - container name instead localhost
Was able to find the solution. Turns out I need to wait for mysql service to be available before I start the php script. So I used the wait-for-it solution and did this on docker-compose:
command: >
bash -c "chmod +x /docker/./wait-for-it.sh
&& /docker/./wait-for-it.sh database:3306 -- echo 'database is up'
&& php phpscript.php"
Thank you for the help

Docker SQLSTATE[08006] for pgsql service only from php service

Not sure if my title is accurate, but here's my issue. I am running a basic laravel site on Docker and cannot get the site itself to connect to the PostgreSQL service. I will post my docker-compose.yml below. When i run php artisan migrate i get no errors and it all works. I can even use my Postico PostgreSQL client to connect to the DB and run queries. But, when i try and connect to the DB from the site, it errors out saying this:
SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5433?
Here are my PostgreSQL client settings (which DO work):
Host: 127.0.0.1
Port: 5433
User: homestead
Password: homestead
Database: homestead
I have been messing around with different settings and things so here is my docker-compose.yml, although i'm sure there are things i don't need in there:
version: '2'
services:
php:
image: jguyomard/laravel-php:7.2
build:
context: .
dockerfile: infrastructure/php/Dockerfile
volumes:
- ./:/var/www/
- $HOME/.composer/:$HOME/.composer/
networks:
- default
links:
- postgresql
- redis
nginx:
image: jguyomard/laravel-nginx:1.13
build:
context: .
dockerfile: infrastructure/nginx/Dockerfile
ports:
- 81:80
networks:
- default
links:
- postgresql
- redis
postgresql:
image: postgres:9.6-alpine
volumes:
- pgsqldata:/var/lib/postgresql/data
environment:
- "POSTGRES_DB=homestead"
- "POSTGRES_USER=homestead"
- "POSTGRES_PASSWORD=homestead"
ports:
- "5433:5432"
networks:
- default
redis:
image: redis:4.0-alpine
command: redis-server --appendonly yes
ports:
- "6379:6379"
networks:
- default
# elastic:
# image: elasticsearch:5.5-alpine
# ports:
# - "9200:9200"
volumes:
pgsqldata:
networks:
default:
Any thoughts on why the site can't connect to the DB?
My docker network ls output:
NETWORK ID NAME DRIVER SCOPE
2bf85424f466 bridge bridge local
c29d413f768e host host local
0bdf9db30cd8 none null local
f3d9cb028ae3 my-app_default bridge local
The error message ask Is the server running on host "127.0.0.1" but in your case PostgreSQL is running on a different docker container which is not 127.0.0.1 reference to php app so, change the server host to postgresql inside your php application.
And for the modified error, it is because that you have used port 5433 inside the php application which is the port of host machine which is for use outside the docker container (for host machine, that's why your Postico PostgreSQL client worked). But the port you have to use inside the docker network is 5432 change the server port to 5432 inside your php application.
And you have made the compose file complex by defining network in each host as default network. (You can follow this link for more details) If you don't have a requirement for that you don't need to do that as docker-compose will deploy all containers in single network.
And you don't need to use links they are deprecated. When multiple containers are in one docker-compose.yml file they are automatically deployed in a same single network.
So this simplified compose file will be recommended.
version: '2'
services:
php:
image: jguyomard/laravel-php:7.2
build:
context: .
dockerfile: infrastructure/php/Dockerfile
volumes:
- ./:/var/www/
- $HOME/.composer/:$HOME/.composer/
nginx:
image: jguyomard/laravel-nginx:1.13
build:
context: .
dockerfile: infrastructure/nginx/Dockerfile
ports:
- 81:80
postgresql:
image: postgres:9.6-alpine
volumes:
- pgsqldata:/var/lib/postgresql/data
environment:
- "POSTGRES_DB=homestead"
- "POSTGRES_USER=homestead"
- "POSTGRES_PASSWORD=homestead"
ports:
- "5433:5432"
redis:
image: redis:4.0-alpine
command: redis-server --appendonly yes
ports:
- "6379:6379"
# elastic:
# image: elasticsearch:5.5-alpine
# ports:
# - "9200:9200"
volumes:
pgsqldata:

How to connect to services running in different docker stack?

Can a services running in two different docker stack communicate ?
This is what I have tried and not able to achieve it.
Created a stack (stack1) running nginx and php-fpm its running great.
Created another stack (stack2) running mysql database.
Now I want to make the stack1 service able to communicate with stack2 such that it can access the database service.
I though this might help and created a external network and trying to
add the service in stack1 and stack2 to it such that they can
communicate with each others too
Mystack1 docker-compose file
version: "3.4"
networks:
apps-net:
db-net:
external:
name: db-net
services:
web:
image: nginx:latest
ports:
- "9080:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
networks:
- apps-net
- db-net
deploy:
mode: replicated
replicas: 1
php:
image: php:7-fpm
volumes:
- ./code:/code
networks:
- apps-net
- db-net
deploy:
mode: replicated
replicas: 1
MY stack2 docker-compose file
version: '3.3'
networks:
db-net:
external:
name: db-net
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: root
MYSQL_PASSWORD: password
networks:
- db-net
volumes:
db_data:
I created a swarm scope network with docker network create db-net command
OUTPUT:
The nginx and php is working fine but I added the database connection
codes in the index.php which resulted in error message.Is the error
because they are not connected? I have installed php-mysql extensions
too but it has the error. How can I make sure the services are
communicating successfully.
nginx and php working
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /code/index.php:11 Stack trace: #0 {main} thrown in /code/index.php on line 11
Instead of networks (which I often see asked here on stack as not working like expected) try using external_links which is well explained here.
Try removing all custom network configurations and simply modify your application compose file like this:
version: "3.4"
# removed all custom networks configuration
services:
web:
image: nginx:latest
external_links:
- mysql_1:mysql
[..]
php:
image: php:7-fpm
external_links:
- mysql_1:mysql
[..]
where mysql_1 is a actual container name create by your latter compose file, and mysql is a alias by which your service will be available inside php and web containers
Links are a legacy option in v3 and Docker suggests using networks instead.
I'll post a edit about swarm deployment as approach would be totally different because Docker ignores links when deploying swarm.

Console PHP script runs differently when executes through docker exec or through PhpStorm

I'm trying to run console run Doctrine 2's console script through the PhpStorm. Docker is set up as Deploy server.
If I run this:
$ docker exec container_name /var/www/vendor/bin/doctrine-module orm:schema-tool:create
it prints:
No Metadata Classes to process.
But when I run PHP Run/Debug configuration in PhpStorm:
File: /home/username/PhpstormProjects/proj/vendor/bin/doctrine-module
it prints:
docker://image_name/container_name /var/www/vendor/bin/doctrine-module
Fatal error: Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 765
Zend\ServiceManager\Exception\ServiceNotCreatedException: Service with name "doctrine.connection.orm_default" could not be created. Reason: An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 765
Call Stack:
0.0001 349368 1. {main}() /var/www/vendor/doctrine/doctrine-module/bin/doctrine-module:0
0.0268 360480 2. include('/var/www/vendor/doctrine/doctrine-module/bin/doctrine-module.php') /var/www/vendor/doctrine/doctrine-module/bin/doctrine-module:4
0.9376 4076096 3. Zend\ServiceManager\ServiceManager->get() /var/www/vendor/doctrine/doctrine-module/bin/doctrine-module.php:61
0.9376 4076096 4. Zend\ServiceManager\ServiceManager->doCreate() /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php:200
Process finished with exit code 255
I checked that when PDO is creating it receives absolutely the same arguments
new PDO("mysql:host=db;port=3306;dbname=dbname", "user", "pass", [])
docker-compose.yml:
version: '2'
services:
nginx:
container_name: nginx
build:
context: .
dockerfile: DockerfileNginx
ports:
- "80:80"
depends_on:
- php
working_dir: /var/www
links:
- php
volumes:
- .:/var/www
links:
- db
php:
container_name: php
build:
context: .
dockerfile: DockerfilePhp
- db
volumes:
- .:/var/www
expose:
- "9000"
depends_on:
- db
db:
container_name: db
image: "mysql:5.6"
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: dbname
MYSQL_USER: user
MYSQL_PASSWORD: pass
I had the same problem and I found out, that PHPStorm starts a new, dedicated Docker container for your PHP calls. So, all your docker-compose configuration is missing at this point.
I did the following:
start your Docker stack with compose
after that find your network name with docker network ls (see Cannot link to a running container started by docker-compose for help)
inside PHPStorm goto your preferences for your Docker container (Languages & Frameworks -> PHP -> PHPUnit: Docker container ...) and add a link to your db (something like: name = your_containter_name_1, alias = db) and change the network mode from bridge to your network name

Categories