|
| 1 | +# Stage 1: Build |
1 | 2 | FROM node:20-alpine AS build |
| 3 | +WORKDIR /app |
2 | 4 |
|
3 | | -# Copy the source code |
4 | | -COPY ./ /tmp/source_code |
5 | | - |
6 | | -# Install dependencies |
7 | | -RUN cd /tmp/source_code && npm install --ignore-scripts |
8 | | - |
9 | | -# Build the source code |
10 | | -RUN cd /tmp/source_code && npm run build |
11 | | - |
12 | | -# create libraries directory |
13 | | -RUN mkdir -p /libraries |
| 5 | +# Copy package files first for better caching |
| 6 | +COPY package*.json ./ |
14 | 7 |
|
15 | | -# Copy the lib, bin, node_modules, and package.json files to the /libraries directory |
16 | | -RUN cp -r /tmp/source_code/lib /libraries |
17 | | -RUN cp -r /tmp/source_code/assets /libraries |
18 | | -RUN cp /tmp/source_code/package.json /libraries |
19 | | -RUN cp /tmp/source_code/package-lock.json /libraries |
20 | | -RUN cp /tmp/source_code/oclif.manifest.json /libraries |
| 8 | +# Install all dependencies (including dev dependencies for build) |
| 9 | +RUN npm ci --ignore-scripts |
21 | 10 |
|
22 | | -# Copy the bin directory to the /libraries directory |
23 | | -RUN cp -r /tmp/source_code/bin /libraries |
| 11 | +# Copy only necessary files for build |
| 12 | +COPY src/ ./src/ |
| 13 | +COPY assets/ ./assets/ |
| 14 | +COPY scripts/ ./scripts/ |
| 15 | +COPY bin/ ./bin/ |
| 16 | +COPY tsconfig.json ./ |
| 17 | +RUN npm run build && \ |
| 18 | + npm prune --omit=dev && \ |
| 19 | + rm -rf test docs .git tmp node_modules/.cache |
24 | 20 |
|
25 | | -# Remove everything inside /tmp |
26 | | -RUN rm -rf /tmp/* |
| 21 | +# Stage 2: Runtime |
| 22 | +FROM ghcr.io/puppeteer/puppeteer:20.8.0 |
27 | 23 |
|
28 | | -FROM node:20-alpine |
| 24 | +# Switch to root to create user and set up permissions |
| 25 | +USER root |
29 | 26 |
|
30 | | -# Set ARG to explicit value to build chosen version. Default is "latest" |
31 | | -ARG ASYNCAPI_CLI_VERSION= |
| 27 | +# Install git (needed by AsyncAPI CLI) |
| 28 | +RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/* |
32 | 29 |
|
33 | | -# Create a non-root user |
34 | | -RUN addgroup -S myuser && adduser -S myuser -G myuser |
| 30 | +# Create non-root user |
| 31 | +RUN groupadd -r asyncapi && useradd -r -g asyncapi asyncapi |
35 | 32 |
|
| 33 | +# Copy built files from builder stage |
36 | 34 | WORKDIR /app |
| 35 | +COPY --from=build /app /app |
37 | 36 |
|
38 | | -# Since 0.14.0 release of html-template chromium is needed for pdf generation |
39 | | -ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium-browser |
40 | | -ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true |
41 | | -# Since 0.30.0 release Git is supported and required as a dependency |
42 | | -# Since 0.14.0 release of html-template chromium is needed for pdf generation. |
43 | | -# More custom packages for specific template should not be added to this dockerfile. Instead, we should come up with some extensibility solution. |
44 | | -RUN apk --update add git chromium && \ |
45 | | - apk add --no-cache --virtual .gyp python3 make g++ && \ |
46 | | - rm -rf /var/lib/apt/lists/* && \ |
47 | | - rm /var/cache/apk/* |
48 | | - |
49 | | -# Copy the libraries directory from the build stage |
50 | | -COPY --from=build /libraries /libraries |
51 | | - |
52 | | -# Install the dependencies |
53 | | -RUN cd /libraries && npm install --production --ignore-scripts |
54 | | - |
55 | | -# Create a script that runs the desired command |
56 | | -RUN ln -s /libraries/bin/run_bin /usr/local/bin/asyncapi |
57 | | - |
58 | | -# Make the script executable |
59 | | -RUN chmod +x /usr/local/bin/asyncapi |
60 | | - |
61 | | -# Change ownership to non-root user |
62 | | -RUN chown -R myuser:myuser /libraries /usr/local/bin/asyncapi || echo "Failed to change ownership" |
| 37 | +# Create symlink and set permissions |
| 38 | +RUN ln -s /app/bin/run_bin /usr/local/bin/asyncapi && \ |
| 39 | + chmod +x /usr/local/bin/asyncapi && \ |
| 40 | + chown -R asyncapi:asyncapi /app |
63 | 41 |
|
64 | | -RUN chown -R myuser:myuser /usr/local/lib/node_modules && \ |
65 | | -chown -R myuser:myuser /usr/local/bin |
| 42 | +# Switch to non-root user for runtime |
| 43 | +USER asyncapi |
66 | 44 |
|
67 | | -# Switch to the non-root user |
68 | | -USER myuser |
| 45 | +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true |
69 | 46 |
|
70 | 47 | ENTRYPOINT [ "asyncapi" ] |
0 commit comments