首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >拒绝连接的Docker web和apis

拒绝连接的Docker web和apis
EN

Stack Overflow用户
提问于 2018-09-09 23:59:33
回答 1查看 3.7K关注 0票数 5

我是码头新手。我已经创建了一个docker-compose,其中有1个主应用程序(laravel)网站,和2个app。

我正在尝试从laravel站点访问apis,但不断收到:

代码语言:javascript
复制
cURL error 7: Failed to connect to 0.0.0.0 port 8082: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

我的docker-compose.yml是:

代码语言:javascript
复制
version: '3'
services:
  web:
    image: nginx:1.10
    volumes:
    - ./vhost.conf:/etc/nginx/conf.d/default.conf
    ports:
    - "8080:80"
    depends_on:
    - app
    restart: always

  app:
    build: . #run the dockerfile to install whatever
    env_file: .env
    volumes:
    - ./:/var/www
    depends_on:
    - database

  web-api2:
    image: nginx:1.10
    volumes:
    - ../api/api2/vhost.conf:/etc/nginx/conf.d/default.conf
    ports:
    - "8081:81"
    depends_on:
    - app-api2

  app-api2:
    build: . #run the dockerfile to install whatever
    env_file: .env
    volumes:
    - ../api/api2:/var/www
    depends_on:
    - database

  web-api1:
    image: nginx:1.10
    volumes:
    - ../api/api1/vhost.conf:/etc/nginx/conf.d/default.conf
    ports:
    - "8082:82"
    depends_on:
    - app-api1

  app-api1:
    build: . #run the dockerfile to install whatever
    env_file: ../api/api1/.env
    volumes:
    - ../api/api1:/var/www
    depends_on:
    - database

  database:
    image: mysql:5.7
    environment:
    - "MYSQL_ROOT_PASSWORD=IAmSoSecure"
    - "MYSQL_DATABASE=my_app"
    ports:
    - "33061:3306"
    volumes:
    - my_app:/var/lib/mysql
    restart: always


volumes:
  wildfire:

我研究过docker网络,但我的尝试失败了。我有一个网络设置,并尝试使用子网,但它不起作用:

代码语言:javascript
复制
"IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "172.18.0.0/16",
                "Gateway": "172.18.0.1"
            }
        ]
    },

我也尝试过访问我启用的端口:

代码语言:javascript
复制
"Ports": {
            "443/tcp": null,
            "80/tcp": null,
            "82/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8082"
                }
            ]
        },

我像往常一样通过Guzzle请求呼叫:

代码语言:javascript
复制
$client = new Client();
$res = $client->get('127.0.0.1:8082/accounts');
dd($res->getBody()->getContents());

我尝试了不同的I,但找不到正确的I。

我当前的vhost.conf是:

代码语言:javascript
复制
server {
listen 80;
index index.php server.php;
root /var/www/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html server.php index.php;

charset utf-8;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass app:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
    include  /etc/nginx/mime.types;
}

location ~ /\.(?!well-known).* {
    deny all;
}

location ~ \.css {
    add_header  Content-Type    text/css;
}

location ~ \.js {
    add_header  Content-Type    application/x-javascript;
}
}
EN

回答 1

Stack Overflow用户

发布于 2018-09-10 15:49:06

为了解决这个问题,我将我正在查看的url更改为:

代码语言:javascript
复制
$client = new Client();
$res = $client->get('**http://web-api2:82/accounts**');
dd($res->getBody()->getContents());

出于最佳实践的考虑,我还将端口号更改为8082。这也反映在我的docker-compose.yml中,所以:

代码语言:javascript
复制
web 8080:8080
web-api1 8081:8081
web-api2 8082:8082

感谢所有人的回答!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52246250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档