Sachith Muhandiram
Sachith Muhandiram

Reputation: 2972

Communicating multiple containers running 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

Upvotes: 0

Views: 1358

Answers (1)

MaartenDev
MaartenDev

Reputation: 5811

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/

Upvotes: 2

Related Questions