Skip to content

Commit

Permalink
Refactor Dockerfile to streamline build process and remove unnecessar…
Browse files Browse the repository at this point in the history
…y stages
  • Loading branch information
DanielSarmiento04 committed Dec 28, 2024
1 parent a2f9cd3 commit 0e18713
Showing 1 changed file with 2 additions and 35 deletions.
37 changes: 2 additions & 35 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
# syntax=docker/dockerfile:1

# Define build arguments for Rust version and application name
ARG RUST_VERSION=1.81.0
ARG APP_NAME=memo

################################################################################
# Stage 1: Build the application
# Create a stage for building the application.

FROM rust:${RUST_VERSION}-alpine AS build
FROM rust:${RUST_VERSION} AS build
ARG APP_NAME
WORKDIR /app

# Install build dependencies
RUN apk add --no-cache clang lld musl-dev git pkgconfig openssl-dev

# Set OpenSSL environment variables for static linking
ENV OPENSSL_DIR=/usr
ENV OPENSSL_LIB_DIR=/usr/lib
ENV OPENSSL_INCLUDE_DIR=/usr/include
ENV OPENSSL_STATIC=1

# Leverage caching for dependencies and bind mounts for source files
RUN --mount=type=bind,source=src,target=src \
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
Expand All @@ -30,28 +18,7 @@ RUN --mount=type=bind,source=src,target=src \
cargo build --locked --release && \
cp ./target/release/$APP_NAME /bin/server

################################################################################
# Stage 2: Prepare the runtime environment

FROM alpine:3.18 AS final

# Create a non-privileged user for the application
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser

# Copy the built binary from the build stage
COPY --from=build /bin/server /bin/

# Expose the application port
EXPOSE 8000

# Specify the command to run the application
CMD ["/bin/server"]

0 comments on commit 0e18713

Please sign in to comment.