Skip to content

Commit e0002c9

Browse files
committed
feat: unify Dockerfile for CI/CD and local development, deprecate Dockerfile.ci
1 parent 0e4c991 commit e0002c9

4 files changed

Lines changed: 66 additions & 66 deletions

File tree

.github/workflows/docker-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
uses: docker/build-push-action@v5
3030
with:
3131
context: .
32-
file: ./Dockerfile.ci
32+
file: ./Dockerfile
3333
push: true
3434
tags: ${{ steps.meta.outputs.tags }}
3535
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,56 @@
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.
127
FROM nginx:alpine
228

3-
# Copy static files
29+
# Copy static files from the build context
430
COPY 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
1136
RUN mkdir -p /app/data && chown nginx:nginx /app/data
1237

13-
# Copy backend config
38+
# Create a directory for backend configs
1439
RUN mkdir -p /app/configs
40+
# Copy the config files from the build context
1541
COPY backend/configs/config.json /app/configs/config.json
1642
COPY backend/configs/airports.json /app/configs/airports.json
1743
COPY backend/configs/station_geo.json /app/configs/station_geo.json
1844

1945
# Copy the custom Nginx configuration
2046
COPY nginx.prod.conf /etc/nginx/nginx.conf
2147

22-
# Copy the entrypoint script
48+
# Copy the entrypoint script and make it executable
2349
COPY docker-entrypoint.sh /docker-entrypoint.sh
2450
RUN chmod +x /docker-entrypoint.sh
2551

2652
# Expose port 80 for Nginx
2753
EXPOSE 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"]

Dockerfile.ci

Lines changed: 0 additions & 56 deletions
This file was deleted.

Dockerfile.local

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM nginx:alpine
2+
3+
# Copy static files
4+
COPY static/ /usr/share/nginx/html
5+
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
9+
10+
# Create a directory for the backend data
11+
RUN mkdir -p /app/data && chown nginx:nginx /app/data
12+
13+
# Copy backend config
14+
RUN mkdir -p /app/configs
15+
COPY backend/configs/config.json /app/configs/config.json
16+
COPY backend/configs/airports.json /app/configs/airports.json
17+
COPY backend/configs/station_geo.json /app/configs/station_geo.json
18+
19+
# Copy the custom Nginx configuration
20+
COPY nginx.prod.conf /etc/nginx/nginx.conf
21+
22+
# Copy the entrypoint script
23+
COPY docker-entrypoint.sh /docker-entrypoint.sh
24+
RUN chmod +x /docker-entrypoint.sh
25+
26+
# Expose port 80 for Nginx
27+
EXPOSE 80
28+
29+
# Run the entrypoint script
30+
CMD ["/docker-entrypoint.sh"]

0 commit comments

Comments
 (0)