# Multi-stage Dockerfile to build stlink from source and provide a runnable container
# Stage 1: Build the project from source
FROM ubuntu:22.04 AS builder
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential cmake libusb-1.0-0-dev libgtk-3-dev pkg-config git ca-certificates \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY . /src
# Build and install to /usr/local
RUN mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local .. && \
make -j$(nproc) && make install
# Stage 2: Run the built binaries
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
# Runtime dependencies (GUI libraries included for compatibility with GUI tools)
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libusb-1.0-0-dev libusb-1.0-0 libgtk-3-0 libgl1-mesa-dri libgl1-mesa-glx ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Create a simple, non-root user for running the application (security best practice)
RUN groupadd -r stlink && useradd -m -r -g stlink -d /home/stlink -s /bin/bash stlink
# Copy the built artifacts from the builder stage
COPY --from=builder /usr/local /usr/local
# Ensure binaries can be found in PATH and fix permissions
ENV PATH=/usr/local/bin:$PATH
RUN chown -R stlink:stlink /usr/local
USER stlink
WORKDIR /workspace
# Provide an interactive shell by default
CMD ["/bin/bash"]