Docker 容器无法在容器本身内调用 localhost

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

我有一个 docker-compose 环境设置。 但在服务“Lumen”内部,我试图向服务本身发出 CURL 请求。

但是,容器无法从 localhost:8000 或 lumen:8000 访问自身??

当我从服务调用 lumen:8000 时,它永远不会返回响应,只是继续加载(并且curl请求是针对不同的url,因此没有无限循环)

在我的 Laravel 控制器中,我发现协议、主机和端口为:http://lumen:8000

我似乎 Laravel 无法连接到自身,而我的项目确实需要它。

我可以通过 localhost 从我自己的计算机连接到 Laravel,但我需要 Laravel 调用它自己。

执行 CURL 请求后来自 Laravel 控制器的错误消息:

Failed to connect to localhost port 8000 after 0 ms: Connection refused
将主机更改为“lumen”只会使请求负载无限。无论我尝试连接到哪个页面。

Docker-compose 文件:

version: "3.5"

services:
  lumen:
    expose: 
      - "8000"
    ports:
      - "8000:8000"
    volumes:
      - ./server:/var/www/html
      - ./server/vendor:/var/www/html/vendor/
    build: 
      context: server
      dockerfile: Dockerfile
    command: php -S lumen:8000 -t public
    restart: always
    privileged: true
    depends_on:
      - database
    networks:
      - database

  frontend:
    build:
      context: client
      dockerfile: Dockerfile
    volumes:
      - ./client/src:/app/src
    ports:
      - 3000:3000
    stdin_open: true
    #restart: always
    networks:
      - database

  # Database Service (Mysql)
  database:
    image: mysql:latest
    container_name: blogmoda_mysql
    environment:
      MYSQL_DATABASE: blogmoda-app
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
    ports:
      - "127.0.0.1:3306:3306"
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - database

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: dev_phpmyadmin
    links:
      - database
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    depends_on: 
      - database
    ports:
      - 9001:80
    networks:
      - database

volumes:
  db-data:

# Networks to be created to facilitate communication between containers
networks:
  database:

服务器dockerfile:

FROM php:8.1-fpm-alpine

RUN apk update && apk add bash

RUN apk add chromium

RUN apk add --no-cache zip libzip-dev
RUN docker-php-ext-configure zip
RUN docker-php-ext-install zip
RUN docker-php-ext-install pdo pdo_mysql 
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install opcache

WORKDIR /var/www/html/

RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

COPY . .

RUN composer install --ignore-platform-req=ext-zip --ignore-platform-reqs
php laravel docker docker-compose
3个回答
0
投票

我相信

localhost
应该有效。假设curl安装在
lumen
中,在
lumen
服务下的撰写文件中,你可以尝试更改你的命令

command: php -S lumen:8000 -t public

通过 bash as 直接卷曲

command: sh -c "curl -s localhost:8000"

然后检查

lumen
容器的日志,看看curl是否运行成功。


0
投票

尝试用

0.0.0.0:8000
代替
localhost:8000
。它也适用于本地主机


0
投票

如果您尝试从 Docker 容器中运行的 PHP 脚本向同一 Docker 容器内的本地主机上运行的服务发出 cURL 请求,则应使用 Docker 容器的 IP 地址,而不是

localhost
0.0.0.0
。这是因为每个 Docker 容器都有自己独立的网络命名空间,而
localhost
指的是容器本身,而不是主机。

以下是如何修改 cURL 请求以使用 Docker 容器的 IP 地址:

<?php
// API endpoint URL
$url = 'http://<container_ip>/api/order-status'; // Replace <container_ip> with the IP address of your Docker container

// Replace with your actual API key
$api_key = '12345690MdeSD';

// Data to be sent in the request body
$data = array(
    'order_id' => $orderId, // assuming $orderId is defined elsewhere
    'status_id' => $statusId // assuming $statusId is defined elsewhere
);

// Encode data as JSON
$data_json = json_encode($data);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Api-Key: ' . $api_key
));

// Execute cURL request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    // Print response
    echo $response;
}

// Close cURL session
curl_close($ch);
?>

<container_ip>
替换为 Docker 容器的实际 IP 地址。您可以通过运行
docker inspect <container_name>
docker inspect <container_id>
来找到此 IP 地址,具体取决于您命名或标识容器的方式。在输出中查找
"IPAddress"
字段。使用该 IP 地址代替
<container_ip>
变量中的
$url

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