» Node.js: Building Full-Text Search API with ElasticSearch » 4. Deployment » 4.1 Dockerfile

Dockerfile

Docker allows developers to package their applications along with all dependencies into a single unit called a container. This ensures consistency across different environments, such as development, testing, and production, reducing the "it works on my machine" problem.

Install Docker: https://docs.docker.com/engine/install/

Dockerfile for the service

Add Dockerfile:

# Use the official Node.js image with specified version
FROM node:20.11-alpine3.18

# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install project dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY src/ /usr/src/app/src
COPY tsconfig.json ./

# Build TypeScript code
RUN npm run build

# Command to run the application
CMD ["node", "dist/main.js"]

Alpine Linux is a lightweight and secure Linux distribution that is particularly well-suited for containerized environments, embedded systems, and resource-constrained environments where efficiency and security are paramount.

With this Dockerfile, we‘re ready to run it with Elasticsearch in the Docker Compose.