1+ # Dockerfile for CI/CD - includes the Go build environment.
2+ # This file compiles the Go binary and then packages it into a final nginx image.
3+
4+ # ---- Builder Stage ----
5+ # Use a specific Go version on Alpine for a smaller build image.
6+ FROM golang:1.21-alpine AS builder
7+
8+ # Set the working directory inside the container
9+ WORKDIR /src
10+
11+ # Copy go.mod and go.sum to leverage Docker cache
12+ COPY backend/go.mod backend/go.sum ./
13+ # Download dependencies
14+ RUN go mod download
15+
16+ # Copy the entire backend source code
17+ COPY backend/ ./
18+
19+ # Build the Go binary.
20+ # CGO_ENABLED=0 creates a static binary without C dependencies.
21+ # -w -s flags strip debug info, reducing the binary size.
22+ RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /app/roadbook-api ./cmd/roadbook-api/main.go
23+
24+
25+ # ---- Final Stage ----
26+ # Use the same nginx:alpine base as the local Dockerfile.
127FROM nginx:alpine
228
3- # Copy static files
29+ # Copy static files from the build context
430COPY static/ /usr/share/nginx/html
531
6- # Copy the backend executable, which is built locally
7- # and located in the 'backend' directory.
8- COPY backend/roadbook-api /usr/local/bin/roadbook-api
32+ # Copy the compiled backend executable from the builder stage
33+ COPY --from=builder /app/roadbook-api /usr/local/bin/roadbook-api
934
10- # Create a directory for the backend data
35+ # Create a directory for backend data and set ownership to the nginx user
1136RUN mkdir -p /app/data && chown nginx:nginx /app/data
1237
13- # Copy backend config
38+ # Create a directory for backend configs
1439RUN mkdir -p /app/configs
40+ # Copy the config files from the build context
1541COPY backend/configs/config.json /app/configs/config.json
1642COPY backend/configs/airports.json /app/configs/airports.json
1743COPY backend/configs/station_geo.json /app/configs/station_geo.json
1844
1945# Copy the custom Nginx configuration
2046COPY nginx.prod.conf /etc/nginx/nginx.conf
2147
22- # Copy the entrypoint script
48+ # Copy the entrypoint script and make it executable
2349COPY docker-entrypoint.sh /docker-entrypoint.sh
2450RUN chmod +x /docker-entrypoint.sh
2551
2652# Expose port 80 for Nginx
2753EXPOSE 80
2854
29- # Run the entrypoint script
30- CMD ["/docker-entrypoint.sh" ]
55+ # Set the default command to run the entrypoint script
56+ CMD ["/docker-entrypoint.sh" ]
0 commit comments