nx-tools
nx-tools copied to clipboard
NextJs e nx-tools-docker
Hi guys, i like run my projects nextjs in my workspace with docker. What would be the best way?
Problem:
- node_modules large size to copy for every application
@gilvaju So what we do at work is we first build the application completely outside of Docker using the standard Nx build system. This allows reusing library builds and computational caching. And as part of the build step we put the Dockerfile
in the dist
via the assets
option.
After we have everything in assets we use nx-docker
copy the compiled Javascript into a docker image.
As part of that we do an npm install
inside the Docker image using the generated package.json
that came from the @nrwl/node:webpack
executor which only list modules that we actually used.
The one gotcha is we have to use "buildLibsFromSource": true,
to inline the library modules into the bundle of the server.
So we aren't really getting the benefit of the libraries being cached and built separately right now, but we have plans to change the webpack config to cover that case.
Overview:
- Configure a Docker file and add it to the
assets
- Build nestjs project with
@nrwl/node:webpack
- Point
@nx-tools/nx-docker:build
at the dist location of the Docker file - Build the docker image from the compiled server
- Run npm install inside the image using the package.json generated from
generatePackageJson
@gilvaju Please check this repository, it's a sandbox with nest, react, next and angular application and all of them use the nx-docker plugin to generate the docker images
@gperdomor so if we have multiple nest apps and a common library. If we modify common lib would docker rebuild all apps?
@kodeine yes... you can run npx nx affected --target=docker
(or the target which you created in the project.json)
@gilvaju Please check this repository, it's a sandbox with nest, react, next and angular application and all of them use the nx-docker plugin to generate the docker images
I'll see the example and then give feedback
@gilvaju Please check this repository, it's a sandbox with nest, react, next and angular application and all of them use the nx-docker plugin to generate the docker images
Hello, now that we had time to study the repository you indicated.
Anyway thanks for the link
I was only able to run the command after installing @swr
It built perfectly and so I have the image to be used as I want
In that case, can I use "executor": "nx:run-commands" to run these new/updated images?
I noticed that this package is a peer here. https://github.com/nx-tools/nx-tools/blob/efec6c8d3fa1946f69941c4eddadc99aa6a18bbd/packages/docker-metadata/package.json#L24
This was introduced in this commit. https://github.com/nx-tools/nx-tools/commit/27b73ccb9615e27ffb00ae2794cad1f1407fb387
I wonder if there is an expectation of another package providing the deposit for @swc/helpers
that does not exist in your stack? Maybe @nrwl/angular
or @nrwl/web
or something like that?
Perhaps this should be moved to a dependency OR perhaps the readme should include @swc/helpers as a required peer along side tslib
and @nrwl/devkit
Perhaps this package manager specific? I know nom has automatic peers, but others (yarn 1) may not.
In that case, can I use "executor": "nx:run-commands" to run these new/updated images?
After you have the image, getting it running is another story.
I have been creating my own executors using dockerode
, perhaps I will create a library with that work some day.
As a simple alternative you can use the run-commands
executor as you mentioned to either use docker or docker-compose to start up the image locally.
The current scope of this library ends at building the image and pushing it to a registry.
There is an open feature request for a executor for running images. https://github.com/nx-tools/nx-tools/issues/12
If you have further comments or ideas about that feature you may add those there.
After you have the image, getting it running is another story. I have been creating my own executors using
dockerode
, perhaps I will create a library with that work some day. As a simple alternative you can use therun-commands
executor as you mentioned to either use docker or docker-compose to start up the image locally.The current scope of this library ends at building the image and pushing it to a registry.
Hmm, I understand
We currently have an ecosystem of about 20 containers, some apps on nx and other services like postgres and mongo.
I believe if it is a way to play these other services for apps tb and use nx-tools to build the images and also use run-commands to run the images (for postgres and mongo for example I would create apps containing only project. json and the Docker file).
It will also be used with github actions
All these images are now served all on just one EC2 with haproxy as a reverse proxy. In the future we have plans to migrate to the container manager or something more complex like kubernets.
Would it be a good way?
There is an open feature request for a executor for running images. #12
If you have further comments or ideas about that feature you may add those there.
Cool, I would like to help, what would be the way? I think I can contribute something.
Im sure PRs are welcome. Maybe tag @gperdomor with a comment in issue #12 with your initial ideas and then create a draft PR? I would think we would add a run executor or something, but I am sure there are a few ways to solve the problem.
Is there an example of how to use this with docker-compose
?
After running a docker-compose file I got the following error: Error: Cannot find module '/usr/src/app/main.js'
+1
I would also like to know how to use docker compose
with nx-container
!