I have a hello, world express app that I run with nodemon
so that whenever I change a file the server restarts.
I am packaging the app as a docker image.
Dockerfile:
FROM node:6.3.0
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY . /opt/app
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]
npm start
runs nodemon -L index.js
as defined in package.json
However, I can't build the image everytime I make a change to the my app.
I also have a docker-compose.yml file, in which I specify the build
option, but still I can't get the container to reload with
version: "2"
services:
rpro:
build: .
command: npm start
ports:
- "3000:3000"
volumes:
- /opt/app
Here the the entire repo
How do I achieve server restart on file changes with docker containers? What is the usual dev workflow in such a case?
Not sure if I would call that a problem since it is the usual workflow -- when a file changes that resides in the container, it will trigger a rebuild. If you don't put the file in the container, it won't do that so as @jatin mentions, you can link your Compose directory via - .:/opt/app
or similar.
If you do this, you don't need to use COPY . /opt/app
in your build as it will mount over that directory.
Note when you docker-compose up --build
Docker will not rebuild (restart the server) if the files in the mounted volume change, only if files in the build change. Since it seems you are watching the files via nodemon, it may restart (since you are using the legacy watch it should, just note there are cases where it may not recognize the file change).
This is basically fine for development, however in production, clearly you wouldn't want to use watch
and you most likely would use Docker to monitor and restart the container if it quits (restart: always
) or use a process manager in non-daemon mode.
Ah, the problem seems to be with docker-compose.yml volumes
param.
Changing it to
volumes:
- .:/opt/app
makes it work. It tells docker to mount the application folder .
on the host to the /opt/app
in the container.
Then after doing docker-compose up
, the server restarts in case of file changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With