Skip to content

Commit 1945817

Browse files
committed
Use multi-stage builds to keep the image smaller. Clean up unnecessary files after install to reduce image size.
1 parent 1ead456 commit 1945817

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

Dockerfile.playwright

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
1-
# === Stage 1: Build and install dependencies ===
2-
FROM mcr.microsoft.com/playwright:v1.53.1-jammy AS builder
1+
# ---- Build Stage ----
2+
FROM mcr.microsoft.com/playwright:v1.53.1-jammy AS build
33

4-
# Install Java & Allure
4+
# Install Java 11 (required for Allure)
55
RUN apt-get update && apt-get install -y wget gnupg2 && \
66
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor -o /usr/share/keyrings/adoptium.gpg && \
77
echo "deb [signed-by=/usr/share/keyrings/adoptium.gpg] https://packages.adoptium.net/artifactory/deb focal main" | tee /etc/apt/sources.list.d/adoptium.list && \
88
apt-get update && apt-get install -y temurin-11-jdk && \
9-
npm install -g allure-commandline --force && \
109
rm -rf /var/lib/apt/lists/*
1110

12-
# Set up env
11+
# Install Allure CLI globally
12+
RUN npm install -g allure-commandline --force
13+
14+
# Set JAVA_HOME for Allure
1315
ENV JAVA_HOME=/usr/lib/jvm/temurin-11-jdk-amd64
1416
ENV PATH=$JAVA_HOME/bin:$PATH
1517

1618
WORKDIR /app
19+
20+
# Copy only package files first to cache layer
1721
COPY package*.json ./
18-
RUN npm install
1922

20-
# === Stage 2: Final image ===
23+
# Install Node dependencies
24+
RUN npm ci
25+
26+
# Copy environment files
27+
COPY env ./env
28+
29+
# Copy the rest of the application
30+
COPY . .
31+
32+
# ---- Final Stage ----
2133
FROM mcr.microsoft.com/playwright:v1.53.1-jammy
2234

23-
# Copy Java, Allure, and installed node_modules from builder
24-
COPY --from=builder /usr/lib/jvm/temurin-11-jdk-amd64 /usr/lib/jvm/temurin-11-jdk-amd64
25-
COPY --from=builder /usr/share/keyrings/adoptium.gpg /usr/share/keyrings/adoptium.gpg
26-
COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules
27-
COPY --from=builder /usr/local/bin/allure /usr/local/bin/allure
28-
COPY --from=builder /app/node_modules /app/node_modules
35+
# Copy Java and Allure from build stage
36+
COPY --from=build /usr/lib/jvm/ /usr/lib/jvm/
37+
COPY --from=build /usr/local/lib/node_modules/allure-commandline /usr/local/lib/node_modules/allure-commandline
38+
RUN ln -s /usr/local/lib/node_modules/allure-commandline/bin/allure /usr/local/bin/allure
2939

30-
# Set up env again
3140
ENV JAVA_HOME=/usr/lib/jvm/temurin-11-jdk-amd64
3241
ENV PATH=$JAVA_HOME/bin:$PATH
3342

3443
WORKDIR /app
35-
COPY . .
36-
COPY env ./env
3744

38-
# Clean unnecessary files
39-
RUN rm -rf /root/.cache /root/.npm /tmp/*
45+
# Copy node_modules and built app from build stage
46+
COPY --from=build /app/node_modules ./node_modules
47+
COPY --from=build /app/env ./env
48+
COPY --from=build /app/. .
49+
COPY --from=build /app/package*.json ./
4050

41-
CMD ["npm", "run", "test:allure"]
51+
CMD ["npm", "run", "test:allure"]

0 commit comments

Comments
 (0)