1. Create a .dockerignore file

node_modules
dist
.git
Dockerfile

2. Create a Dockerfile file

# Step 1: Use a base image with Node.js
FROM node:18

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

# Step 3: Copy package.json and package-lock.json
COPY package*.json ./

# Step 4: Install dependencies
RUN npm install

# Step 5: Copy the rest of the app's source code
COPY . .

# Step 6: Build the TypeScript code
RUN npm run build

# Step 7: Expose the port the app runs on
EXPOSE 3000

# Step 8: Specify the command to run your app
CMD ["node", "dist/index.js"]

Dockerfile for pdf generator project as an example

3. Build the Docker Image

docker build -t your-ts-backend:latest .

4. Test Running locally

Get the image name

docker images

Run the image

docker run -it -p 3000:3000 my-image-name

-t (Allocate a pseudo-TTY)

-i (Keep STDIN open)

5. Push the Image

Push to Docker Hub