问题
I have a website (ZF1) and an API (Laravel) running on the same Docker (Laradock) container. I can access each separately through a browser, but when I make a cURL request from the website to the application, I get a null response and the header returns 0. If I output the cURL errors, then I get this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '[API_ENDPOINT]');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_close ($ch);
echo curl_strerror(curl_errno($ch));
(7) Failed to connect to [API_HOST] port 80: Connection refused
However, if I make a cURL request to https://www.google.com from the application, then it returns a result. I also tried using file_get_contents(), but I received no response and the following warning:
[Thu May 18 21:41:33.828737 2017] [proxy_fcgi:error] [pid 949:tid 139999802541824] [client 172.20.0.1:49652] AH01071: Got error 'PHP message: PHP Warning: file_get_contents
([API_ENDPOINT]): failed to open stream: Connection refused in /var/www/projects/[APPLICATION_PATH]/[CONTROLLER].php on line 2367\n', referer: [WEBSITE_HOST]/[URI]
I also SSHed into the Apache2 container and was able to make a successful cURL call to the API_ENDPOINT and get the expected data back. I then tried using wget to get the header info and received the following:
root@cd3a4177dcfa:/var/log/apache2# wget --header="Host: http://subdomain.example.dev/api/calendarevents" -Os http://localhost
--2017-05-19 07:28:15-- http://localhost/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... connected.
HTTP request sent, awaiting response... No data received.
Retrying.
--2017-05-19 07:28:16-- (try: 2) http://localhost/
Connecting to localhost (localhost)|::1|:80... connected.
HTTP request sent, awaiting response... No data received.
Retrying.
In case it is an Apache2 issue, here is my API VirtualHost:
Listen 80
<VirtualHost *:80>
ServerName subdomain.example.dev
DocumentRoot /var/www/projects/[API_PROJECT]/public/
CustomLog /var/log/apache2/[API_HOST]-access.log combined
ErrorLog /var/log/apache2/[API_HOST]-error.log
Options Indexes FollowSymLinks
<Directory "/var/www/projects/[API_PROJECT]/public/">
Options FollowSymLinks
AllowOverride All
Require all Granted
</Directory>
# set environment
#SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 // Did not notice a difference in behavior when enabled
#CGIPassAuth on // Sites stopped loading when enabled
</VirtualHost>
And Website VirtualHost:
Listen 80
<VirtualHost *:80>
ServerName [WEBSITE_HOST]
DocumentRoot /var/www/projects/[WEBSITE_PROJECT]
ErrorLog /var/log/apache2/[WEBSITE_HOST]-error.log
Options Indexes FollowSymLinks
<Directory "/var/www/projects/[WEBSITE_PROJECT]/repo">
Options FollowSymLinks
AllowOverride All
Require all Granted
</Directory>
# set environment
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_LOGGING false
#SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 // Did not notice a difference in behavior when enabled
#CGIPassAuth on // Sites stopped loading when enabled
</VirtualHost>
This is the Docker info (Windows 10/Hyper V/Docker Version 17.03.1-ce-win12 (12058) Channel: stable)
Containers: 9
Running: 5
Paused: 0
Stopped: 4
Images: 233
Server Version: 17.03.1-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.27-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.837 GiB
Name: moby
ID: PE42:IS45:4OO6:JMEQ:NWNB:NQDF:RPEL:JPHJ:L6OP:A5SL:IDP3:F7SV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 86
Goroutines: 74
System Time: 2017-05-19T04:52:50.5943959Z
EventsListeners: 0
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
And these are the containers that I am currently running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd3a4177dcfa laradock_apache2 "/opt/docker/bin/e..." 8 hours ago Up 8 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp laradock_apache2_1
8ef66cb80a2e laradock_php-fpm "docker-php-entryp..." 8 hours ago Up 8 hours 9000/tcp laradock_php-fpm_1
988eff458036 laradock_workspace "/sbin/my_init" 8 hours ago Up 8 hours 0.0.0.0:2222->22/tcp laradock_workspace_1
8be5253e8622 laradock_redis "docker-entrypoint..." 8 hours ago Up 8 hours 0.0.0.0:6379->6379/tcp laradock_redis_1
aa6d8d6ae950 laradock_mysql "docker-entrypoint..." 8 hours ago Up 8 hours 0.0.0.0:3306->3306/tcp laradock_mysql_1
I am not sure what is causing the connection to be refused between the two sites on the same container. I checked my php.ini and both curl and allow_url_fopen = on were enabled. I am able access the API_ENDPOINT directly through both browser and CLI cURL and get the expected results. My best guesses at this point are some sort of port conflict, an authorization header being blocked, Apache2 configuration, or some sort of odd Docker/Laradock issue. I also thought it might of been a conflict between Apache2 and PHP-FPM, but the solutions detailed here didn't seem to work for me.
Thank you in advanced for any assistance that you can provide.
回答1:
Instead of adding to extra_hosts of the php-fpm container. Modify the Apache2 container's Backend network definition and add an alias. this was you dont have to keep changing the extra_hosts everytime the ip changes
networks:
frontend:
backend:
aliases:
- subdomain.example.app
回答2:
I figured out what was causing my issue. I needed to add an extra-host to the PHP-FPM section of my docker-compose.yml file. So add:
- "subdomain.example.app:10.0.75.1"
To extra-hosts. Then you need to rebuild your containers:
docker-compose up -d --build apache2 mysql redis
And this is what the PHP-FPM section of your docker-compose.yml for Laradock should look like:
php-fpm:
build:
context: ./php-fpm
args:
- INSTALL_XDEBUG=${PHP_FPM_INSTALL_XDEBUG}
- INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
- INSTALL_SOAP=${PHP_FPM_INSTALL_SOAP}
- INSTALL_MONGO=${PHP_FPM_INSTALL_MONGO}
- INSTALL_ZIP_ARCHIVE=${PHP_FPM_INSTALL_ZIP_ARCHIVE}
- INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
- INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
- INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
- INSTALL_OPCACHE=${PHP_FPM_INSTALL_OPCACHE}
- INSTALL_EXIF=${PHP_FPM_INSTALL_EXIF}
- INSTALL_AEROSPIKE_EXTENSION=${PHP_FPM_INSTALL_AEROSPIKE_EXTENSION}
- INSTALL_MYSQLI=true
- INSTALL_TOKENIZER=${PHP_FPM_INSTALL_TOKENIZER}
- INSTALL_INTL=${PHP_FPM_INSTALL_INTL}
- INSTALL_GHOSTSCRIPT=${PHP_FPM_INSTALL_GHOSTSCRIPT}
dockerfile: "Dockerfile-${PHP_VERSION}"
volumes_from:
- applications
volumes:
- ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
expose:
- "9000"
depends_on:
- workspace
extra_hosts:
- "dockerhost:${DOCKER_HOST_IP}"
- "subdomain.example.app:10.0.75.1"
environment:
- PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
networks:
- backend
来源:https://stackoverflow.com/questions/44064557/curl-connection-refused-between-applications-on-docker-container