-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you handle code sharing between different services in the same monorepo? #3
Comments
@itamaro This is an amazing question! Whenever I start a new project or altering infrastructure - I prefer to not share at all. Even if there are similar parts - they are just copied/manually merged between services. This allows you to easy experiment within any given service without breaking others. If changes worked well for one service, they are back copied to others manually. Now let me go back to your question. There are few ways I've used for different projects:
We personally use approach #1 at the moment. Our Also if you do use Node.JS, you can easily connect Hope this helps! |
@anorsich thank you for sharing! Couple of thoughts. First, regarding the "not sharing at all" approach for the beginning - while the thought of duplication and maintenance overhead makes me shiver, I can see how it can work reasonably well in a single-contributor environment. At least from the collaborative monorepos I'm familiar with, I find it difficult to imagine how it could work without significant "forking" between the different copies. The 2 solutions you present definitely make sense. As anything, I see pro's and con's, and I wonder if we can get to an ultimately better solution, when compared to a monolithic application, where it is trivial to have one Some issues that bothered me when attempting the solutions you described (in no particular order):
I think this is a real pain-point in the overly-simplistic approach that Dockerfiles force, and real deep solutions are needed... We're actually building internal tooling to make it better for us. Maybe it can be relevant to others as well :-) |
@itamaro you describe the exact problem I'm having -- I'm experimenting with changing the build context of a service to be the root of the monorepo so I can copy over the |
@richardscarrott I still use |
@richardscarrott not a perfect solution, but something that worked for us pretty well. we wrote a build tool, YaBT, that is essentially a glorified Dockerfile generator that understands semantics of both internal (e.g. shared code) and external (e.g. apt / pip / npm packages) dependencies, and allows you to describe your artifacts in a DAG (you can see examples under the tests dir). it has good support for Python & C/C++ monorepos with external dependencies based on a bunch of package management frameworks, parallel building & target caching (of artifacts as well as test execution). you're welcome to take it for a spin, and let me know how it works for you (or submit issues as to how it isn't :-) ). worth mentioning also Bazel, which may work for your needs (it didn't for ours). also worth mentioning Buck, which may also work for you, but probably doesn't (because there's no support for Docker images). YaBT shares a similar DSL with both of these, and maybe they can even coexist under the same project. |
@itamaro Have you found an elegant solution for this? |
@cerino-ligutom the build tool I mentioned in the previous comment is still being used successfully :-) |
I recently found an elegant solution and thought I'd share! In March of this year, Docker Compose released version 2.17.0 which supports specifying multiple build contexts.
I can now have the primary build context be a service-specific folder, and add a common folder as a named context passed in to the Dockerfile. It's super neat. The ...
services:
...
server:
...
build:
context: server
dockerfile: Dockerfile
additional_contexts:
- shared=common
...
...
... And my ...
COPY . .
COPY --from=shared . ../common
... This copies all of my source files from my My folder structure:
And that's it! I think it's pretty clean. |
It doesn't come up in this sample repo, but I'm assuming that in a large enough project you would have several top-level "common" directories, meant to be shared between two or more services.
With every service having its own Dockerfile under its sub-dir, it wouldn't be possible to include also the required common dirs straight from the repo, since Docker doesn't allow referencing the filesystem from outside the "build context" (e.g.
../common
).Do you have an elegant solution for this use-case?
The text was updated successfully, but these errors were encountered: