Skip to content

Commit ab44406

Browse files
authored
Dockerfile Rework (#243)
1 parent a1bd8e8 commit ab44406

File tree

5 files changed

+115
-32
lines changed

5 files changed

+115
-32
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
name: Test
1+
name: CI - Build and Test Service
2+
23
on: [pull_request]
34
jobs:
4-
test:
5+
continuous-tests:
56
name: Test
67
runs-on: ubuntu-latest
78
steps:
89
- name: Set up Go
910
uses: actions/setup-go@v5
1011
with:
11-
go-version: '1.23'
12+
go-version: '1.24'
1213

1314
- name: Check out code into the Go module directory
1415
uses: actions/checkout@v4
@@ -23,4 +24,4 @@ jobs:
2324
run: go install golang.org/x/lint/golint@latest && golint -set_exit_status ./...
2425

2526
- name: test
26-
run: go test -timeout 30s -race ./...
27+
run: go test -timeout 30s -race ./...

Dockerfile

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,79 @@
1+
ARG CONTEXT=prod
2+
13
FROM golang:1.24.4-alpine as base
2-
WORKDIR /root/openslides-search-service
34

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

613
COPY go.mod go.sum ./
714
RUN go mod download
815

916
COPY cmd cmd
1017
COPY pkg pkg
1118

12-
# Build service in seperate stage.
13-
FROM base as builder
14-
RUN go build -o openslides-search-service cmd/searchd/main.go
15-
16-
17-
# Test build.
18-
FROM base as testing
19-
20-
RUN apk add build-base
21-
22-
CMD go vet ./... && go test -test.short ./...
23-
19+
## External Information
20+
EXPOSE 9050
2421

25-
# Development build.
26-
FROM base as development
22+
# Development Image
23+
FROM base as dev
2724

2825
RUN ["go", "install", "github.com/githubnemo/CompileDaemon@latest"]
29-
EXPOSE 9050
3026

31-
WORKDIR /root
3227
COPY entrypoint.sh ./
3328
COPY meta/search.yml .
3429
COPY meta/models.yml .
30+
31+
## Entrypoint
3532
ENTRYPOINT ["./entrypoint.sh"]
3633

37-
CMD CompileDaemon -log-prefix=false -build="go build -o search-service ./openslides-search-service/cmd/searchd/main.go" -command="./search-service"
34+
## Command
35+
CMD CompileDaemon -log-prefix=false -build="go build -o openslides-search-service ./cmd/searchd/main.go" -command="./openslides-search-service"
36+
37+
# Testing Image
38+
FROM base as tests
3839

40+
COPY dev/container-tests.sh ./dev/container-tests.sh
3941

40-
# Productive build
41-
FROM alpine:3
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
4248

49+
## Command
50+
STOPSIGNAL SIGKILL
51+
CMD ["sleep", "inf"]
52+
53+
# Production Image
54+
FROM base as builder
55+
RUN go build -o openslides-search-service cmd/searchd/main.go
56+
57+
FROM alpine:3 as prod
58+
59+
## Setup
60+
ARG CONTEXT
61+
ENV APP_CONTEXT=prod
62+
63+
COPY entrypoint.sh /
64+
COPY meta/search.yml /
65+
COPY meta/models.yml /
66+
COPY --from=builder /app/openslides-search-service/openslides-search-service /
67+
68+
## External Information
4369
LABEL org.opencontainers.image.title="OpenSlides Search Service"
4470
LABEL org.opencontainers.image.description="The Search Service is a http endpoint where the clients can search for data within Openslides."
4571
LABEL org.opencontainers.image.licenses="MIT"
4672
LABEL org.opencontainers.image.source="https://github.com/OpenSlides/openslides-search-service"
4773

48-
COPY entrypoint.sh ./
49-
COPY meta/search.yml .
50-
COPY meta/models.yml .
51-
COPY --from=builder /root/openslides-search-service/openslides-search-service .
5274
EXPOSE 9050
75+
76+
## Command
5377
ENTRYPOINT ["./entrypoint.sh"]
78+
5479
CMD exec ./openslides-search-service

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
SERVICE=search
2+
3+
build-prod:
4+
docker build ./ --tag "openslides-$(SERVICE)" --build-arg CONTEXT="prod" --target "prod"
5+
16
build-dev:
2-
docker build . --target development --tag openslides-search-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"
311

412
run-tests:
5-
docker build . --target testing --tag openslides-search-test
6-
docker run openslides-search-test
13+
bash dev/run-tests.sh
714

815
all: gofmt gotest golinter
916

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 erronoeus exit code.
4+
5+
echo "########################################################################"
6+
echo "###################### Run Tests and Linters ###########################"
7+
echo "########################################################################"
8+
9+
# Setup
10+
IMAGE_TAG=openslides-search-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)