我在 Docker 容器內有一個用 PHP/Symfony 構建的 api。我想測試一下。要做到這一點 :首先:我進入我的容器:docker-compose exec da-invoicing-php sh第二:我運行測試:vendor/bin/simple-phpunit在我的測試中,我有這個要求:$result = $this->client->request( 'POST', '10.110.167.124:8080/api/v1/course_invoices', [ RequestOptions::HEADERS => [ 'Accept' => 'application/ld+json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$this->token}", ], RequestOptions::BODY => json_encode([ 'courseInstanceId' => self::COURSE_INSTANCE, ]), ] );如您所見,我向端點“'10.110.167.124:8080/api/v1.....”請求。它有效,但我知道我不能這樣繼續下去。我嘗試使用“localhost”、“localhost:8080”、“http://localhost”等...但沒有成功。我總是有這個錯誤:GuzzleHttp\Exception\ConnectException:cURL 錯誤 7:無法連接到 localhost 端口 8080:連接被拒絕(請參閱https://curl.haxx.se/libcurl/c/libcurl-errors.html)那么如何在容器內進行這個測試呢?
1 回答

隔江千里
TA貢獻1906條經驗 獲得超10個贊
你不能在你的 PHP 容器中直接使用 localhost 它不提供 HTTP,它是 php-fpm。
您必須調用 nginx 容器 da-invoicing-api
$result = $this->client->request(
'POST',
'da-invoicing-api/api/v1/course_invoices',
[
RequestOptions::HEADERS => [
'Accept' => 'application/ld+json',
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$this->token}",
],
RequestOptions::BODY => json_encode([
'courseInstanceId' => self::COURSE_INSTANCE,
]),
]
);
Docker-compose為您的 compose 文件中的每個容器創建主機別名(同一網絡上的其他容器可以使用該服務名稱)。
- 1 回答
- 0 關注
- 170 瀏覽
添加回答
舉報
0/150
提交
取消