I’m having trouble with my Express API in a Docker container. It starts up fine on port 5000, but then crashes with the following error message:
[nodemon] Internal watch failed: Circular symlink detected: "/usr/bin/X11" points to "/usr/bin"
npm ERR! code 1
npm ERR! path /
npm ERR! command failed
npm ERR! command sh -c nodemon server.js
My Dockerfile is structured like this:
FROM node:latest
WORKDIR /
COPY package*.json ./
RUN npm install
COPY . .
ENV port=5000
EXPOSE 5000
CMD ["npm", "start"]
I followed Docker’s documentation when creating it. Does anyone have insights into what might be causing this error or suggestions on how to adjust the Dockerfile?
hey there, i’ve seen this before. it’s prolly cuz ur using nodemon in the container. try switching to node server.js
instead of npm start in ur CMD. nodemon can be finicky with file watchers in docker. also, consider using a specific node version instead of latest for better stability
I encountered a similar issue when containerizing my Node.js app. The root cause is often related to how Docker handles file system operations, especially with symlinks. Here’s what worked for me:
- Change your WORKDIR to /app instead of root (/).
- Use a specific Node version rather than ‘latest’ for consistency.
- Run the app directly with ‘node’ instead of ‘npm start’ to avoid nodemon issues.
Modified Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT=5000
EXPOSE 5000
CMD ["node", "server.js"]
This setup should resolve the symlink problem and provide a more stable environment for your Express API in Docker. Remember to adjust your code if it relies on the root directory structure.
The issue is likely due to Docker’s handling of symlinks and file watching rather than a problem with Express itself. One approach is to change the working directory from root to a dedicated folder such as /app
. Running the server using the node
command directly instead of via npm start
can also help bypass issues with file watchers. Additionally, using a non-root user improves security and may reduce unexpected behavior in Docker. Consider a Dockerfile that sets WORKDIR to /app
, installs dependencies, copies your code, and finally runs node server.js
.