- trim container image size - sanitize socktop inputs
This commit is contained in:
+94
-92
@@ -1,127 +1,129 @@
|
||||
# Dockerfile for socktop webterm
|
||||
# Based on Debian Trixie Slim with all required dependencies
|
||||
# Multi-stage Dockerfile for socktop webterm
|
||||
# This reduces the final image size significantly by separating build and runtime
|
||||
|
||||
FROM debian:trixie-slim
|
||||
# ============================================================================
|
||||
# Stage 1: Rust Builder
|
||||
# ============================================================================
|
||||
FROM rust:1.90-slim-bookworm AS rust-builder
|
||||
|
||||
# Avoid prompts from apt
|
||||
ENV DEBIAN_FRONTEND=noninteractive
|
||||
WORKDIR /build
|
||||
|
||||
# Set environment variables
|
||||
ENV RUST_VERSION=stable
|
||||
ENV CARGO_HOME=/usr/local/cargo
|
||||
ENV RUSTUP_HOME=/usr/local/rustup
|
||||
ENV PATH=/usr/local/cargo/bin:$PATH
|
||||
ENV TERM=xterm-256color
|
||||
|
||||
# Install system dependencies and security updates
|
||||
# Install build dependencies
|
||||
RUN apt-get update && \
|
||||
apt-get upgrade -y && \
|
||||
apt-get install -y \
|
||||
# Build dependencies
|
||||
build-essential \
|
||||
apt-get install -y --no-install-recommends \
|
||||
pkg-config \
|
||||
libssl-dev \
|
||||
# Rust/Cargo (needed to build webterm)
|
||||
curl \
|
||||
ca-certificates \
|
||||
# Node.js and npm (for xterm.js)
|
||||
nodejs \
|
||||
npm \
|
||||
# Alacritty dependencies
|
||||
cmake \
|
||||
fontconfig \
|
||||
libfontconfig1-dev \
|
||||
libfreetype6-dev \
|
||||
libxcb-xfixes0-dev \
|
||||
libxkbcommon-dev \
|
||||
python3 \
|
||||
# Runtime dependencies
|
||||
fonts-liberation \
|
||||
gnupg2 \
|
||||
wget \
|
||||
unzip \
|
||||
git \
|
||||
# Process management
|
||||
supervisor \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Install Rust
|
||||
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
|
||||
sh -s -- -y --default-toolchain ${RUST_VERSION} --profile minimal && \
|
||||
chmod -R a+w ${RUSTUP_HOME} ${CARGO_HOME}
|
||||
# Copy only dependency files first for better caching
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
|
||||
# Install Alacritty
|
||||
RUN cargo install alacritty && \
|
||||
rm -rf ${CARGO_HOME}/registry ${CARGO_HOME}/git
|
||||
# Create dummy source to cache dependencies
|
||||
RUN mkdir src && \
|
||||
echo "fn main() {}" > src/server.rs && \
|
||||
echo "pub fn lib() {}" > src/lib.rs && \
|
||||
cargo build --release && \
|
||||
rm -rf src
|
||||
|
||||
# Download and install FiraCode Nerd Font
|
||||
RUN mkdir -p /usr/share/fonts/truetype/firacode-nerd && \
|
||||
cd /tmp && \
|
||||
wget -q https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/FiraCode.zip && \
|
||||
unzip -q FiraCode.zip -d /usr/share/fonts/truetype/firacode-nerd/ && \
|
||||
rm FiraCode.zip && \
|
||||
fc-cache -fv && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
# Copy actual source code
|
||||
COPY src ./src
|
||||
COPY templates ./templates
|
||||
COPY static ./static
|
||||
|
||||
# Add socktop APT repository with GPG key
|
||||
RUN curl -fsSL https://jasonwitty.github.io/socktop/KEY.gpg | \
|
||||
gpg --dearmor -o /usr/share/keyrings/socktop-archive-keyring.gpg && \
|
||||
echo "deb [signed-by=/usr/share/keyrings/socktop-archive-keyring.gpg] https://jasonwitty.github.io/socktop stable main" > /etc/apt/sources.list.d/socktop.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y socktop socktop-agent && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Create application user (if not already exists from package)
|
||||
RUN id -u socktop &>/dev/null || useradd -m -s /bin/bash socktop && \
|
||||
mkdir -p /home/socktop/.config/alacritty && \
|
||||
mkdir -p /home/socktop/.config/socktop && \
|
||||
chown -R socktop:socktop /home/socktop
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy application files
|
||||
COPY --chown=socktop:socktop Cargo.toml Cargo.lock ./
|
||||
COPY --chown=socktop:socktop src ./src
|
||||
COPY --chown=socktop:socktop templates ./templates
|
||||
COPY --chown=socktop:socktop static ./static
|
||||
COPY --chown=socktop:socktop package.json package-lock.json ./
|
||||
|
||||
# Build the Rust application
|
||||
# Build the actual application
|
||||
RUN cargo build --release && \
|
||||
rm -rf target/release/build target/release/deps target/release/incremental && \
|
||||
strip target/release/webterm-server
|
||||
|
||||
# Install npm dependencies and copy static files
|
||||
# ============================================================================
|
||||
# Stage 2: Node.js Builder
|
||||
# ============================================================================
|
||||
FROM node:20-slim AS node-builder
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Copy package files
|
||||
COPY package.json package-lock.json ./
|
||||
COPY static ./static
|
||||
|
||||
# Install only production dependencies
|
||||
RUN npm ci --only=production && \
|
||||
# Copy static files to node_modules for serving
|
||||
cp static/terminado-addon.js node_modules/ && \
|
||||
cp static/bg.png node_modules/ && \
|
||||
cp static/styles.css node_modules/ && \
|
||||
cp static/terminal.js node_modules/ && \
|
||||
cp static/favicon.png node_modules/
|
||||
|
||||
# Copy configuration files from /files directory (will be mounted as volume)
|
||||
# This will be done at runtime via entrypoint script
|
||||
# ============================================================================
|
||||
# Stage 3: Runtime Image
|
||||
# ============================================================================
|
||||
FROM debian:trixie-slim
|
||||
|
||||
# Copy supervisor configuration
|
||||
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
||||
# Avoid prompts from apt
|
||||
ENV DEBIAN_FRONTEND=noninteractive
|
||||
ENV TERM=xterm-256color
|
||||
|
||||
# Copy entrypoint and restricted shell scripts
|
||||
# Install only runtime dependencies
|
||||
RUN apt-get update && \
|
||||
apt-get upgrade -y && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
# Runtime libraries
|
||||
libssl3 \
|
||||
ca-certificates \
|
||||
# For socktop packages
|
||||
curl \
|
||||
gnupg2 \
|
||||
# Shell and utilities
|
||||
bash \
|
||||
procps \
|
||||
# Health check
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Add socktop APT repository and install packages
|
||||
RUN curl -fsSL https://jasonwitty.github.io/socktop/KEY.gpg | \
|
||||
gpg --dearmor -o /usr/share/keyrings/socktop-archive-keyring.gpg && \
|
||||
echo "deb [signed-by=/usr/share/keyrings/socktop-archive-keyring.gpg] https://jasonwitty.github.io/socktop stable main" > /etc/apt/sources.list.d/socktop.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y --no-install-recommends socktop socktop-agent && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Create application user (if not already exists from socktop packages)
|
||||
RUN id -u socktop &>/dev/null || useradd -m -s /bin/bash socktop && \
|
||||
mkdir -p /home/socktop/.config/socktop && \
|
||||
chown -R socktop:socktop /home/socktop
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy built binary from rust-builder
|
||||
COPY --from=rust-builder /build/target/release/webterm-server /usr/local/bin/webterm-server
|
||||
|
||||
# Copy templates and static files
|
||||
COPY --from=rust-builder /build/templates ./templates
|
||||
COPY --from=rust-builder /build/static ./static
|
||||
|
||||
# Copy node_modules from node-builder
|
||||
COPY --from=node-builder /build/node_modules ./node_modules
|
||||
|
||||
# Copy runtime scripts
|
||||
COPY docker/entrypoint.sh /entrypoint.sh
|
||||
COPY docker/restricted-shell.sh /usr/local/bin/restricted-shell
|
||||
RUN chmod +x /entrypoint.sh && chmod +x /usr/local/bin/restricted-shell
|
||||
RUN chmod +x /entrypoint.sh
|
||||
|
||||
# Expose ports
|
||||
# 8082 - webterm HTTP server
|
||||
# 3001 - socktop agent
|
||||
# 3001 - socktop agent (if used)
|
||||
EXPOSE 8082 3001
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD curl -f http://localhost:8082/ || exit 1
|
||||
|
||||
# Set entrypoint (runs as root, then switches to socktop user)
|
||||
# Run as socktop user
|
||||
USER socktop
|
||||
|
||||
# Set entrypoint
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
|
||||
# Default command (can be overridden)
|
||||
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
|
||||
# Default command - run webterm server
|
||||
CMD ["webterm-server", "--host", "0.0.0.0", "--port", "8082"]
|
||||
|
||||
Reference in New Issue
Block a user