-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
51 lines (35 loc) · 1.28 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Build stage
FROM golang:1.23-alpine AS builder
# Install git and SS certificates
RUN apk add --no-cache git ca-certificates tzdata
# Set the working directory inside the container
WORKDIR /app
# Copy go.mod and go.sum files to the working directory
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy the entire source code to the working directory
COPY . .
# Build the Go application for linux with CGO disabled
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/urlshortener ./cmd/api
# Final stage
FROM alpine:latest
# Import SSL certificates and timezone data from the builder stage
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
# Create a non-root user
RUN adduser -D -g '' appuser
# Copy the built binary from the builder stage
COPY --from=builder /app/urlshortener /app/urlshortener
# Copy the .env file to the working directory
COPY .env /app/.env
# Set the working directory inside the container
WORKDIR /app
# Change ownership of the /app directory to the non-root user
RUN chown -R appuser:appuser /app
# Switch to the non-root user
USER appuser
# Expose the port the application will run on
EXPOSE 8080
# Set the entrypoint to the built binary
ENTRYPOINT ["/app/urlshortener"]