Skip to content
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

NextJs e nx-tools-docker #494

Open
gilvaju opened this issue Apr 2, 2022 · 13 comments
Open

NextJs e nx-tools-docker #494

gilvaju opened this issue Apr 2, 2022 · 13 comments

Comments

@gilvaju
Copy link

gilvaju commented Apr 2, 2022

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
@wSedlacek
Copy link
Collaborator

@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:

  1. Configure a Docker file and add it to the assets
  2. Build nestjs project with @nrwl/node:webpack
  3. Point @nx-tools/nx-docker:build at the dist location of the Docker file
  4. Build the docker image from the compiled server
  5. Run npm install inside the image using the package.json generated from generatePackageJson

@gperdomor
Copy link
Owner

@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

@kodeine
Copy link

kodeine commented Apr 17, 2022

@gperdomor so if we have multiple nest apps and a common library. If we modify common lib would docker rebuild all apps?

@gperdomor
Copy link
Owner

@kodeine yes... you can run npx nx affected --target=docker (or the target which you created in the project.json)

@gilvaju
Copy link
Author

gilvaju commented Apr 20, 2022

@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
Copy link
Author

gilvaju commented Jun 9, 2022

@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

@gilvaju
Copy link
Author

gilvaju commented Jun 9, 2022

image

I was only able to run the command after installing @swr

image

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?

@wSedlacek
Copy link
Collaborator

image

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.
27b73cc

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.

@wSedlacek
Copy link
Collaborator

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.
#12

If you have further comments or ideas about that feature you may add those there.

@gilvaju
Copy link
Author

gilvaju commented Jun 9, 2022

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.

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.

@wSedlacek
Copy link
Collaborator

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.

@danyhiol
Copy link

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'

@corbinday
Copy link

+1
I would also like to know how to use docker compose with nx-container!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants