DevOps效率提升:如何使用 ChatGPT 輕鬆優化 NodeJS Docker 镜像大小?

2023/11/10閱讀時間約 8 分鐘
raw-image

你有沒有想過用 ChatGPT 來減少 DevOps 工作?

今天我將向您展示如何使用 ChatGPT 減少 NodeJS 網絡應用程式的鏡像大小。

您會驚訝於 ChatGPT 如何能夠在短短幾分鐘內輕鬆生成一個無錯誤的 Docker 文件,來優化 Node.js docker 映像的大小。

在我們開始之前,您可能有一些問題。

我們為什麼需要優化網站應用程式的 Docker 映像?

因為這可以節省您建立Dcoker映像及發佈應用程式到雲端的時間。

想想看,如果您只是更改了一行代碼,但您需要等待超過 10 分鐘來檢查結果。聽起來像很瘋狂且效率不高。

假設您需要在 nodejs 中構建一個網站應用程式,您實際上可以在 Google 上輕易找到一個優化的 Docker 文件,如下所示。

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 8080EXPOSE 8080CMD ["dumb-init", "npm", "start"]


這個 Docker 文件可以幫助您將檔案大小從 1 GB 降低到 401 MB,因為它使用了一個精簡版的 bullseye-slim docker。

raw-image

但這還不夠好,如果你知道多階段構建可以減少大小,但不知道如何編寫 Docker 文件怎麼辦?

您可以要求 ChatGPT 為您編寫完整的 Docker 文件,只需提供您原始的 Docker 文件並談論如何改進它。讓我們試試看。


ChatGPT Prompt:

raw-image

小秘訣:

  • 為了獲得更好的結果,您的提示應該給 ChatGPT 一個清晰的定位比如專業的 C#/Docker/JS/C++ 程序員、DevOps 或解決方案架構師,並提供一個示例。
  • 然後 ChatGPT 可以提供與您的問題相關的更好的答案。
  • 當然,這個小秘訣未來可以幫助您快速找到不同技術問題的解決方案。
  • 本文展示為英文,指令其實可以下中文,只要能夠滿足這秘訣的條件,你都會得到不錯的結果。

結果

raw-image
# 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/TokyoENV PORT $TARGET_PORT

EXPOSE $TARGET_PORT

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


接著我們檢查結果。現在我們能將Docker大小從 401 MB 降低到 293 MB。

raw-image

接下來,我們可以詢問 ChatGPT 是否可以做得更好。

Prompt

raw-image
# 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 nodeCMD ["dumb-init", "node", "index.js"]


接著我們檢查結果。現在我們能將鏡像大小從 401 MB 降低到 293 MB。

raw-image

哇,真不錯。

我們實際上在幾分鐘內就將 NodeJS 大小從 1 GB 減少了 73.2%,而且甚至不需要知道如何編寫 Docker 文件。

raw-image

最後的想法

在這篇文章中,您了解到只要通過給出適當的指令,ChatGPT 可能夠輕鬆地簡化您的 DevOps 工作,來提高您的生產力。

你甚至無需精通各種複雜編程代碼或 shell 腳本,只需提供清晰的指令。

通過這種方法,您不僅可以節省工作時間,還可以騰出更多時間來做喜歡的事,像是玩遊戲。

如果您認為這篇文章有用,請通過 👏 這篇文章或追蹤我們以獲得未來的最新教程。來表達您的支持。

感謝您的閱讀!


原文發佈於 Game Tech Tutorial

歡迎參觀Game Tech Tutorial 來獲取更多教程喔!

分享技術文章解決問題及生活點滴。技術包括雲端技術(AWS, GCP, Azure)的實戰經驗、雲端證照考試指南、Unity遊戲開發、DevOps、SDK,手機遊戲和應用程式及網站開發。
留言0
查看全部
發表第一個留言支持創作者!