Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/1817.md
Original file line number Diff line number Diff line change
@@ -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


8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
.git
test
docs
*.log
dist
tmp
coverage
78 changes: 22 additions & 56 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]