build: optimize

This commit is contained in:
kroyzen 2023-08-23 18:36:17 +03:00
parent 415b01cb1e
commit cc79aefe9b
7 changed files with 45 additions and 1086 deletions

@ -1,9 +1,9 @@
GRPC_HOST=0.0.0.0 GRPC_HOST=0.0.0.0
GPRC_PORT=9001 GPRC_PORT=9000
HTTP_PORT=8001 HTTP_PORT=8000
MONGO_HOST=mongo MONGO_HOST=discount-db
MONGO_PORT=27017 MONGO_PORT=27017
MONGO_USER=test MONGO_USER=test
MONGO_PASSWORD=test MONGO_PASSWORD=test

@ -1,71 +1,54 @@
# BUILD # BUILD
FROM golang:1.19.5-alpine AS build FROM golang:1.20.3-alpine AS build
# Update depences # Update packages and clear cache
RUN apk update && apk add --no-cache curl RUN apk update && apk add --no-cache curl && rm -rf /var/cache/apk/*
# Create build directory # Set work directory
RUN mkdir /app/bin -p
RUN mkdir /bin/golang-migrate -p
# Download migrate app
RUN GOLANG_MIGRATE_VERSION=v4.15.1 && \
curl -L https://github.com/golang-migrate/migrate/releases/download/${GOLANG_MIGRATE_VERSION}/migrate.linux-amd64.tar.gz |\
tar xvz migrate -C /bin/golang-migrate
# Download health check utility
RUN GRPC_HEALTH_PROBE_VERSION=v0.4.6 && \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
chmod +x /bin/grpc_health_probe
# Set home directory
WORKDIR /app WORKDIR /app
# Copy go.mod # Create binary directory
ADD go.mod go.sum /app/ RUN mkdir /app/bin -p
# Create golang migrate util directory
RUN mkdir /bin/golang-migrate -p
# Add migrate tool
ADD ./tools/migrate /bin/golang-migrate/
# Add main files to app
ADD . .
# Download go depences # Download go depences
RUN go mod download RUN go mod download
# Copy all local files
ADD . /app
# Build app # Build app
RUN GOOS=linux go build -o bin ./... RUN GOOS=linux go build -o bin ./...
# TEST # TEST
FROM alpine:latest AS test FROM alpine:3.18.3 AS test
# Install packages # Install packages
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates && rm -rf /var/cache/apk/*
# Set GO111MODULE env
ENV GO111MODULE=off
# Create home directory # Create home directory
WORKDIR /app WORKDIR /app
# Copy build file # Copy build file
COPY --from=build /app/bin/app ./app COPY --from=build /app/bin/app ./app
# CMD
CMD ["./app"]
# MIGRATION
FROM alpine:latest AS migration
# Install packages
RUN apk --no-cache add ca-certificates
# Create home directory
WORKDIR /app
# Copy migration dir # Copy migration dir
COPY --from=build /app/migrations/tests ./migrations COPY --from=build /app/migrations/test ./migrations
# Install migrate tool # Install migrate tool
COPY --from=build /bin/golang-migrate /usr/local/bin COPY --from=build /bin/golang-migrate /usr/local/bin
# CMD
CMD [ "./app" ]
# PRODUCTION # PRODUCTION
FROM alpine:latest AS production FROM alpine:3.18.3 AS production
# Install packages # Install packages
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates && rm -rf /var/cache/apk/*
# Create home directory # Create home directory
WORKDIR /app WORKDIR /app
# Copy build file # Copy build file
COPY --from=build /app/bin/app ./app COPY --from=build /app/bin/app ./app
# Copy grpc health probe dir
COPY --from=build /bin/grpc_health_probe /bin/grpc_health_probe
# Install migrate tool # Install migrate tool
COPY --from=build /bin/golang-migrate /usr/local/bin COPY --from=build /bin/golang-migrate /usr/local/bin
# CMD # CMD

@ -1,46 +1,51 @@
version: "3" version: "3"
services: services:
app: discount-service:
container_name: discount-service
build: build:
context: ../../. context: ../../.
dockerfile: Dockerfile dockerfile: Dockerfile
target: test target: test
env_file: env_file:
- ../../.env.test - ../../.env.test
ports:
- 9001:9001
- 8001:8001
depends_on: depends_on:
- migration - discount-db
- discount-migration
ports:
- 9040:9000
- 8008:8000
networks: networks:
- integration_test - test
migration: discount-migration:
container_name: discount-migration
build: build:
context: ../../. context: ../../.
dockerfile: Dockerfile dockerfile: Dockerfile
target: migration target: test
command: command:
[ [
"sh", "sh",
"-c", "-c",
'migrate -source file://migrations -database "mongodb://$MONGO_USER:$MONGO_PASSWORD@$MONGO_HOST:$MONGO_PORT/$MONGO_AUTH?authSource=$MONGO_AUTH" up', 'migrate -source file://migrations -database "mongodb://test:test@discount-db:27017/admin?authSource=admin" up',
] ]
depends_on: depends_on:
- mongo - discount-db
networks: networks:
- integration_test - test
mongo: discount-db:
image: 'mongo:6.0.3' container_name: discount-db
image: "mongo:6.0.3"
environment: environment:
MONGO_INITDB_ROOT_USERNAME: test MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: test MONGO_INITDB_ROOT_PASSWORD: test
ports: ports:
- '27017:27017' - "27023:27017"
networks: networks:
- integration_test - test
networks: networks:
integration_test: test:

File diff suppressed because it is too large Load Diff

BIN
tools/migrate Normal file

Binary file not shown.