Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dockerfile #17

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Use a specific version of the Golang image for the build stage
FROM golang:1.22.2-alpine AS builder

# Install git, necessary for fetching the Go modules and source code
RUN apk --no-cache add git

# Set the working directory inside the container
WORKDIR /app

# Copy your local source code into the image
COPY . .

# Adjust permissions to ensure all files in /app are accessible by 'nobody'
# Set ownership of all files in the /app directory to 'nobody'
RUN chown -R nobody:nobody /app

# Create a directory for the build cache that the 'nobody' user can access
RUN mkdir /.cache && chown nobody:nobody /.cache

# Add a non-root user and switch to it
USER nobody

# Set environment variable for Go cache directory
ENV GOCACHE=/.cache/go-build

# Run go mod tidy to ensure all dependencies are correct and go.sum is updated based on your go.mod
RUN go mod tidy

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Build the application. Consider simplifying build flags if debug information is not an issue or necessary for troubleshooting
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o electronwall .

# Switch to a new stage using Alpine to keep the final image small
FROM alpine:latest

# Install Bash in the final image
RUN apk add --no-cache bash

# Create a user 'appuser' and switch to it
RUN adduser -D appuser
USER appuser

# Set work directory to /app
WORKDIR /app

# Copy the binary from the builder stage
COPY --from=builder /app/electronwall .

# Copy the rules directory from the builder stage
COPY --from=builder /app/rules /app/rules

# Specify the container's executable
CMD ["./electronwall"]
Loading