Node.js - Getting Started with Node.js on Docker

by
Jeremy Canfield |
Updated: July 22 2024
| Node.js articles
Create a file named package.json that contains the following.
{
"name": "my-nodejs-image",
"version": "1.0.0",
"description": "my NodeJS image"
}
Create a file named Dockerfile that contains the following.
FROM node:18-alpine
WORKDIR /src/
COPY package.json /src/
RUN npm install --omit=dev
EXPOSE 12345
On your Docker server, use the docker build command to create the Node.js image.
sudo docker build --file Dockerfile --tag nodejs:18-alpine .
The docker images command should return something like this.
~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nodejs 18-alpine 824127ec51d1 1 minute ago 189MB
The docker run command can be used to create and start a Docker container from the image.
sudo docker run \
--name nodejs \
--publish 0.0.0.0:12345:12345 \
--detach \
nodejs:18-alpine
The docker container ls command should show the container is up and running.
~]$ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c19d5eab9a75 nodejs:18-alpine "docker-entrypoint.s…" 1 minute ago Up 1 minute 0.0.0.0:12345->12345/tcp nodejs
The docker logs command should return nothing since this is just a totally empty Node.js container.
sudo docker logs nodejs
Did you find this article helpful?
If so, consider buying me a coffee over at