Skip to content

Commit 1c312c4

Browse files
committed
refactor: added dockerignore and improved Dockerfile
1 parent ceef6d7 commit 1c312c4

File tree

2 files changed

+121
-55
lines changed

2 files changed

+121
-55
lines changed

.dockerignore

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Dependencies
2+
node_modules
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
pnpm-debug.log*
7+
8+
# Build output
9+
lib
10+
dist
11+
*.tgz
12+
*.tar.gz
13+
14+
# Testing
15+
test
16+
coverage
17+
.nyc_output
18+
*.test.*
19+
*.spec.*
20+
__tests__
21+
__mocks__
22+
23+
# Development
24+
.git
25+
.gitignore
26+
.github
27+
.vscode
28+
.idea
29+
*.swp
30+
*.swo
31+
*~
32+
33+
# Documentation
34+
docs
35+
*.md
36+
!README.md
37+
CHANGELOG.md
38+
CONTRIBUTING.md
39+
CODE_OF_CONDUCT.md
40+
DEVELOPMENT.md
41+
42+
# Temporary files and directories
43+
tmp
44+
temp
45+
.tmp
46+
.temp
47+
.cache
48+
49+
# Environment and config files
50+
.env*
51+
.eslintrc*
52+
.prettierrc*
53+
lefthook.yml
54+
nodemon.json
55+
jest.config.*
56+
commitlint.config.js
57+
58+
# OS specific files
59+
.DS_Store
60+
Thumbs.db
61+
62+
# IDE files
63+
*.sublime-project
64+
*.sublime-workspace
65+
66+
# Logs
67+
logs
68+
*.log
69+
70+
# Optional npm cache directory
71+
.npm
72+
73+
# Optional eslint cache
74+
.eslintcache
75+
76+
# TypeScript cache
77+
*.tsbuildinfo
78+
79+
# Notes and misc
80+
notes.md
81+
pnpm-lock.yaml
82+
83+
# Action files (for main CLI docker image)
84+
action.yml
85+
action-template.yml
86+
github-action
87+
88+
# Test directories and files
89+
test-alpine-fix

Dockerfile

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,47 @@
1+
# Stage 1: Build
12
FROM node:20-alpine AS build
3+
WORKDIR /app
24

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 ./
147

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
2110

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
2420

25-
# Remove everything inside /tmp
26-
RUN rm -rf /tmp/*
21+
# Stage 2: Runtime
22+
FROM ghcr.io/puppeteer/puppeteer:20.8.0
2723

28-
FROM node:20-alpine
24+
# Switch to root to create user and set up permissions
25+
USER root
2926

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/*
3229

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
3532

33+
# Copy built files from builder stage
3634
WORKDIR /app
35+
COPY --from=build /app /app
3736

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
6341

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
6644

67-
# Switch to the non-root user
68-
USER myuser
45+
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
6946

7047
ENTRYPOINT [ "asyncapi" ]

0 commit comments

Comments
 (0)