简体   繁体   English

如何使用 docker 搭建 NodeJs 开发环境?

[英]How to set up a NodeJs Development environment with docker?

我正在尝试在 docker 中设置一个 nodejs 开发环境,我还希望热重载和源文件在本地和容器中同步,任何帮助都适用,谢谢

这是一篇关于在开发环境的 docker 容器中热重载源文件的好文章

source files to be in sync in both local and container源文件在本地和容器中同步

To achieve that you basically just need to mount your project directory to your container, as says the official documentation .要实现这一点,您基本上只需要将您的项目目录挂载到您的容器中,如官方文档所述 For example:例如:

docker run -v $PWD:/home/node node:alpine node index.js

What it does is:它的作用是:

  • It will run container based on node:alpine image;它将运行基于node:alpine镜像的容器;

  • node index.js command will be executed as the container is ready; node index.js命令将在容器准备就绪时执行;

  • The console output will come from the container to your host console, so you could debug things.控制台输出将来自容器到您的主机控制台,因此您可以进行调试。 If you don't want to see the output but return control to your console, you could use flag -d .如果您不想看到输出但将控制权返回给您的控制台,您可以使用标志-d

  • And, the most valuable thing is that your current directory ( $PWD ) is fully synchronized with /home/node/ directory of the container.而且,最有价值的是您的当前目录 ( $PWD ) 与容器的/home/node/目录完全同步。 Any file update will be immediately represented at your container files.任何文件更新都会立即显示在您的容器文件中。

I also want hot reloading我也想热加载

It depends on the approach you are using to serve your application.这取决于您用于为应用程序提供服务的方法。

For example, you could use Webpack dev server with a hot reload setting.例如,您可以使用带有热重载设置的Webpack 开发服务器 After that, all you need to map a port to your webpack dev server's port.之后,您只需要将端口映射到 webpack 开发服务器的端口即可。

docker run \
  -v $PWD:/home/node \
  -p 8080:8080 \
  node:alpine \
  webpack-dev-server \
    --host 0.0.0.0 \
    --port 8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM