Reputation: 71
[demonstration below]
Greeting developers! Recently I'm trying to dockerize a php website, but I got connection refused when trying to curl from one to another.
Basically I have three site:
The problem is connection is refused when I try to call api from first-site.
./docker-compose.yml
version: "3.1"
services:
www:
build: ./www/.
ports:
- "0.0.0.0:8080:80"
- "0.0.0.0:443:443"
volumes:
- ./htdocs:/var/www
- ./www/conf:/etc/httpd/conf
networks:
- local
php:
build: ./php/.
ports:
- '9000'
volumes:
- ./htdocs:/var/www
networks:
- local
networks:
local:
driver: bridge
./www/Dockerfile
FROM centos:8.2.2004
MAINTAINER kenphanith <https://github.com/kenphanith>
LABEL description="develop environment"
LABEL httpd_version="2.4"
RUN dnf -yq module install httpd:2.4 && \
dnf -yq install epel-release && \
dnf -yq install mod_perl ImageMagick && \
dnf clean all
RUN sscg -q \
--cert-file /etc/pki/tls/certs/localhost.crt \
--cert-key-file /etc/pki/tls/private/localhost.key \
--ca-file /etc/pki/tls/certs/localhost.crt \
--lifetime 365 \
--hostname localhost \
--email root@localhost
EXPOSE 80/tcp 443/tcp
WORKDIR /var/www
CMD ["/usr/sbin/apachectl","-D","FOREGROUND"]
inside ./www/conf
I have
firstsite.conf
<VirtualHost *:80>
ServerName firstsite.com
DocumentRoot /var/www/firstsite
<Directory "/var/www/firstsite">
Options -Indexes +FollowSymLinks
DirectoryIndex index.php index.html
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName firstsite
DocumentRoot /var/www/firstsite
SSLEngine on
SSLCertificateFile /etc/httpd/conf/vhost.d/ssl/site.crt
SSLCertificateKeyFile /etc/httpd/conf/vhost.d/ssl/site.key
SSLCertificateChainFile /etc/httpd/conf/vhost.d/ssl/site.mid.crt
<Directory "/var/www/ap.subaru.jp">
Options -Indexes +FollowSymLinks
DirectoryIndex index.php index.html
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
</Directory>
</VirtualHost>
api.conf
<VirtualHost *:80>
ServerName api.com
DocumentRoot /var/www/api
<Directory "/var/www/api">
AllowOverride All
Options -Indexes +FollowSymLinks
DirectoryIndex index.php index.html
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName api
DocumentRoot /var/www/api
SSLEngine on
SSLCertificateFile /etc/httpd/conf/vhost.d/ssl/site.crt
SSLCertificateKeyFile /etc/httpd/conf/vhost.d/ssl/site.key
SSLCertificateChainFile /etc/httpd/conf/vhost.d/ssl/site.mid.crt
<Directory "/var/www/api">
AllowOverride All
Options -Indexes +FollowSymLinks
DirectoryIndex index.php index.html
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
</Directory>
</VirtualHost>
secondsite.conf
<VirtualHost *:80>
ServerName secondsite.com
Include conf/vhost.d/secondsite.inc
</VirtualHost>
<VirtualHost *:443>
ServerName secondsite.com
SSLEngine on
SSLCertificateFile /etc/httpd/conf/vhost.d/ssl/site.crt
SSLCertificateKeyFile /etc/httpd/conf/vhost.d/ssl/site.key
SSLCertificateChainFile /etc/httpd/conf/vhost.d/ssl/site.mid.crt
Include conf/vhost.d/secondsite.inc
</VirtualHost>
./php/Dockerfile
FROM php:7.3.19-fpm-alpine
MAINTAINER KenPhanith <https://github.com/kenphanith>
LABEL description="develop environment"
LABEL httpd_version="7.3.19"
RUN apk update && apk add bash
RUN apk add --no-cache --update libpq && \
docker-php-ext-install pdo_mysql mysqli
RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini && \
sed -ie 's/^short_open_tag = Off$/short_open_tag = ON/g' $PHP_INI_DIR/php.ini
RUN sed -ie 's/;security.limit_extensions = .php .php3 .php4 .php5 .php7/security.limit_extensions = .php .php3 .html/g' /usr/local/etc/php-fpm.d/www.conf
From ./firstsite/index.php I have this code
<?php
try {
date_default_timezone_set('Asia/Tokyo');
$base = "https://api.com"
$url = $base."/api/detailparam=1¶m2=".$_REQUEST['param2']."¶m3=".$_REQUEST['param3'];
$ch = curl_init();
// Check if initialization had gone wrong*
if ($ch === false) {
throw new Exception('failed to initialize');
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
// Check the return value of curl_exec(), too
if ($response === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
$json = json_decode($response,true);
if ($json==null) {
echo "Can't connect server";
exit;
}
} catch (Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
when I execute this code I got
Fatal error: Curl failed with error #56: Proxy CONNECT aborted in /var/www/api/xxxxxp on line xx
My own assumption was, the php and www container I am having in docker-compose.yml are not connected or linked on the same network, but I cannot figure out the solution until now. Please help!!!! I appreciate that <3
Upvotes: 2
Views: 3937
Reputation: 12384
The names of your services are networkable. For instance, I can put mariadb
in my database connections instead of localhost
or an ip. In the example below, awesome.scot
is my Apache server, you'll notice another called app
, that literally just mounts the files, so it's possible to add a service for each web site! And if you refer to the service names, your calls will complete without any problems :-)
Here's the Docker compose file for my LAMP sdtack. If you'd like to try it or steal bits from it, you'll find it here https://github.com/delboy1978uk/lamp, It comes with mailhog as well, self signed ssl, xdebug, etc.
version: '2'
volumes:
db_data:
driver: local
services:
awesome.scot:
build: ./build/httpd
links:
- php
ports:
- 80:80
- 443:443
volumes_from:
- app
php:
build: ./build/php
ports:
- 9000
- 9001
volumes_from:
- app
links:
- mariadb
- mail
environment:
APPLICATION_ENV: 'development'
user: php:staff
app:
image: httpd:2.4.38
volumes:
- ./:/var/www/html
command: "echo true"
mariadb:
image: mariadb:latest
volumes:
- ./build/data:/docker-entrypoint-initdb.d
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: '[123456]'
MYSQL_USER: dbuser
MYSQL_PASSWORD: '[123456]'
ports:
- 3306:3306
mail:
image: mailhog/mailhog
ports:
- 1025:1025
- 8025:8025
Upvotes: 2