dreamworld2007 2019-08-25 14:08
浏览 64
已采纳

通讯运行golang的多个容器

I am trying to make to simple micro service using golang and Docker

I dont understand how to call add.go functions from mainmodule.

This post does the same, but it uses github repo and build it.

I am new to both golang and Docker, so any suggestion to make this simple app working would be great.

This is my folder structure.

- addmodule
    - add.go
    - Dockerfile
- mainmodule
    - main.go
    - Dockerfile

- docker-compose.yml

Dockerfile in addmodule

FROM   golang:alpine
RUN apk update
COPY . /go/src
WORKDIR /go/src
RUN go build -o add .
EXPOSE 7070
RUN chmod 755 add
CMD [ "./add" ]

Dockerfile in mainmodule

FROM golang:alpine
RUN apk update
COPY . /go/src
WORKDIR /go/src
CMD [ "go","run","main.go" ]

docker-compose.yml

version: '3'

services:
  addmodule:
    image: addmodule
    build:
      context: ./addmodule
      dockerfile: Dockerfile
    depends_on:
      - mainmodule

  mainmodule:
    image : mainmodule
    build:
      context: ./mainmodule
      dockerfile: Dockerfile

My add.go

package main

import (
    "log"
    "net/http"
)

func add(reswt http.ResponseWriter, req *http.Request) {
    log.Println("Request came to here")
}

func main() {
    http.HandleFunc("/add", add)
    http.ListenAndServe("0.0.0.0:7070", nil)

}

My main.go in mainmodule

package main

import (
    "log"
    "net/http"
)

func main() {
    log.Println("From main package")

    res, err := http.Get("0.0.0.0:7070/add")

    if err != nil {
        log.Println("couldnt send get request")
    }

    log.Println(res)

}

When I run sudo docker-compose up --build , I just get couldnt send get request

PS : Question edited with modified codes according to suggestions given by @MaartenDev

  • 写回答

1条回答 默认 最新

  • drv13270 2019-08-25 14:21
    关注

    Docker compose creates a network by default. The service names can be used to reach the contains in the same network.

    version: '3'
    
    services:
      addmodule:
        image: addmodule
        build:
          context: ./addmodule
          dockerfile: Dockerfile
        depends_on:
          - mainmodule
    
      mainmodule:
        image : mainmodule
        build:
          context: ./mainmodule
          dockerfile: Dockerfile
    

    And the mainmodule would be able to reach the add module by connecting to http://addmodule:<port> and addmodule can reach mainmodule by: http://mainmodule

    This assumes these are http services. Any other transport will work because the addmodule and mainmodule are made available using DNS.

    When using custom ports ensure that the ports are exposed in your Dockerfile:

    FROM   golang:alpine
    RUN apk update
    COPY . /go/src
    WORKDIR /go/src
    
    EXPOSE 7070
    

    Your http should bind to 0.0.0.0 instead of localhost. This ensures that the service is reachable from outside the container:

    http.ListenAndServe("0.0.0.0:7070", nil)
    

    The main code should use the following:

    package main
    
    import (
        "log"
        "net/http"
    )
    
    func main() {
        log.Println("From main package")
    
        res, err := http.Get("http://addmodule:7070/add")  // The mistake I made after a long chat. forgot http://
    
        if err != nil {
            log.Println("couldnt send get request")
        }
    
        log.Println(res)
    
    }
    

    Read more about the default network: https://docs.docker.com/compose/networking/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端