post-thumb

DevOps How to use ChatGPT to reduce NodeJS docker image?

Hi

Have you ever thought about using ChatGPT to reduce DevOps work?

Today I will be showing you how to use ChatGPT to reduce the image size of the NodeJS web application.

You will be surprised at how ChatGPT can easily generate a bug-free Docker file to optimize the Node.js docker image size in just a few minutes.

Before we dive in, you may have a few questions.

Why do we need to optimize the docker image for web application?

Because this can save you time to build an application.

Think about if you just change one line of code, but you need to wait for more than 10 minutes to check the result. It’s sound like insane and insufficient.

Suppose you need to build a web application in nodejs, you can actually google and find an optimized Docker file as shown below.

FROM node:18.16.0-bullseye-slim

RUN apt-get update && apt-get install -y --no-install-recommends dumb-init

# Create app directory
WORKDIR /app

# Install app dependencies
COPY package*.json ./
COPY .env ./

RUN npm install

# Copying rest of the application to app directory
COPY . .

# Expose the port and start the application
ENV PORT 8080
EXPOSE 8080

CMD ["dumb-init", "npm", "start"]

This Docker File can help you to reduce image size down to 401 MB from 1 GB because of using a slim docker bullseye-slim.

img

But this is not good enough what if you know multiple build phases can reduce the size but don’t know how to write the Docker file?

You can ask ChatGTP to write a complete Docker file for you by simply providing your original Docker file and talking about how to improve it.

Let’s give it a try.


ChatGPT Prompt:

img

Tips

  • To achieve better results, your prompt should give ChatGPT a clear position like a pro C#/Docker/JS/C++ programmer, DevOps, or Solution Architect, and provide an example.
  • Then ChatGPT can provide better answers related to your questions.
  • Of course, these tips can help you quickly find a solution to different technical problems.

Result

img

# Builder stage
FROM node:18.16.0-alpine as builder

# Create app directory
WORKDIR /app

# Install app dependencies by copying package.json and package-lock.json (if available)
COPY package*.json ./
COPY .env ./

# Install dependencies
RUN npm install --production

# Copy the rest of the application to app directory
COPY . .

# Runner stage
FROM node:18.16.0-alpine

# Install dumb-init
RUN apk add --no-cache dumb-init

# Create app directory
WORKDIR /app

# Copy built node modules and built dist directory from builder stage
COPY --from=builder /app .

# Set the timezone and port environment variables
ENV TZ=Asia/Tokyo
ENV PORT $TARGET_PORT
EXPOSE $TARGET_PORT

CMD ["dumb-init", "npm", "start"]

Then we check the result. Now we can reduce the image size to 293 MB from 401 MB.

img

Next, we can ask ChatGPT to see whether we can make it even better.


Prompt

img

# Builder stage
FROM node:18.16.0-alpine as builder

# Create app directory
WORKDIR /app

# Install app dependencies by copying package.json and package-lock.json (if available)
COPY package*.json ./

# Install only the production dependencies and make sure to clean up the cache
RUN npm install --production && npm cache clean --force

# Copy the rest of the application to app directory
COPY . .

# Prune unnecessary files, you might need to adjust this to your app's structure
# RUN rm -rf ./tests ./docs

# Runner stage
FROM node:18.16.0-alpine

# Install dumb-init
RUN apk add --no-cache dumb-init

# Create app directory
WORKDIR /app

# Copy built node modules and the necessary app files from builder stage
COPY --from=builder /app .

# If you have static assets or other files that need to be served, you could also add them here
# COPY --from=builder /app/public /public

ENV PORT=$TARGET_PORT

# Expose the application's port
EXPOSE $PORT

# Ensure the process is run as non-root user for security
USER node

CMD ["dumb-init", "node", "index.js"]

Though ChatGPT cannot achieve 100 MB, you can get the final optimized image size of 268 MB.

img

Wow, this is not bad.

We actually reduced 73.2% of NodeJS image size from 1 GB in just a few minutes without even knowing how to write the Docker file.

img


Final thoughts

In this article, you learn how ChatGPT can easily streamline your DevOps tasks to enhance your productivity by giving proper instructions.

There’s no need to master various programming codes or shell scripts but simply provide clear prompts.

With this approach, you can not only save time but also free up your schedule for more enjoyable activities, like playing games.

Thank you for reading!

Website: Game Tech tutorial