How to host NodeJs app on Docker Container

Daniel Mesizah
1 min readJan 5, 2023

To host a Node.js app in a Docker container, you will need to create a Dockerfile that specifies how to build the Docker image for your app. Here is an example Dockerfile that you can use as a starting point:

FROM node:latest
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]

This Dockerfile uses the latest version of the Node.js base image and creates a working directory for your app. It then copies your package.json and package-lock.json files (if you are using npm) to the working directory and installs the dependencies for your app. Finally, it copies the rest of your app's source code to the working directory and exposes port 8080.

To build the Docker image for your app, run the following command in the same directory as your Dockerfile:

docker build -t my-node-app .

This will build a Docker image for your app and tag it with the name “my-node-app”.

To run your app in a Docker container, use the following command:

docker run -p 8080:8080 my-node-app

This will run a Docker container based on the “my-node-app” image and map port 8080 on the host to port 8080 in the container. Your app will be accessible at http://localhost:8080.

--

--

Daniel Mesizah

Coder who likes to share what he knows with the rest of the world