I wanted to use docker for a personal project I set up it’s written in go few services. So at the beging my "architecture" was spliting each service in diferent folder each service has it’s own main which starts the srvice initialize what it needs opens a specific port for it. So far so good. So I wanted to use docker so when i started reading about it I wanted to introduce dockerfile for each service, but since I used combined go.mod file the docker image could not build.
So my question is how is the right architecture for these kind of stuff, should I introduce new go.mod file for each service, or somehow use combined go.mod which is for the entire project, but somehow split all services to be deployed seperately having their own ports and stuff. I am NEW to docker so I am looking for suggestion how this kind of stuff should be build?
I was looking into video of some guy explaining that every service should have it’s own go.mod, but I was looking into some open source projects and it looked to me that they use only one go.mod file and somehow still deploy services as different services.
2
Answers
It depends the way you want to build and deploy it. In my company we use a mono repo with different services inside cmd folder using the same go.mod file.
Docker compose file:
This project is using https://github.com/cortesi/modd which is a flexible developer tool that runs processes and responds to filesystem changes and you can benefit of having hot reload when developing locally with docker but we are using https://github.com/cosmtrek/air for new projects.
I hope I could have helped you. I’ll keep looking this thread and see if you could solve your issue.
Good luck.
You can use single go.mod file and still build different services executables for docker.
Single go.mod makes sense if services are related more like a single project than a independent releaseable module.
By specifying the main file name instead of . or ./… you will be able to build the executables for the different services
e.g. for service dir as
Dockerfile for m1 can be as below, similarly for m2.
Dockerfiles can be in the m1, m2 folders or you can use a common Dockerfile with build-args to specify the main.go path and output file name.