Bootstrap FreeKB - NodeJS - Getting Started with NodeJS on Docker
NodeJS - Getting Started with NodeJS on Docker

Updated:   |  NodeJS articles

Create a file named package.json that contains the following.

{
  "name": "docker-node-container",
  "version": "1.0.0",
  "description": "my NodeJS npm container"
}

 

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 NodeJS 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 NodeJS Express 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 NodeJS container.

]$ sudo docker logs nodejs

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8dfa4b in the box below so that we can be sure you are a human.