How To Get Image Build a Node.js Typescript Application with Docker.
I’ve explained how to get image build on your Node.js anyone Express (or) Nest.js Framework applications' with docker.
What is docker?
Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.
Step 1:-
First install the ‘Docker Desktop’ application on your computer. Aims to understand the available Docker images and running containers. According to your requirement:-
See this link for install Docker Desktop.
The Above step is not mandatory based on your understanding. If no need forget the above step.
Step 2:-
- Check the docker version in your machine.
- docker — version
2. Once Docker successfully installed then follow as below steps.
3. Go to the your project directory and create the Dockerfile.
Like this 👇.
Step 2:-
There are two types of build variations:-
- Development layer
- Production layer
Development layer
- Follow as below lines’. Copy and paste it here.
# Runtime (development) layer
FROM node:18-alpine as development
#Below line apply for Nest.js framework only ,other framework no need
RUN npm install -g @nestjs/cli
#Working directory inside docker
WORKDIR /app
# Copy dependencies files
COPY package*.json ./
COPY tsconfig.build.json ./
COPY .env ./
# Install development dependencies
RUN npm install — force
# Get build
RUN npm run build
# Copy development build
COPY dist ./
# Expose application port
EXPOSE 4500
# Start application node
CMD [ “node”, “main.js” ]
Explanation:-
1. Runtime (development) layer:
- This part of the Dockerfile defines a stage named
development
using thenode:18-alpine
base image. - Alpine Linux is a lightweight Linux distribution, and
node:18-alpine
is a Docker image containing Node.js 18.x on Alpine Linux. - The
FROM
instruction specifies the base image for this stage.
2. Install Nest.js CLI:
- The
RUN npm install -g @nestjs/cli
command installs the Nest.js CLI globally. This is useful for running Nest.js commands during development, such as generating new modules, services, etc. - This line need only using Nest.js framework while using other framework ignore it.
3. Set Working Directory:
- The
WORKDIR /app
command sets the working directory inside the container to/app
. All subsequent commands will be executed from this directory.
4. Copy Dependency Files:
- The
COPY
instructions copy thepackage.json
,package-lock.json
,tsconfig.build.json
, and.env
files from the local file system into the/app
directory in the container. These files are necessary for installing dependencies and building the application.
5. Install Development Dependencies:
- The
RUN npm install --force
command installs the Node.js dependencies defined inpackage.json
. The--force
flag ensures that dependencies are installed even if there are vulnerabilities or missing peer dependencies.
6. Build Application:
- The
RUN npm run build
command runs the build script defined inpackage.json
. This script typically compiles TypeScript code into JavaScript and generates the production-ready build of the application.
7. Copy Built Application:
- The
COPY dist ./
command copies the built application files from thedist
directory (which is typically the output directory for the build script) into the working directory in the container.
8. Expose Application Ports:
- The
EXPOSE
instructions expose ports 4500 to allow incoming connections to the application. This is necessary for accessing the application from outside the container.
9. Start Application:
- The
CMD [ "node", "main.js" ]
command specifies the default command to run when the container starts. It starts the Node.js application by running themain.js
file.
Production Layer:-
- Follow as below lines’. Copy and paste it here.
# Runtime (production) layer
FROM node:18-alpine as production
RUN npm install -g @nestjs/cli
WORKDIR /app
# Copy dependencies files
COPY package*.json ./
COPY tsconfig.build.json ./
COPY .env ./
# Install runtime dependencies (without dev/test dependencies)
#RUN yarn install — production
# (or)
RUN npm install — force — production
#Get production build
RUN npm run build
# Install pm2 globally
RUN npm install -g pm2
# Copy production build
COPY dist ./
# Expose application port
EXPOSE 4500
EXPOSE 4600
# Set NODE_ENV to production carefully handle this line, affecting #your .env file
#ENV NODE_ENV=production
# Start application pm2
#(or)
#CMD [“pm2”, “start”, “main.js”, “ — no-daemon”] CMD [ “pm2-runtime”, “main.js”, “ — no-daemon” ]
Explanation:-
- Runtime (production) layer:
- This part of the Dockerfile defines a stage named
production
using thenode:18-alpine
base image. - Change your based on your node version
node:version-alpine
. - Alpine Linux is a lightweight Linux distribution, and
node:18-alpine
is a Docker image containing Node.js 18.x on Alpine Linux. - The
FROM
instruction specifies the base image for this stage.
2. Install Nest.js CLI:
- The
RUN npm install -g @nestjs/cli
command installs the Nest.js CLI globally. This is useful for running Nest.js commands during production, such as generating new modules, services, etc. - This line need only using Nest.js framework while using other framework ignore it.
3. Set Working Directory:
- The
WORKDIR /app
command sets the working directory inside the container to/app
. All subsequent commands will be executed from this directory.
4. Copy Dependency Files:
- The
COPY
instructions copy thepackage.json
,package-lock.json
,tsconfig.build.json
, and.env
files from the local file system into the/app
directory in the container. These files are necessary for installing dependencies and building the application.
5. Install Production Dependencies:
- The
RUN npm install --force --production
command installs the Node.js dependencies defined inpackage.json
without dev/test dependencies. The--force
flag ensures that dependencies are installed even if there are vulnerabilities or missing peer dependencies.
6. Build Application:
- The
RUN npm run build
command runs the build script defined inpackage.json
. This script typically compiles TypeScript code into JavaScript and generates the production-ready build of the application.
7. Install pm2 Globally:
- The
RUN npm install -g pm2
command installs the PM2 process manager globally. PM2 is commonly used to manage Node.js applications in production environments.
8. Copy Built Application:
- The
COPY dist ./
command copies the built application files from thedist
directory (which is typically the output directory for the build script) into the working directory in the container.
9. Expose Application Ports:
- The
EXPOSE
instructions expose ports 4500 to allow incoming connections to the application. This is necessary for accessing the application from outside the container.
10. Start Application with PM2:
- The
CMD [ "pm2-runtime", "main.js", "--no-daemon" ]
command specifies the default command to run when the container starts. It starts the Node.js application using PM2 as the process manager in production mode.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Step 3: -
- After followed above lines then execute below command’s .
2. Enter the below command for get build command inside the project directory like this👇
docker build -t <your_app_name> .
The docker build
command is used to build a Docker image from a Dockerfile. Here's an explanation of the command along with its options:
docker build
: This is the main command to build Docker images.-t <your_app_name>
: The-t
option is used to tag the built image with a name and optionally a tag. In this case,<your_app_name>
is the name you choose for your Docker image. You can replace<your_app_name>
with the desired name for your application..
: The dot.
at the end of the command specifies the build context. It tells Docker to look for the Dockerfile in the current directory (.
). The Dockerfile is used as the recipe to build the image.
3. When you run this command, Docker will look for a Dockerfile in the current directory, and use it to build an image with the specified name and tag. The resulting image will be tagged with the name <your_app_name>
.
4. Once successfully runned then, after you can see your builded images use this command.
docker images
(or)
docker images -a
(or)
You can see your images from your installed Docker Desktop application. If not installed then Download a Docker Desktop .
5. You can then use this image to create containers and run your application in Docker.
6. Run the following command to build the container:
docker run -p 4200:4500 — restart on-failure:2 node_app
(Or) Detach mode
docker run -d -p 4500:4500 — restart on-failure:2 app
docker run
: This is the main command to run Docker containers.-d
: This option runs the container in detached mode.-p 4200:4500
: The-p
option is used to map ports between the container and the host system. In this case, it maps port 4500 inside the container to port 4200 on the host system. This means that traffic sent to port 4200 on the host will be forwarded to port 4500 inside the container.--restart on-failure:2
: The--restart
option specifies the restart policy for the container. In this case, it is set toon-failure:2
, which means that the container will automatically restart if it exits with a non-zero status, and it will attempt to restart a maximum of 2 times.node_app
: This is the name or ID of the Docker image that the container should be based on. It specifies the image to use when creating the container.
7. When you run this command, Docker will create a new container based on the node_app
image, and it will map port 4200 on the host system to port 4500 inside the container. Additionally, it will set the restart policy to on-failure:2
, so the container will automatically restart if it fails, up to a maximum of 2 times.
(or)
8. See your running docker build inside Docker Desktop containers tab.
9. Once your container is up and running, you can inspect a list of your running containers with docker ps
:
docker ps
10. It will ready to go live your node application check your any browser.
URL — http://localhost:4200/
(or)
11. Using the CONTAINER ID
listed in your output, stop the running application container. Be sure to replace the highlighted ID below with your own CONTAINER ID
:
docker stop e50ad27074a7
12. Remove the stopped container and all of the images, including unused or dangling images, with the following command:
docker system prune -a
— — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Author by
Sasiclinton. R (Profile)