亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CURL 在 Docker 映像中不起作用 [無法訪問 Docker 映像中的主機]

CURL 在 Docker 映像中不起作用 [無法訪問 Docker 映像中的主機]

Go
開心每一天1111 2022-05-18 13:48:40
碼頭工人文件# Start from the latest golang base imageFROM golang:latest# Add Maintainer InfoLABEL maintainer="Sumit Thakur <[email protected]>"# Set the Current Working Directory inside the containerWORKDIR /app# Copy go mod and sum filesCOPY go.mod go.sum ./# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changedRUN go mod download# Copy the source from the current directory to the Working Directory inside the containerCOPY . .# Build the Go appRUN go build -o testapp myapplication.go testapp.go# Expose port 50051 / for internal comunication ENV PORT 50051RUN echo $PORTEXPOSE ${PORT}# Command to run the executableCMD ["./testapp"]構建 Docker 文件docker build -t testapp  -f Dockerfile .這是完美的工作運行 Docker 文件docker run -d -p 50051:50051 testapp這也可以正常工作我檢查正在運行的容器docker ps這會給我CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                      NAMES57cd3c01bcda        testapp           "./testapp"       2 seconds ago       Up 2 seconds        0.0.0.0:50051->50051/tcp   gracious_bhaskara當我檢查網絡檢查時docker network inspect bridge
查看完整描述

3 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

從評論中的討論來看,問題似乎與https握手有關。

一個簡單的解決方案是使用以下 URL 查詢服務:

curl -H "Content-type:application/json" -X GET 'https://localhost:50051/testapp/v1/order' -v -k

使用-k,您將忽略HTTPS驗證。

注意:我已將 URL 從更改httphttps.


查看完整回答
反對 回復 2022-05-18
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

從容器內部,您需要偵聽所有接口,而不是 localhost 或環回接口。網絡是在 docker 中命名的,因此您不僅可以獲得一個新的私有 ip,還可以獲得容器內部一個無法從外部訪問的單獨環回接口。為此,請更改:

http.ListenAndServe("localhost:50051", headersOk)

到:

http.ListenAndServe(":50051", headersOk)


查看完整回答
反對 回復 2022-05-18
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

上提供的代碼main.go不會啟動 TLS,因此請停止使用 curl 來卷曲https,它將無法正常工作。卷曲http會起作用


package main


import (

    "io"

    "net/http"


    "github.com/gorilla/handlers"

)


func main() {

    http.HandleFunc("/testapp/v1/order", testHandler)

    headersOk := handlers.AllowedHeaders([]string{""})

    http.ListenAndServe(":50051", headersOk)

}

func testHandler(w http.ResponseWriter, r *http.Request) {

    io.WriteString(w, "Heyyy!")

}

該命令openssl s_client https://localhost:50051/testapp/v1/order -connect localhost:50051向您確認您請求的端口上不存在 TLS


很多評論確認 usinghttps不相關,再次在您的代碼上未激活 TLS


curl -H "Content-type:application/json" -X GET 'http://localhost:50051/testapp/v1/order'作品。再次確認 不使用 TLS


結論:


https當您的代碼上未激活 TLS 時,不要嘗試 curl


查看完整回答
反對 回復 2022-05-18
  • 3 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號