npm is a software package manager for JavaScript programming language. npm makes it easy for JavaScript developers to share the code they write. npm also provides a command-line interface to manage the dependencies in a project. Docker is a containerization platform that allows developers to package their applications and dependencies into a portable image.
npm with Docker makes it easy to package and ship Node.js applications. npm with Docker also enables developers to share their code easily. npm with Docker is an excellent tool for JavaScript developers who want to share their code with others.
Dockerfile for npm start
npm start is frequently used command to run a node application like: Reactjs. Use can use the below Dockerfile for running node applications with Docker.
Create a file named Dockerfile in the project base directory and add the below code.
FROM node:16-alpine RUN mkdir /app WORKDIR /app COPY package.json /app RUN npm install COPY . /app EXPOSE 3000 CMD ["npm", "start"]
Make sure to change the value of EXPOSE to the port application runs on. Also assuming that your application runs with npm start
command.
Now, build a docker image for your application. In a terminal, run the following command from the application base directory.
docker build -t image-name .
Once the image build is completed, you can run your application.
sudo docker run -it -d image-name
That’s it.