Skip to content

Commit

Permalink
chore: optimize docker files
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Apr 10, 2023
1 parent 34587d6 commit d33565e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
depends_on:
- api
ports:
- "${WEB_EXPORT_PORT}:3000"
- "${WEB_EXPORT_PORT}:80"
networks:
- sxcctw_web
environment:
Expand Down
29 changes: 19 additions & 10 deletions build/sxcctw_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
FROM golang:1.19-alpine
# First stage: Build the Go application
FROM golang:alpine AS build

WORKDIR /usr/src/app
# Set the working directory
WORKDIR /app

RUN apk add --no-cache git

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
# Copy go.mod and go.sum
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download && go mod verify

# Copy the rest of the application files
COPY . .
RUN go build -v -o /usr/local/bin/app ./cmd/sxcctw_api/ \
&& go clean -fuzzcache \
&& go clean -testcache \
&& go clean -modcache

CMD ["app"]
# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/bin/app ./cmd/sxcctw_api/

# Second stage: Serve the Go application
FROM alpine

# Copy the built binary from the build stage
COPY --from=build /go/bin/app /go/bin/app

# Start the Go application
CMD ["/go/bin/app"]
29 changes: 19 additions & 10 deletions build/sxcctw_db/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
FROM golang:1.19-alpine
# First stage: Build the Go application
FROM golang:alpine AS build

WORKDIR /usr/src/app
# Set the working directory
WORKDIR /app

RUN apk add --no-cache git

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
# Copy go.mod and go.sum
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download && go mod verify

# Copy the rest of the application files
COPY . .
RUN go build -v -o /usr/local/bin/app ./cmd/sxcctw_db/ \
&& go clean -fuzzcache \
&& go clean -testcache \
&& go clean -modcache

CMD ["app"]
# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/bin/app ./cmd/sxcctw_db/

# Second stage: Serve the Go application
FROM alpine

# Copy the built binary from the build stage
COPY --from=build /go/bin/app /go/bin/app

# Start the Go application
CMD ["/go/bin/app"]
44 changes: 28 additions & 16 deletions build/sxcctw_web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
FROM node:alpine
# First stage: Build the application
FROM node:alpine AS build

ARG WORKPLACE=/sxcctw-web
ENV WEBPORT=$WEBPORT
# Install pnpm globally
RUN npm install -g pnpm

COPY ./web $WORKPLACE
# Set the working directory
WORKDIR /app

WORKDIR $WORKPLACE
# Copy package.json and pnpm-lock.yaml
COPY package.json pnpm-lock.yaml ./

RUN npm i -g pnpm serve && \
SHELL="/bin/bash" pnpm setup && \
pnpm i --frozen-lockfile && \
pnpm build
# Install dependencies
RUN pnpm install --frozen-lockfile

RUN mkdir ../to_rm && \
mv ./* ../to_rm && \
mv ../to_rm/dist ./ && \
rm -rf ../to_rm
# Copy the rest of the application files
COPY . .

ENV NODE_ENV=production
# Build the application
RUN pnpm build

# The default export port will be set to 3000.
CMD ["serve", "-s", "dist"]
# Second stage: Serve the application with NGINX
FROM nginx:stable-alpine

# Copy built files from the build stage to the NGINX html directory
COPY --from=build /app/dist /usr/share/nginx/html

# Copy an optional custom nginx configuration file if needed
# COPY nginx.conf /etc/nginx/conf.d/default.conf

# Expose port 80
EXPOSE 80

# Start NGINX
CMD ["nginx", "-g", "daemon off;"]

0 comments on commit d33565e

Please sign in to comment.