Getting error while starting the docker container. I am using nodemon to listen to the file changes.
DockerFile
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm","run","serve"]
Package.json
{
"dependencies": {
"express": "*",
"nodemon": "*"
},
"scripts": {
"serve": "nodemon index.js",
"start": "node index.js"
}
}
build command
docker build -f Dockerfile.dev -t test/nodeapp1 .
cmdLine docker cmd ->
docker run -p 3000:8080 -v /app/node_modules -v pwd:/app test/nodeapp1.
Iam new to docker, and not able to figure out the cause.
Make this changes in your dockerfile
FROM node:alpine
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV HOME=/home/node/app
ENV PATH="/home/node/.npm-global/bin:${PATH}"
USER node
RUN npm install -g nodemon
RUN mkdir -p ${HOME}
WORKDIR ${HOME}
ADD package.json ${HOME}
RUN cd ${HOME} && npm install
CMD [ "npm" ,"run", "serve" ]
Build the docker container
docker build -f Dockerfile -t prac/nodeApp .
Run the docker container
docker run -p 3000:8080 -v /app/node_modules -v pwd:/app prac/nodeApp
Changing the WORKDIR to a new value worked.
FROM node:alpine
WORKDIR '/dir'
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm" ,"run", "serve" ]
Your docker run -v
options are wrong. You probably actually meant to write
docker run ... -v $PWD:/app ...
docker run ... -v $(pwd):/app ...
to use the current directory (from the PWD
environment variable or from the pwd
command, respectively) as a bind mount .
I tend to not recommend this pattern, especially for Node applications where the host dependencies are minimal and you're not interacting much with other containers. It's probably easier to just install Node locally (if you don't already have it) and do live development against that; when you want to use Docker to deploy your application, use the version you've COPY
ed into the image, and don't separately use a -v
option to inject your code over it.
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.