侦听更改并在代码更改时重新加载容器-docker-compose [英] Listen to changes and reload container on code change - docker-compose
问题描述
我在Visual Studio 2019中将docker-compose与运行Linux容器的Windows的docker结合使用。我想为有角度的客户端应用程序启用热重装。
I am using docker-compose in visual studio 2019 with docker for windows running linux containers. I want to enable hot reload for the angular client app.
我修改了运行该应用程序的npm命令,使其包含以下民意调查:
I modified the npm command running the app to include poll like:
"docker-start": "ng serve --host 0.0.0.0 --port 4200 --proxy-config proxy-conf.json --poll 1"
,然后在docker-compose中添加一个卷,如下所示:
and added a volume into docker-compose like so:
volumes:
- ./ClientApp:/app/
还公开了webpack端口
also additionally exposing the webpack port
ports:
- 4200:4200
- 49153:49153
docker-compose文件位于仓库的根目录,而角度应用位于/ ClientApp文件夹。这使得每次我导航到localhost:4200时应用程序都无法获得GET\。如果我注释掉了卷映射,该应用程序将开始工作,但重新加载不会。我希望它每次更改任何前端代码时都听代码中的更改并根据需要更新容器。
docker-compose file is at the root of the repo and the angular app is in the /ClientApp folder. This makes the application throw cannot GET\ every time I navigate to localhost:4200. If I comment out the volume mapping, the application starts working but the reload does not. I would like it to listen to changes in the code and update the container as needed every time I change any frontend code.
整个dockerfile:
Entire dockerfile:
FROM node:9.6.1
RUN mkdir -p /app
WORKDIR /app
EXPOSE 4200
EXPOSE 49153
ENV PATH /app/node_modules/.bin:$PATH
COPY . /app
RUN npm install --silent
RUN npm rebuild node-sass
CMD ["npm", "run", "docker-start"]
推荐答案
使用代码代码时,使用nodemon自动重启节点服务器变了。在该操作之前,请在您的docker映像中安装nodemon并确保它存在。
Use nodemon to automatically restart the node server when code is changed . Before that install nodemon in your docker image and make sure it is present.
引用此URL来安装nodemon: https://www.npmjs.com/package/nodemon
Refer this URL to install nodemon :https://www.npmjs.com/package/nodemon
然后更改您的CMD Dockerfile
then change your CMD in Dockerfile
CMD ["nodemon", "--exec", "npm", "run", "docker-start"]
只要更改代码,这就会重新加载nodejs应用程序
This is reload your nodejs application whenever codes are changed
这篇关于侦听更改并在代码更改时重新加载容器-docker-compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!