-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
š³ chore(Dockerfile): update Dockerfile for improved build process andā¦
ā¦ security with non-root user configuration
- Loading branch information
fsociety
committed
Oct 9, 2024
1 parent
6f827cc
commit bfab9f2
Showing
1 changed file
with
21 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
# Build stage | ||
FROM golang:bookworm AS builder | ||
# Use Golang image based on Debian Bookworm | ||
FROM golang:bookworm | ||
|
||
# Set the working directory within the container | ||
WORKDIR /app | ||
|
||
ARG REPO_URL=https://github.com/bitvora/haven.git | ||
ARG VERSION | ||
|
||
# Clone and build application | ||
RUN git clone $REPO_URL . && git checkout $VERSION | ||
RUN go mod download | ||
RUN go build -o main . | ||
# Clone the repository | ||
RUN git clone --branch ${VERSION} ${REPO_URL} . | ||
|
||
# Final stage | ||
FROM debian:bookworm-slim | ||
|
||
WORKDIR /app | ||
# Download dependencies | ||
RUN go mod download | ||
|
||
COPY --from=builder /app/main . | ||
# Build the Go application | ||
RUN go build -o main . | ||
|
||
# Use a non-root user in final image for better security | ||
# Add environment variables for UID and GID | ||
ARG DOCKER_UID=1000 | ||
ARG DOCKER_GID=1000 | ||
|
||
# Create a new group and user | ||
RUN groupadd -g ${DOCKER_GID} appgroup && \ | ||
useradd -u ${DOCKER_UID} -g appgroup -m appuser && \ | ||
chown -R appuser:appgroup /app | ||
useradd -u ${DOCKER_UID} -g appgroup -m appuser | ||
|
||
# Change ownership of the working directory | ||
RUN chown -R appuser:appgroup /app | ||
|
||
# Switch to the new user | ||
USER appuser | ||
|
||
# Expose the port that the application will run on | ||
EXPOSE 3355 | ||
|
||
# Set the command to run the executable | ||
CMD ["./main"] |