Merge pull request #100 from lsmoura/sergio/simple-docker

Improve docker images by using alpine, simplifying Dockerfiles, and improving the docker-compose example
This commit is contained in:
David Dworken 2023-09-07 18:11:32 -07:00 committed by GitHub
commit 4d0b45d975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 29 deletions

View File

@ -25,11 +25,11 @@ release:
git push && git push --tags
build-static:
ssh server "cd ~/code/hishtory/; git pull; docker build -t gcr.io/dworken-k8s/hishtory-static -f backend/web/caddy/Dockerfile ."
ssh server "cd ~/code/hishtory/; git pull; docker build --build-arg GOARCH=amd64 --tag gcr.io/dworken-k8s/hishtory-static --file backend/web/caddy/Dockerfile ."
build-api:
rm hishtory server || true
docker build -t gcr.io/dworken-k8s/hishtory-api -f backend/server/Dockerfile .
docker build --build-arg GOARCH=amd64 --tag gcr.io/dworken-k8s/hishtory-api --file backend/server/Dockerfile .
deploy-static: build-static
ssh server "docker push gcr.io/dworken-k8s/hishtory-static"

View File

@ -1,10 +1,14 @@
FROM golang:1.18 AS builder
COPY go.mod ./
COPY go.sum ./
RUN unset GOPATH; go mod download
COPY . ./
RUN unset GOPATH; GOARCH=amd64 go build -o /server -ldflags "-X main.ReleaseVersion=v0.`cat VERSION`" backend/server/server.go
FROM golang:1.18-alpine3.17 AS builder
FROM golang:1.18
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
ARG GOARCH
RUN apk add --update --no-cache --virtual .build-deps build-base && \
GOARCH=${GOARCH} go build -o /server -ldflags "-X main.ReleaseVersion=v0.`cat VERSION`" backend/server/server.go && \
apk del .build-deps
FROM alpine:3.17
COPY --from=builder /server /server
CMD ["/server"]

View File

@ -5,6 +5,7 @@
# 4. Point your hiSHtory client at the server by putting `export HISHTORY_SERVER=http://1.2.3.4` in your shellrc
# 5. Run `hishtory init` to initialize hiSHtory with the local server
# 6. [Optional, but recommended] Add a TLS proxy to enable https
version: "3.8"
networks:
hishtory:
driver: bridge
@ -19,17 +20,28 @@ services:
POSTGRES_DB: hishtory
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- ./postgres-data:/var/lib/postgresql/data
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: pg_isready -U postgres
interval: 10s
timeout: 3s
hishtory:
depends_on:
- postgres
postgres:
condition: service_healthy
networks:
- hishtory
build:
context: ../../
dockerfile: ./backend/server/native-arch-Dockerfile
dockerfile: ./backend/server/Dockerfile
restart: unless-stopped
deploy:
restart_policy:
condition: on-failure
delay: 3s
environment:
HISHTORY_POSTGRES_DB: postgresql://postgres:TODO_YOUR_POSTGRES_PASSWORD_HERE@postgres:5432/hishtory?sslmode=disable
ports:
- 80:8080
volumes:
postgres-data:

View File

@ -1,17 +0,0 @@
# A fork of Dockerfile that doesn't hard code GOARCH and that uses wait-for to wait
# until the postgres server is up. Meant to be used in the docker-compose file for self hosting.
FROM golang:1.18 AS builder
COPY go.mod ./
COPY go.sum ./
RUN unset GOPATH; go mod download
COPY . ./
RUN unset GOPATH; go build -o /server -ldflags "-X main.ReleaseVersion=v0.`cat VERSION`" backend/server/server.go
FROM golang:1.18
RUN apt-get update && apt-get install -y netcat
# Downlaod wait-for from a specific commit hash. This ensures that the owner of wait-for isn't in our TCB (though Github still is)
RUN curl https://raw.githubusercontent.com/eficode/wait-for/59bec22851ba83e9cc735a67a7d961f8aae2cd85/wait-for > /wait-for
RUN chmod +x /wait-for
COPY --from=builder /server /server
CMD ["/wait-for", "postgres:5432", "--", "/server"]