Skip to content

Commit

Permalink
Support multi-stage builds for Management & Treatment Services Docker…
Browse files Browse the repository at this point in the history
…file (#66)

* Update management dockerfile

* Make binary executable

* Add multi-stage build

* Update workflow versions

* Update Treatment Service Dockerfile and deps

* Make Management Service Dockerfile multi-step build

* Remove binary download step in release workflow

* Copy API specs

* Standardize schema file copy

* Address PR comments
  • Loading branch information
terryyylim committed Mar 14, 2023
1 parent 69c6158 commit 12e2691
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
api-version: ${{ steps.build-image.outputs.api-version }}
steps:
- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
api-version: ${{ steps.build-image.outputs.api-version }}
steps:
- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/xp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
env:
PYTHON: 3.7
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup Python
id: setup-python
uses: actions/setup-python@v2
Expand All @@ -56,21 +56,21 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- uses: actions/checkout@v3
- name: Lint Common module
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: ${{ env.GO_LINT_VERSION }}
working-directory: common
args: --timeout 3m --verbose --skip-files=testutils/mocks/ManagementClientInterface.go
- name: Lint Management Service module
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: ${{ env.GO_LINT_VERSION }}
working-directory: management-service
skip-pkg-cache: true
skip-build-cache: true
args: --timeout 3m --verbose
- name: Lint Treatment Service module
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: ${{ env.GO_LINT_VERSION }}
working-directory: treatment-service
Expand Down Expand Up @@ -221,7 +221,7 @@ jobs:
outputs:
release-type: ${{ steps.release-rules.outputs.release-type }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- id: release-rules
uses: ./.github/actions/release-rules

Expand Down
21 changes: 19 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
FROM golang:1.18-alpine as api-builder
ARG API_BIN_NAME=xp-management

ENV GO111MODULE=on \
GOOS=linux \
GOARCH=amd64

WORKDIR /app
COPY . .

# Build Management Service binary
WORKDIR /app/management-service
RUN go build \
-mod=vendor \
-o ./bin/${API_BIN_NAME}

FROM alpine:latest

# Install bash
Expand All @@ -19,8 +35,9 @@ RUN addgroup -S ${XP_USER_GROUP} \
&& mkdir /app \
&& chown -R ${XP_USER}:${XP_USER_GROUP} /app

COPY --chown=${XP_USER}:${XP_USER_GROUP} management-service/bin/* /app/
COPY --chown=${XP_USER}:${XP_USER_GROUP} management-service/database /app/database/
COPY --chown=${XP_USER}:${XP_USER_GROUP} management-service/bin/*.yaml /app/
COPY --from=api-builder --chown=${XP_USER}:${XP_USER_GROUP} /app/management-service/bin/* /app/
COPY --from=api-builder --chown=${XP_USER}:${XP_USER_GROUP} /app/management-service/database /app/database/

USER ${XP_USER}
WORKDIR /app
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ lint: lint-python lint-go
.PHONY: vendor
vendor:
@echo "Fetching dependencies..."
go mod vendor
cd common && go mod vendor
cd clients && go mod vendor
cd management-service && go mod vendor

.PHONY: version
version:
Expand Down Expand Up @@ -171,7 +173,7 @@ build-treatment-service: version
build: build-management-service build-treatment-service

.PHONY: build-image
build-image: version
build-image: version vendor
@$(eval IMAGE_TAG = $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/,)${BIN_NAME}:${VERSION})
@echo "Building docker image: ${IMAGE_TAG}"
docker build --tag ${IMAGE_TAG} .
Expand Down
30 changes: 25 additions & 5 deletions treatment-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
FROM alpine:latest
FROM golang:1.18-alpine as api-builder
ARG API_BIN_NAME=xp-treatment

RUN apk update && apk add musl-dev gcc

ENV GO111MODULE=on \
GOOS=linux \
GOARCH=amd64

WORKDIR /app
COPY . .

RUN apk update && apk add git make bash unzip libtool gcc musl-dev ca-certificates dumb-init tzdata
# Build Treatment Service binary
RUN go build \
-mod=vendor \
-tags musl \
-o ./bin/${API_BIN_NAME}

FROM alpine:latest

RUN apk update && apk add git make bash unzip libtool ca-certificates dumb-init tzdata
RUN mkdir -p /opt/xp_treatment

COPY . /opt/xp_treatment/
COPY ./bin/* /opt/xp_treatment/
COPY --from=api-builder /app/bin/* /opt/xp_treatment/

WORKDIR /opt/xp_treatment
ENTRYPOINT [ "./xp-treatment" ]

ARG API_BIN_NAME=xp-treatment
ENV XP_API_BIN "./${API_BIN_NAME}"

ENTRYPOINT ["sh", "-c", "${XP_API_BIN} \"$@\"", "--"]
2 changes: 1 addition & 1 deletion treatment-service/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version:

build-treatment-service: version
@echo "Building binary..."
@cd ${TREATMENT_SVC_PATH} && go build -o ./bin/${TREATMENT_SVC_BIN_NAME}
@go build -o ./bin/${TREATMENT_SVC_BIN_NAME}
@cp api/treatment.yaml ${TREATMENT_SVC_PATH}/bin
@cp api/schema.yaml ${TREATMENT_SVC_PATH}/bin

Expand Down

0 comments on commit 12e2691

Please sign in to comment.