php小編魚仔發現許多開發者在使用docker-compose時遇到了一個常見的問題,即無法存取Go REST API。這個問題可能導致開發者無法正常進行API的測試和偵錯工作。在本文中,我們將分享一些解決問題的方法和技巧,幫助開發者順利使用docker-compose存取Go REST API,提高開發效率。
問題內容
我正在嘗試使用 gin gonic 和 mysql 以及 phpmyadmin 和 docker 運行一個簡單的 go rest api。
雖然 phpmyadmin 運作正常,但當我嘗試存取我的 go api 時,我收到以下錯誤訊息:localhost 未發送任何資料。 err_empty_response
#如果我在沒有 docker 的情況下運行 main.go
文件,一切都會按預期工作
這是我的 docker-compose
檔案
version: '3' services: app: container_name: full_app build: . ports: - 8080:8080 expose: - 8080 restart: on-failure environment: - pma_host=fullstack-mysql - db_name=${db_name} - db_user=${db_user} - db_password=${db_port} volumes: - .:/usr/src/app/ depends_on: - fullstack-mysql networks: - fullstack fullstack-mysql: image: mysql:5.7 container_name: full_db_mysql ports: - 3306:3306 environment: - mysql_root_host=${db_host} - mysql_user=${db_user} - mysql_root_user=${db_root_user} - mysql_password=${db_password} - mysql_root_password=${db_root_password} - mysql_database=${db_name} - mysql_root_password=${db_password} volumes: - database_mysql:/var/lib/mysql networks: - fullstack phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin_container depends_on: - fullstack-mysql environment: - pma_host=fullstack-mysql - pma_user=${db_user} - pma_port=${db_port} - pma_password=${db_password} ports: - 9090:80 restart: always networks: - fullstack volumes: api: database_mysql: # networks to be created to facilitate communication between containers networks: fullstack: driver: bridge
這是我的 dockerfile
:
# start from golang base image from golang:alpine as builder # env go111module=on # install git. # git is required for fetching the dependencies. run apk update && apk add --no-cache git # set the current working directory inside the container workdir /app # copy go mod and sum files copy go.mod go.sum ./ # download all dependencies. dependencies will be cached if the go.mod and the go.sum files are not changed run go mod download # copy the source from the current directory to the working directory inside the container copy . . # build the go app run cgo_enabled=0 goos=linux go build -a -installsuffix cgo -o main . # start a new stage from scratch from alpine:latest run apk --no-cache add ca-certificates workdir /root/ # copy the pre-built binary file from the previous stage. observe we also copied the .env file copy --from=builder /app/main . copy --from=builder /app/.env . # expose port 8080 to the outside world expose 8080 #command to run the executable cmd ["./main"]
這是我的 go main.go
檔案(目前):
package main import ( "github.com/gin-gonic/gin" "log" "net/http" ) func main() { router := gin.Default() router.GET("/", func(context *gin.Context) { context.JSON(http.StatusOK, gin.H{"data": "Hello World !"}) }) router.Run("localhost:8080") }
如果我訪問 http://localhost:9090
phpmyadmin 正在加載(正是我所期望的)
如果我訪問 http://localhost:8080
我收到此錯誤訊息:localhost 未發送任何資料。 err_empty_response
#我為此運行 docker-compose up --build
。
我顯然做錯了什麼,但我不確定是什麼。
解決方法
問題似乎在於您的 go 程式碼以及我為 api 指定偵聽主機的方式。在我的main.go
檔案中,我目前將主機設定為“localhost:8080”,但是當在docker 容器內執行我的應用程式時,我應該監聽“0.0.0.0:8080” 。這允許容器綁定到所有網路介面。
要解決此問題,我必須修改 main.go
檔案中的 router.run
行,如下所示:
router.Run("0.0.0.0:8080")
進行此更改後,我重建了 docker 映像並使用 docker-compose up --build 再次運行容器。這應該允許在 docker 容器外部的 http://localhost:8080
上存取我的 go rest api。
注意:容器內的localhost
指的是容器本身,而不是宿主機。透過使用“0.0.0.0”,我指示容器綁定到所有可用的網路接口,從而允許我從主機訪問 api。
以上是使用 docker-compose 無法存取 Go REST API的詳細內容。更多資訊請關注PHP中文網其他相關文章!

golangisidealforperformance-Critical-clitageAppations and ConcurrentPrompromming,而毛皮刺激性,快速播種和可及性。 1)forhigh-porformanceneeds,pelectgolangduetoitsefefsefefseffifeficefsefeflicefsiveficefsiveandconcurrencyfeatures.2)fordataa-fordataa-fordata-fordata-driventriventriventriventriventrivendissp pynonnononesp

Golang通過goroutine和channel實現高效並發:1.goroutine是輕量級線程,使用go關鍵字啟動;2.channel用於goroutine間安全通信,避免競態條件;3.使用示例展示了基本和高級用法;4.常見錯誤包括死鎖和數據競爭,可用gorun-race檢測;5.性能優化建議減少channel使用,合理設置goroutine數量,使用sync.Pool管理內存。

Golang更適合系統編程和高並發應用,Python更適合數據科學和快速開發。 1)Golang由Google開發,靜態類型,強調簡潔性和高效性,適合高並發場景。 2)Python由GuidovanRossum創造,動態類型,語法簡潔,應用廣泛,適合初學者和數據處理。

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

Go語言在並發編程、性能、學習曲線等方面有獨特優勢:1.並發編程通過goroutine和channel實現,輕量高效。 2.編譯速度快,運行性能接近C語言。 3.語法簡潔,學習曲線平緩,生態系統豐富。

Golang和Python的主要區別在於並發模型、類型系統、性能和執行速度。 1.Golang使用CSP模型,適用於高並發任務;Python依賴多線程和GIL,適合I/O密集型任務。 2.Golang是靜態類型,Python是動態類型。 3.Golang編譯型語言執行速度快,Python解釋型語言開發速度快。

Golang通常比C 慢,但Golang在並發編程和開發效率上更具優勢:1)Golang的垃圾回收和並發模型使其在高並發場景下表現出色;2)C 通過手動內存管理和硬件優化獲得更高性能,但開發複雜度較高。

Golang在雲計算和DevOps中的應用廣泛,其優勢在於簡單性、高效性和並發編程能力。 1)在雲計算中,Golang通過goroutine和channel機制高效處理並發請求。 2)在DevOps中,Golang的快速編譯和跨平台特性使其成為自動化工具的首選。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境