From 4b0017da279c21827e566a8d2f688aea99f0044b Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Thu, 10 Jul 2025 16:46:10 +0530 Subject: [PATCH 1/2] build: optimize Dockerfile and add .dockerignore to reduce image size --- .dockerignore | 8 ++++++ Dockerfile | 78 +++++++++++++++------------------------------------ 2 files changed, 30 insertions(+), 56 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000000..e23d1323639 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +.git +test +docs +*.log +dist +tmp +coverage diff --git a/Dockerfile b/Dockerfile index bee568fe55f..2dc8213928b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,70 +1,36 @@ +# Stage 1: Build FROM node:20-alpine AS build - -# Copy the source code -COPY ./ /tmp/source_code +WORKDIR /app # Install dependencies -RUN cd /tmp/source_code && npm install --ignore-scripts - -# Build the source code -RUN cd /tmp/source_code && npm run build - -# create libraries directory -RUN mkdir -p /libraries +COPY package*.json ./ +RUN npm install --ignore-scripts -# Copy the lib, bin, node_modules, and package.json files to the /libraries directory -RUN cp -r /tmp/source_code/lib /libraries -RUN cp -r /tmp/source_code/assets /libraries -RUN cp /tmp/source_code/package.json /libraries -RUN cp /tmp/source_code/package-lock.json /libraries -RUN cp /tmp/source_code/oclif.manifest.json /libraries +# Copy rest of the source and build +COPY . . +RUN npm run build && \ + npm prune --omit=dev && \ + rm -rf test docs .git -# Copy the bin directory to the /libraries directory -RUN cp -r /tmp/source_code/bin /libraries +# Stage 2: Runtime +FROM ghcr.io/puppeteer/puppeteer:20.8.0 -# Remove everything inside /tmp -RUN rm -rf /tmp/* +# Switch to root to create user and set up permissions +USER root -FROM node:20-alpine - -# Set ARG to explicit value to build chosen version. Default is "latest" -ARG ASYNCAPI_CLI_VERSION= - -# Create a non-root user -RUN addgroup -S myuser && adduser -S myuser -G myuser +# Create non-root user +RUN groupadd -r myuser && useradd -r -g myuser myuser +# Copy built files from builder stage WORKDIR /app +COPY --from=build /app /app -# Since 0.14.0 release of html-template chromium is needed for pdf generation -ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium-browser -ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true -# Since 0.30.0 release Git is supported and required as a dependency -# Since 0.14.0 release of html-template chromium is needed for pdf generation. -# More custom packages for specific template should not be added to this dockerfile. Instead, we should come up with some extensibility solution. -RUN apk --update add git chromium && \ - apk add --no-cache --virtual .gyp python3 make g++ && \ - rm -rf /var/lib/apt/lists/* && \ - rm /var/cache/apk/* - -# Copy the libraries directory from the build stage -COPY --from=build /libraries /libraries +# Create symlink and set permission, as root +RUN ln -s /app/bin/run_bin /usr/local/bin/asyncapi && chmod +x /usr/local/bin/asyncapi -# Install the dependencies -RUN cd /libraries && npm install --production --ignore-scripts - -# Create a script that runs the desired command -RUN ln -s /libraries/bin/run_bin /usr/local/bin/asyncapi - -# Make the script executable -RUN chmod +x /usr/local/bin/asyncapi - -# Change ownership to non-root user -RUN chown -R myuser:myuser /libraries /usr/local/bin/asyncapi || echo "Failed to change ownership" - -RUN chown -R myuser:myuser /usr/local/lib/node_modules && \ -chown -R myuser:myuser /usr/local/bin - -# Switch to the non-root user +# Now switch to non-root user for runtime USER myuser +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true + ENTRYPOINT [ "asyncapi" ] From 83420351739d5296f15ae0380bf8d8936560d3cb Mon Sep 17 00:00:00 2001 From: asyncapi-bot Date: Thu, 10 Jul 2025 12:29:51 +0000 Subject: [PATCH 2/2] chore: add changeset for PR #1817 --- .changeset/1817.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .changeset/1817.md diff --git a/.changeset/1817.md b/.changeset/1817.md new file mode 100644 index 00000000000..dfef1141210 --- /dev/null +++ b/.changeset/1817.md @@ -0,0 +1,9 @@ +--- +'@asyncapi/cli': minor +--- + +feat: optimize Dockerfile to reduce image size and build time + +- 4b0017d: build: optimize Dockerfile and add .dockerignore to reduce image size + +