Is there a way to set up multiple Local Go Modules, so they can run inside one Docker container without pulling from Github?

Published

So my goal is to set up a local development environment with multiple Go services (all as independent modules) running in multiple docker containers and one Go module, which all services can use to start a new service with connection to the database etc. without repeating this code in every module/service.

So my structure would look something like that (in $GOPATH/src/github.com/name/backend/):

|--services
|  |--service1
|     |--Dockerfile
|     |--main.go
|     |--go.mod
|  |--service2
|     |--Dockerfile
|     |--main.go
|     |--go.mod
|--serviceHelper
|  |--serviceHelper.go
|  |--go.mod

My Dockerfile currently is just a plain Dockerfile for Go:

FROM golang:alpine AS build-env
WORKDIR /backend
ADD . /backend
RUN cd /backend && go build -o service1

FROM alpine
RUN apk update && 
   apk add ca-certificates && 
   update-ca-certificates && 
   rm -rf /var/cache/apk/*
WORKDIR /backend
COPY --from=build-env /backend/service1 /backend
EXPOSE 8080
ENTRYPOINT ["./service1"]

My go.mod file also is just:

module github.com/name/backend/services/service1

go 1.17

The problem I’m having now though is that you either have to pull a module from a github repository, which I don’t want to do, or put the serviceHelper code in every single module for the services, which I don’t want to do either.

I work with VSCode and have since learned that you have to put single modules into single workspace folders. Still I can’t manage to configure the modules locally to import both normal packages like github.com/gorilla/mux and also my local package in one service. I work with an Apple M1, I hope that might not cause problems as well.

How do I need to configure the go.mod files, the Docker files and the Go imports so that I can both debug Go normally in the editor (i.e. that the serviceHelper module is not just loaded into the Docker container directly), and also run everything locally without having to source the serviceHelper from github?

Source: Docker Questions

Published
Categorised as docker, go, import, local, module Tagged , , , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq