Skip to content

Modern Docker Management, Designed for Everyone

refactor: improve dockerfile

Kyle Mendell1a66037f6e01
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 000000000..88feeb3ca
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,55 @@
+# Version control
+.git
+.github
+.gitignore
+
+# Build artifacts
+node_modules
+build
+.svelte-kit
+coverage
+.coverage
+.nyc_output
+.env
+.env.*
+!.env.example
+
+# Development tools
+.vscode
+.idea
+*.sublime-*
+*.swp
+*.swo
+
+# Docker related
+Dockerfile
+.dockerignore
+docker-compose*.yml
+
+# Documentation and non-essential files
+README.md
+LICENSE
+*.md
+docs
+tests
+**/*.test.js
+**/*.spec.js
+**/*.stories.js
+
+# Temporary files
+.tmp
+.temp
+.cache
+**/.DS_Store
+npm-debug.log
+yarn-debug.log
+yarn-error.log
+pnpm-debug.log
+
+# Large media files that should be mounted or stored elsewhere
+**/*.mp4
+**/*.mov
+**/*.iso
+**/*.tar
+**/*.gz
+**/*.zip
\ No newline at end of file
diff --git a/.github/workflows/release-image.yml b/.github/workflows/release-image.yml
index 04529841b..8016c5f2c 100644
--- a/.github/workflows/release-image.yml
+++ b/.github/workflows/release-image.yml
@@ -11,8 +11,21 @@ jobs:
contents: read
packages: write
steps:
- - name: checkout code
- uses: actions/checkout@v3
+ - name: Checkout code
+ uses: actions/checkout@v4 # Use latest version
+ with:
+ fetch-depth: 1 # Shallow clone for faster checkout
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v3
+ with:
+ version: latest
+ buildkitd-flags: --debug
+
+ - name: Set up QEMU
+ uses: docker/setup-qemu-action@v3
+ with:
+ platforms: arm64,amd64
- name: Docker metadata
id: meta
@@ -23,20 +36,30 @@ jobs:
tags: |
type=semver,pattern={{version}},prefix=v
type=semver,pattern={{major}}.{{minor}},prefix=v
+ type=raw,value=latest,enable={{is_default_branch}}
- - name: Set up QEMU
- uses: docker/setup-qemu-action@v3
-
- - name: Set up Docker Buildx
- uses: docker/setup-buildx-action@v3
-
- - name: "Login to GitHub Container Registry"
+ - name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
- username: ${{github.repository_owner}}
- password: ${{secrets.GITHUB_TOKEN}}
+ username: ${{ github.repository_owner }}
+ password: ${{ secrets.GITHUB_TOKEN }}
+ # New step: Cache Docker layers
+ - name: Cache Docker layers
+ uses: actions/cache@v3
+ with:
+ path: /tmp/.buildx-cache
+ key: ${{ runner.os }}-buildx-${{ github.sha }}
+ restore-keys: |
+ ${{ runner.os }}-buildx-
+
+ # New step: Pull previous images for better layer caching
+ - name: Pull previous images for cache
+ run: |
+ docker pull ghcr.io/${{ github.repository }}:latest || true
+
+ # Optimized build step
- name: Build and push
uses: docker/build-push-action@v6
with:
@@ -45,5 +68,22 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- cache-from: type=gha
- cache-to: type=gha,mode=max
+ # Enhanced caching strategy
+ cache-from: |
+ type=local,src=/tmp/.buildx-cache
+ type=registry,ref=ghcr.io/${{ github.repository }}:latest
+ type=gha,scope=buildx-${{ github.workflow }}
+ cache-to: |
+ type=local,dest=/tmp/.buildx-cache-new,mode=max
+ type=gha,scope=buildx-${{ github.workflow }},mode=max
+ # Optional build arguments for better performance
+ build-args: |
+ BUILDKIT_INLINE_CACHE=1
+ # Output more build info for debugging
+ outputs: type=image,name=target,annotation-index.org.opencontainers.image.description=Arcane Docker Image
+
+ # Move cache to prevent cache growth issues
+ - name: Move cache
+ run: |
+ rm -rf /tmp/.buildx-cache
+ mv /tmp/.buildx-cache-new /tmp/.buildx-cache
diff --git a/Dockerfile b/Dockerfile
index 6c6d0ded2..79add31b9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,61 +1,61 @@
-# Stage 1: Build the application
-FROM node:22-alpine AS builder
+# Stage 1: Build dependencies
+FROM node:22-alpine AS deps
WORKDIR /app
-RUN mkdir -p /app/data && chown node:node /app/data
COPY package*.json ./
-RUN npm install
-COPY . .
+# Install dependencies first (better layer caching)
RUN npm ci
+
+# Stage 2: Build the application
+FROM node:22-alpine AS builder
+WORKDIR /app
+# Copy dependencies from previous stage
+COPY --from=deps /app/node_modules ./node_modules
+COPY . .
# When building, set NODE_ENV to "build" to prevent connection attempts
RUN NODE_ENV=build npm run build
-# Stage 2: Production image
-FROM node:22-alpine
-
-# Delete default node user
-RUN deluser --remove-home node
+# Stage 3: Production image
+FROM node:22-alpine AS runner
-# Install necessary packages
-RUN apk add --no-cache su-exec curl shadow
+# Delete default node user first (combine with package installation to reduce layers)
+RUN deluser --remove-home node && \
+ apk add --no-cache su-exec curl shadow
WORKDIR /app
-# Make sure data directory exists and is writable
-RUN mkdir -p /app/data && chmod 755 /app/data
+# Set up environment variables early for better caching
+ENV DOCKER_GID=998 \
+ PUID=1000 \
+ PGID=1000
-# Copy default settings if starting fresh
-COPY app-settings.json /app/data/app-settings.json.default
+# Create necessary groups and users
+RUN addgroup -g ${PGID} arcane && \
+ adduser -D -u ${PUID} -G arcane arcane && \
+ addgroup -g ${DOCKER_GID} docker && \
+ adduser arcane docker
-# Copy entrypoint script
-COPY scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.sh
-RUN chmod +x /usr/local/bin/entrypoint.sh
+# Set up directories and permissions
+RUN mkdir -p /app/data && chmod 755 /app/data
-# Copy application files
+# Copy only necessary files from builder
COPY --from=builder /app/build ./build
-COPY package*.json ./
-RUN npm install --omit=dev
COPY --from=builder /app/static ./static
+COPY --chown=arcane:arcane app-settings.json /app/data/app-settings.json.default
-# Create docker group with same GID as the host (typically 998 or 999)
-# We'll use 998 which is common, but this can be overridden at runtime
-ENV DOCKER_GID=998
-RUN addgroup -g ${DOCKER_GID} docker
+# Copy entrypoint script
+COPY --chmod=755 scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.sh
-# Create arcane user/group
-ENV PUID=1000
-ENV PGID=1000
-RUN addgroup -g ${PGID} arcane && \
- adduser -D -u ${PUID} -G arcane arcane && \
- # Add arcane user to docker group to allow socket access
- adduser arcane docker && \
- # Set ownership of app files
+# Install only production dependencies
+COPY package*.json ./
+RUN npm install --omit=dev && \
+ npm cache clean --force && \
chown -R arcane:arcane /app
+# Configure container
EXPOSE 3000
-LABEL org.opencontainers.image.authors="kmendell"
-
-# Add volume for persistent data
VOLUME ["/app/data"]
+LABEL org.opencontainers.image.authors="kmendell"
+# Set the entrypoint and command
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["node", "build"]
\ No newline at end of file