Skip to content

Commit 18f34d2

Browse files
committed
refactor: added dockerignore and improved Dockerfile
1 parent de8e46a commit 18f34d2

File tree

2 files changed

+120
-58
lines changed

2 files changed

+120
-58
lines changed

.dockerignore

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