I have a php project in localhost which I can access from http://localhost:8000 and I have a service running in a docker container which I can access from http://192.168.56.100:8080. To access http://192.168.56.100:8080 I must configure my browser under manual proxy like this( this is from firefox configuration)
http proxy: proxy.comp.mycompanyname.com
No proxy for: localhost, 127.0.0.1,192.168.56.0/24
If I configure my browser above way then I can access this service http://192.168.56.100:8080 from browser.
Now the problem is, I want to connect to the service from my php project from localhost in windows machine. I tried this
$client = new Soapclient("http://192.168.56.100:8080");
This gives me error
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'SoapFault: Could not connect to host"
I think I am missing some proxy configuration somewhere but not sure where. How to solve this?
Running following command gives me this
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.56.100:2376 v1.12.3
And my docker compose file
version: '2'
services:
idtool3-mysql-gpas:
image: mysql:5.7
container_name: tmf-gpas-1.7.8-mysql
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "gpas_2016"
volumes:
- ./gpas/sqls:/docker-entrypoint-initdb.d
labels:
- "service-name:gpas"
- "service-type:db-srv"
- "environment:test"
idtool3-gpas-wildfly:
image: tmfev/gpas:1.7.8
container_name: tmf-gpas-1.7.8-wildfly
links:
- "idtool3-mysql-gpas:mysqldb"
ports:
- "8080:8080"
depends_on:
- idtool3-mysql-gpas
labels:
- "service-name:gpas"
- "service-type:app-srv"
- "environment:test"
entrypoint: /bin/bash
command: -c "/opt/wait-for-it.sh mysqldb:3306 -t 120 && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"
Related
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']);
I have created an nginx container that is open to port 8080:80
so I could access it from the host.
it is connected to php fpm container that has an open port 9000:9000
nginx successfully runs with php.
My problem is that php tries to access localhost:8080
but the problem is that the php localhost:8080 is not valid, it needs to connect to the nginx container.
here is the error on my wordpress site:
you can see that something is funky there...
below I'll attach my docker-compose.yml
Downloading install package from http://localhost:8080/wp-content/themes/realtyspace/plugins/advanced-custom-fields-pro.zip…
Download failed. cURL error 7: Failed to connect to localhost port 8080: Connection refused
docker-compose.yml
version: '2'
services:
my-nginx:
build: .
volumes:
- ./../:/var/www/html
ports:
- "8080:80"
links:
- my-php
my-php:
build:
context: .
dockerfile: Dockerfile.php-fpm
volumes:
- ./../:/var/www/html
ports:
- "9000:9000"
links:
- my-mysql
my-mysql:
image: mariadb:5.5
volumes:
- /var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp
MYSQL_DATABASE: wp
MYSQL_USER: wp
MYSQL_PASSWORD: wp
Use docker's internal networking and configure php to access http://my-nginx:80.
localhost will resolve to the isolated IP of the php container itself, not that of the Docker host that's running everything. And trying to pass http://dockerhost:8080 will result in a non-portable docker-compose.yml and likely issues with iptables firewall and nat rules that are more trouble than they are worth. The value of using the v2 compose files is that you get an isolated network internal to Docker with DNS resolution of each of your containers to work with each other.
My question is how to configure connection to mysql container.
Here is my docker-compose.yml
version: '3'
services:
php:
build: ./php-fpm
volumes:
- ./iym:/var/www/iym
- ./php-fpm/php.ini:/usr/local/etc/php/php.ini
depends_on:
- mysql
web:
build: ./nginx
ports:
- "8888:80"
volumes:
- ./iym:/var/www/iym
- ./nginx/iym.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
mysql:
image: mysql:5.6
restart: always
command: --default-authentication-plugin=mysql_native_password
volumes:
- ${DB_PATH_HOST}:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "symf0ny"
ports:
- "3306:3306"
And here is my DATABASE_URL in .env file
DATABASE_URL=mysql://root:symf0ny#127.0.0.1:3306/iym
When i try to run php bin/console doctrine:database:create i get an error like "SQLSTATE[HY000] [2002] Connection refused". OS - ubuntu 18.04. What should i do to connect to DB? Many thanks!
I assume you are trying to connect from another container/service defined in docker-compose and you are using current version of docker-compose (2.4 or 3.7)
You need to change
DATABASE_URL=mysql://root:symf0ny#127.0.0.1:3306/iym
to
DATABASE_URL=mysql://root:symf0ny#mysql:3306/iym
The reason is that 127.0.0.1 is refering to the localhost of the machine on which php runs. In this case it's the php's container localhost. But the db doesn't run there. It runs in another docker container, which can be reached under the service name, mysql in this case.
In docker-compose networking docs is written:
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
but, the service is discoverable under the container name (which is automatically generated to projectName_serviceName_1 (project name begin by default folder name), but also under service name, link and service alias if defined. I would recommend using service name wherever possible.
More in docs: https://docs.docker.com/compose/networking/
In my case, the Docker variable overrides the parameter from .env file and I'm trying to connect to the wrong host. Found that via dd($this->constructPdoDsn($params)) in the Doctrine\DBAL\Driver\PDO\MySQL\Driver::connect, maybe it will be helpful
I'm using docker-php with nginx + php-fpm (docker-compose project). When I'm trying to run an example from the documentation:
<?php
use Docker\API\Model\ContainersCreatePostBody;
use Docker\Docker;
$docker= Docker::create();
$containerConfig = new ContainersCreatePostBody();
$containerConfig->setImage('nginx:latest');
$containerConfig->setCmd(['echo', 'I am running a command']);
$containerCreateResult = $docker->containerCreate($containerConfig);
var_dump($containerCreateResult);
exit;
and I'm getting the error:
Http \ Client \ Socket \ Exception \ ConnectionException - Permission denied
As far as I understand the problem is that user group, that php-fpm is using, does not have rw rights to docker.sock (I'm mounting it from the host on which the docker is running).
Configuration:
docker-compose:
The shell directory contains an application on yii2, that is used by docker-php.
version: '2'
services:
web:
image: 'nginx:latest'
container_name: web
ports:
- '80:80'
- '443:443'
volumes:
- './:/shell'
networks:
- backend
- frontend
restart: always
php:
build: ./docker/php/
container_name: php
volumes:
- './:/shell'
- '/var/run/docker.sock:/var/run/docker.sock'
environment: []
networks:
- backend
restart: always
networks:
frontend:
driver: bridge
backend:
driver: bridge
Dockerfile for php-fpm: github gist (too large file for post ~100 lines)
Docker is installed for the experiment, and so it is useless in the container php-fpm.
Software versions:
Docker version 1.13.1
docker-compose version 1.8.0
Kubuntu 17.10 x64
I found something similar in the Internet (one, two, three ...), the decision is to add the user, from which the application works in the container, to the group www-data.
If I assign 777 rights to docker.sock, then everything will be working, but this is a bad solution =)
I use Docker for my PHP development environment, and I set up my images with Docker Compose this way:
myapp:
build: myapp/
volumes:
- ./myapp:/var/www/myapp
php:
build: php-fpm/
expose:
- 9000:9000
links:
- elasticsearch
volumes_from:
- myapp
extra_hosts:
# Maybe the problem is related to this line
- "myapp.localhost.com:127.0.0.1"
nginx:
build: nginx/
ports:
- 80:80
links:
- php
volumes_from:
- myapp
elasticsearch:
image: elasticsearch:1.7
ports:
- 9200:9200
Nginx is configured (in its Docker file) with a virtual host named myapp.localhost.com (server_name parameter) and that points to the /var/www/myapp folder.
All this works fine.
But here is my problem: my web app is calling itself via the myapp.localhost.com URL with cURL (in the PHP code), which can be more easily reproduced by running this command:
docker-compose run php curl http://myapp.localhost.com
The cURL response is the following:
cURL error 7: Failed to connect to myapp.localhost.com port 80: Connection refused
Do you have any idea on how I can call the app URL? Is there something I missed in my docker-compose.yml file?
Months later, I come back to post the (quite straightforward) answer to my question:
Remove the server_name entry in the Nginx host configuration
Remove the extra_hosts entry in docker-compose.yml file (not necessary, but it's useless)
Simply call the server with the Nginx container name as a host (nginx here):
docker-compose exec php curl http://nginx