Nodemon
At this point we probably want to install the Nodemon tool in our backend.
Install Nodemon
First, we’ll get a terminal inside of the container
# Terminal
docker exec -it project-server bashThen we can use the usual process to install Nodemon using npm. Make sure this is done inside of the /app/server directory:
# Docker project-server Terminal
npm install nodemonOnce Nodemon is installed, we can close the terminal connected to the container using the exit command.
Tip
This same basic process can be used to install any library using npm inside of the container.
Update Dockerfile
To use Nodemon inside of our container, we need to update the scripts section of the package.json file for the server:
/* server/package.json */
{
/* other contents */
"scripts": {
"dev": "nodemon ./bin/www",
"start": "NODE_ENV=production node ./bin/www"
},
/* other contents */
}This adds a new dev command that uses Nodemon to run the server in development mode, and also changes the start command to run the server in production mode.
We also need to update the CMD entry in the server’s Dockerfile to use the new dev command:
# server/Dockerfile
# For running in development mode only
FROM node:18
WORKDIR /app/server
COPY package*.json ./
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "dev"]With those changes, we need to rebuild and restart our containers:
# Terminal
docker compose up -d --buildThe --build will build any changed containers.