卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35556本站已运行397

使用 docker-compose 无法访问 Go REST API

使用 docker-compose 无法访问 go rest api

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。

卓越飞翔博客
上一篇: Gin Web 应用程序仅渲染一个模板
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏