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