Skip to content

Commit 95a21a9

Browse files
authored
Dockerfile Rework (#251)
1 parent a562a2b commit 95a21a9

File tree

6 files changed

+150
-61
lines changed

6 files changed

+150
-61
lines changed

.github/workflows/startup.yml renamed to .github/workflows/continuous-integration.yml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1-
name: Check building and Makefile
1+
name: CI - Build and Test Service
2+
23
on: [pull_request]
34
jobs:
5+
continuous-tests:
6+
name: Run tests
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Set up Go
11+
uses: actions/setup-go@v2.1.3
12+
with:
13+
go-version: 1.24
14+
15+
- name: Check out code into the Go module directory
16+
uses: actions/checkout@v2
17+
18+
- name: go fmt
19+
run: test -z $(gofmt -l .)
20+
21+
- name: go vet
22+
run: go vet ./...
23+
24+
- name: golint
25+
run: go install golang.org/x/lint/golint@latest && golint -set_exit_status ./...
26+
27+
- name: go test
28+
run: go test -timeout 10s -race ./...
29+
430
startup:
531
name: Building of Go binaries and Docker images and test some Makefile targets
632
runs-on: ubuntu-latest
@@ -27,21 +53,28 @@ jobs:
2753
go build ./cmd/openslides
2854
./openslides
2955
56+
- name: Build Prod
57+
run: make build-prod
58+
3059
- name: Build and start server with Docker
3160
run: |
32-
docker build . --tag openslides-manage
3361
timeout --preserve-status --signal SIGINT 5s docker run --env MANAGE_AUTH_PASSWORD_FILE=/manage_auth_password --volume $PWD/manage_auth_password:/manage_auth_password openslides-manage
3462
env:
3563
DOCKER_BUILDKIT: 1
3664

65+
- name: Build Dev
66+
run: make build-dev
67+
3768
- name: Start development version of server with Makefile
3869
run: |
39-
make build-dev
4070
timeout --preserve-status --signal SIGINT 5s docker run --env MANAGE_AUTH_PASSWORD_FILE=/manage_auth_password --volume $PWD/manage_auth_password:/manage_auth_password openslides-manage-dev
4171
env:
4272
DOCKER_BUILDKIT: 1
4373

74+
- name: Build Tests
75+
run: make build-test
76+
4477
- name: Run tests with Makefile
4578
run: make run-tests
4679
env:
47-
DOCKER_BUILDKIT: 1
80+
DOCKER_BUILDKIT: 1

.github/workflows/test.yml

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

Dockerfile

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
FROM golang:1.19-alpine as base
1+
ARG CONTEXT=prod
22

3-
WORKDIR /root
3+
FROM golang:1.24.4-alpine as base
44

5-
RUN apk add git
5+
## Setup
6+
ARG CONTEXT
7+
WORKDIR /app
8+
ENV APP_CONTEXT=${CONTEXT}
9+
10+
## Installs
11+
RUN apk add git --no-cache
612

713
COPY go.mod go.sum ./
814
RUN go mod download
@@ -12,50 +18,70 @@ COPY pkg pkg
1218
COPY proto proto
1319
COPY Makefile Makefile
1420

21+
## External Information
22+
EXPOSE 9008
1523

16-
# Build service in seperate stage.
17-
FROM base as builder
24+
## Healthcheck
25+
HEALTHCHECK CMD ["/app/healthcheck"]
1826

19-
RUN CGO_ENABLED=0 go build ./cmd/openslides
20-
RUN CGO_ENABLED=0 go build ./cmd/server
21-
RUN CGO_ENABLED=0 go build ./cmd/healthcheck
27+
# Development Image
2228

29+
FROM base as dev
2330

24-
# Test build.
25-
FROM base as testing
31+
RUN ["go", "install", "github.com/githubnemo/CompileDaemon@latest"]
2632

27-
RUN apk add build-base
33+
## Command
34+
CMD CompileDaemon -log-prefix=false -build="go build ./cmd/server" -command="./server"
2835

29-
CMD make test
36+
# Testing Image
3037

38+
FROM base as tests
3139

32-
# Development build.
33-
FROM base as development
40+
COPY dev/container-tests.sh ./dev/container-tests.sh
3441

35-
RUN ["go", "install", "github.com/githubnemo/CompileDaemon@latest"]
36-
EXPOSE 9008
42+
RUN apk add --no-cache \
43+
build-base \
44+
docker && \
45+
go get -u github.com/ory/dockertest/v3 && \
46+
go install golang.org/x/lint/golint@latest && \
47+
chmod +x dev/container-tests.sh
3748

38-
CMD CompileDaemon -log-prefix=false -build="go build ./cmd/server" -command="./server"
49+
## Command
50+
STOPSIGNAL SIGKILL
51+
CMD ["sleep", "inf"]
52+
53+
# Production Image
54+
55+
FROM base as builder
3956

57+
RUN CGO_ENABLED=0 go build ./cmd/openslides && \
58+
CGO_ENABLED=0 go build ./cmd/server && \
59+
CGO_ENABLED=0 go build ./cmd/healthcheck
4060

41-
# Productive build (client) tool.
4261
FROM scratch as client
43-
COPY --from=builder /root/openslides .
62+
63+
WORKDIR /
64+
ENV APP_CONTEXT=prod
65+
66+
COPY --from=builder /app/openslides .
67+
4468
ENTRYPOINT ["/openslides"]
4569

70+
FROM scratch as prod
4671

47-
# Productive build server.
48-
FROM scratch
72+
## Setup
73+
ARG CONTEXT
74+
ENV APP_CONTEXT=prod
4975

5076
LABEL org.opencontainers.image.title="OpenSlides Manage Service"
5177
LABEL org.opencontainers.image.description="Manage service and tool for OpenSlides which \
52-
provides some management commands to setup and control OpenSlides instances."
78+
provides some management commands to setup and control OpenSlides instances."
5379
LABEL org.opencontainers.image.licenses="MIT"
5480
LABEL org.opencontainers.image.source="https://github.com/OpenSlides/openslides-manage-service"
5581
LABEL org.opencontainers.image.documentation="https://github.com/OpenSlides/openslides-manage-service/blob/main/README.md"
5682

57-
COPY --from=builder /root/healthcheck .
58-
COPY --from=builder /root/server .
83+
COPY --from=builder /app/healthcheck /
84+
COPY --from=builder /app/server /
5985

6086
EXPOSE 9008
6187

Makefile

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
all: openslides
1+
SERVICE=manage
2+
3+
build-prod:
4+
docker build ./ --tag "openslides-$(SERVICE)" --build-arg CONTEXT="prod" --target "prod"
25

36
build-dev:
4-
docker build . --target development --tag openslides-manage-dev
7+
docker build ./ --tag "openslides-$(SERVICE)-dev" --build-arg CONTEXT="dev" --target "dev"
8+
9+
build-test:
10+
docker build ./ --tag "openslides-$(SERVICE)-tests" --build-arg CONTEXT="tests" --target "tests"
11+
12+
all: openslides
513

614
run-tests:
7-
docker build . --target testing --tag openslides-manage-test
8-
docker run openslides-manage-test
15+
bash dev/run-tests.sh
916

1017
test:
1118
# Attention: This steps should be the same as in .github/workflows/test.yml.
@@ -31,8 +38,8 @@ openslides:
3138
if [ $(shell whoami) != root ]; then \
3239
addgroup -g $(shell id -g) build ; \
3340
adduser -u $(shell id -u) -G build -D build ; \
34-
chown build: /root/openslides ; \
41+
chown build: /app/openslides ; \
3542
fi; \
36-
cp -p /root/openslides /build/"
43+
cp -p /app/openslides /build/"
3744

3845
.PHONY: openslides

dev/container-tests.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
dockerd --storage-driver=vfs --log-level=error &
4+
5+
# Close Dockerd savely on exit
6+
DOCKERD_PID=$!
7+
trap 'kill $DOCKERD_PID' EXIT INT TERM ERR
8+
9+
RETRY=0
10+
MAX=10
11+
until docker info >/dev/null 2>&1
12+
do
13+
if [ "$RETRY" -ge "$MAX" ]
14+
then
15+
echo "Dockerd setup error"
16+
exit 1
17+
fi
18+
sleep 1
19+
RETRY=$((RETRY + 1))
20+
echo "Waiting for dockerd $RETRY/$MAX"
21+
done
22+
23+
echo "Started dockerd"
24+
25+
CATCH=0
26+
27+
# Run Linters & Tests
28+
go vet ./... || CATCH=1
29+
go test -timeout 60s -race ./... || CATCH=1
30+
gofmt -l . || CATCH=1
31+
golint -set_exit_status ./... || CATCH=1
32+
33+
exit $CATCH

dev/run-tests.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# Executes all tests. Should errors occur, CATCH will be set to 1, causing an erroneous exit code.
4+
5+
echo "########################################################################"
6+
echo "###################### Run Tests and Linters ###########################"
7+
echo "########################################################################"
8+
9+
# Setup
10+
IMAGE_TAG=openslides-manage-tests
11+
12+
# Safe Exit
13+
trap 'docker stop $(docker ps -a -q --filter ancestor=${IMAGE_TAG})' EXIT
14+
15+
# Execution
16+
make build-test
17+
docker run --privileged -t ${IMAGE_TAG} ./dev/container-tests.sh

0 commit comments

Comments
 (0)