Docker web和apis拒绝连接

问题描述 投票:4回答:3

我是码头工人的新手。我创建了一个docker-compose,它有1个主应用程序(laravel)站点和2个api。

我试图从laravel网站访问apis,但继续得到:

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是:

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:

我已经研究过码头网络,但我的尝试失败了。我有一个网络设置,尝试使用子网,但它没有做到这一点:

"IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "172.18.0.0/16",
                "Gateway": "172.18.0.1"
            }
        ]
    },

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

"Ports": {
            "443/tcp": null,
            "80/tcp": null,
            "82/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8082"
                }
            ]
        },

我通过Guzzle按照惯例请求通话:

$client = new Client();
$res = $client->get('127.0.0.1:8082/accounts');
dd($res->getBody()->getContents());

我尝试过不同的IP但找不到正确的IP。

我当前的vhost.conf是:

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;
}
}
laravel api docker docker-compose lumen
3个回答
1
投票

在docker撰写的默认网络上,由orchestrator管理的每个容器都可以通过它的服务名访问,该服务名恰好与docker-compose.yaml文件中为其指定的名称相同。因此,在您的情况下,同一网络上的每个容器可能会达到app-api2作为您的问题详细信息:

http://app-api2:82/accounts

有关在容器中使用127.0.0.1的更完整答案意味着您正在尝试访问容器内的服务,并在您的情况下监听82。由于您没有在同一容器中运行的其他可执行文件并且正在侦听该端口,因此您正在接收Connection refused

您可能也很有用,要知道您不需要公开docker-compose.yaml文件中的端口,以便同一网络上的其他容器进行通信。暴露的端口使您可以从主机发出请求(实际上您将使用127.0.0.1)。

如果出于某种原因,您确实希望使用容器的IP地址在容器之间进行通信,那么您可以使用docker inspect <container name>找到特定容器的当前IP。但请注意,此IP是短暂的,并且每次容器旋转时都可能会更改。

更多详细信息可以在https://docs.docker.com/compose/networking找到


2
投票

在docker compose上,启动3 nginx:web,web-api2和web-api1。

根据您的编写,web-api 2在端口81上侦听,在端口82上侦听web-api 1。

如果您希望从Web应用程序访问api,您将需要使用:

http://web-api2:81/...
http://web-api1:82/...

请注意,在本地计算机上,当您在web-api2上将端口8081映射到81并在web-api1上映射8082到82时,将使用http://localhost:8081http://localhost:8082


1
投票

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

$client = new Client();
$res = $client->get('**http://web-api2:82/accounts**');
dd($res->getBody()->getContents());

我还将端口号更改为8082以获得最佳实践。这也反映在我的docker-compose.yml中:

web 8080:8080
web-api1 8081:8081
web-api2 8082:8082

感谢大家的回答!

© www.soinside.com 2019 - 2024. All rights reserved.