Docker php curl连接被拒绝

Docker php curl连接被拒绝,php,docker,curl,docker-compose,Php,Docker,Curl,Docker Compose,[下面的演示] 欢迎开发者! 最近,我试图将一个php网站dockerize,但在尝试从一个网站向另一个网站卷曲时,连接被拒绝 基本上我有三个网站: 原料药 第一站点 第二站点 问题是当我尝试从第一个站点调用api时,连接被拒绝。 /docker compose.yml version: "3.1" services: www: build: ./www/. ports: - "0.0.0.0:8080:80"

[下面的演示]

欢迎开发者! 最近,我试图将一个php网站dockerize,但在尝试从一个网站向另一个网站卷曲时,连接被拒绝

基本上我有三个网站:

  • 原料药
  • 第一站点
  • 第二站点
问题是当我尝试从第一个站点调用api时,
连接被拒绝。

/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"]
api.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>
<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>
<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>


ServerName api.com
DocumentRoot/var/www/api
允许超越所有
选项-索引+FollowSymLinks
DirectoryIndex.php index.html
SetHandler“代理:fcgi://php:9000"
服务器名api
DocumentRoot/var/www/api
斯伦金安
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
允许超越所有
选项-索引+FollowSymLinks
DirectoryIndex.php index.html
SetHandler“代理:fcgi://php:9000"
secondsite.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>
<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>
<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>


ServerName secondsite.com
包括conf/vhost.d/secondsite.inc
ServerName secondsite.com
斯伦金安
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
包括conf/vhost.d/secondsite.inc
/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

来自php:7.3.19-fpm-alpine
维护者肯法尼思
标签说明=“开发环境”
标签httpd_version=“7.3.19”
运行apk更新和apk添加bash(&A)
运行apk add--无缓存--更新libpq&&\
docker php ext安装pdo_mysql mysqli
运行cp$PHP_INI_DIR/PHP.INI-development$PHP_INI_DIR/PHP.INI&&\
sed-ie's/^short\u open\u tag=Off$/short\u open\u tag=ON/g'$PHP\u INI\u DIR/PHP.INI
运行sed-ie/;security.limit_extensions=.php.php3.php4.php5.php7/security.limit_extensions=.php.php3.html/g'/usr/local/etc/php-fpm.d/www.conf
/firstsite/index.php我有以下代码

<?php
try {
    date_default_timezone_set('Asia/Tokyo');

    $base = "https://api.com"
    $url = $base."/api/detailparam=1&param2=".$_REQUEST['param2']."&param3=".$_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);
}
?>

您的服务名称可以联网。例如,我可以在数据库连接中放置
mariadb
,而不是
localhost
或ip。在下面的示例中,
awesome.scot
是我的Apache服务器,您会注意到另一个名为
app
,它实际上只是挂载文件,因此可以为每个网站添加服务!如果您参考服务名称,您的呼叫将毫无问题地完成:-)

这是我的LAMP sdtack的Docker撰写文件。如果您想尝试它或从中窃取一些信息,您可以在这里找到它,它还附带mailhog、自签名ssl、xdebug等

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