How to Call 'npm start' though Docker ?
Last Updated :
14 Jun, 2024
Docker is a powerful tool for creating, deploying, and running applications inside containers. If you have a Node.js application, you might want to run it inside a Docker container to ensure consistency across different environments. This article will guide you on how to call npm start
through Docker to run your Node.js application.
It runs the application in a kind of isolated environment called container. A container is lightweight and contains everything needed to run the application. Multiple containers can run on the same host, and they can also share data between them securely.
Steps to Setup the Application
Step 1: Create a React application using the following command.
npx create-react-app docker-react
Step 2: After creating your project folder(i.e. docker-react), move to it by using the following command.
cd docker-react
Step 3: Create a file named Dockerfile in the root of your app.
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.19.2",
}
Create a Dockerfile
A Dockerfile
is a script that contains a series of instructions on how to build a Docker image for your application. Create a Dockerfile
in your project directory with the following content:
FROM node:alpine
RUN mkdir /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm", "start"]
Explanation:
- First, we load the base image node:alpine which is a lightweight Linux distribution with a node installed in it.
- Then we copy the package.json file which contains the app's dependencies into our working directly.
- And then we install the dependencies and copy the project files into our working directory.
- Finally, we run the command npm start.
Creating docker image:
Run the following command from the project's root directory. Make sure your docker daemon is running.
docker build -t <yourname/projectname> .
Steps to run the application:
Run the application using following command from the root directory of your folder.
docker run -d -it -p 3000:3000 <yourname/projectname>
Here we need to map our localhost port to the one of the container which in our case is 3000.
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.

Conclusion
Running npm start
through Docker allows you to encapsulate your Node.js application in a container, ensuring that it runs consistently across different environments. By following the steps in this article, you can build and run a Docker container for your Node.js application, leveraging the power of Docker for development, testing, and deployment. This approach enhances the portability and scalability of your application.
Similar Reads
How to NPM Run Start At The Background?
Running npm start or npm run start in the background is one of those things that may not realized until you are knee-deep in a project. Whether you are building a web application, working with microservice, or just trying to keep your terminal free from having a server run without locking your termi
3 min read
How to Start or Run Docker Daemon
Docker has had a significant impact on the development, shipping, and deployment of applications. By using containerization, Docker allows a developer to package an application along with all the dependencies into standard units termed containers. This way, it makes sure the application behaves unif
6 min read
How to Install Docker on CentOS?
Quick Preview to Install Docker on CentOS:Installation of Docker on CentOS:Open CentOS Terminal.Execute the command yum update in the Root Menu.Run the command yum install -y yum-utils device-mapper-persistent-data lvm2.Execute the command yum-config-manager --add-repo https://download.docker.com/li
4 min read
How to Install Docker on Debian?
Docker Service Product is the essential tool used for the development purpose of any software where the said software needs to be passed through different development phases. The Installed Docker Service makes Operating System-Level Virtualization to create Docker Containers. Docker can easily be in
4 min read
How to Create Docker Image?
Docker is a powerful containerization tool that enables developers to package their applications and their dependencies into a single unit called Docker image. The Docker image offers seamless deployment, scalability, and portability. In this article, I will make sure that you understand what is doc
12 min read
How To Install Docker On AWS EC2 ?
You can use the following instructions to install Docker on an AWS EC2 instance. Note that depending on the Linux distribution your EC2 instance is running, there may be differences in certain commands or processes. What is Docker?Docker is an OS-level virtualization that provides tools for building
3 min read
How to Install and Run Jekyll on Docker?
Jekyll is an open-source software used to develop websites. Jekyll is a free application present on the internet. The word open source refers that the application being free to use. Jekyll is the application used to develop static websites. Jekyll generally takes the text in the user's favorite mark
4 min read
How to Install Docker on MacOS?
Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to Create a Dockerfile in Node.js ?
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is something like a shellscript that gathers multiple commands into a single document to fulfill a single task. Prerequisite: Before you can Dockerize any application, you ne
4 min read
How to Install Docker on Amazon Linux
Docker is a tool which helps to automate the deployment of applications in containers to make sure that particular applications can work efficiently in different environments without any errors.It helps developers to build, ship, and run application faster and more reliable way. In this article, we
3 min read