Skip to content

Commit

Permalink
šŸ³ chore(Dockerfile): update Dockerfile for improved build process andā€¦
Browse files Browse the repository at this point in the history
ā€¦ 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.
35 changes: 21 additions & 14 deletions Dockerfile
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"]

0 comments on commit bfab9f2

Please sign in to comment.