简体   繁体   中英

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 index.js command will be executed as the container is ready;

  • 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 .

  • And, the most valuable thing is that your current directory ( $PWD ) is fully synchronized with /home/node/ directory of the container. 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. After that, all you need to map a port to your webpack dev server's port.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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