From 3a4d901f6d8211c023e17c26aab686571e5079fb Mon Sep 17 00:00:00 2001 From: Jinn Koriech Date: Thu, 25 Mar 2021 08:56:55 +0000 Subject: [PATCH] Docker: build rest-server at container build time Using docker's multi-stage builds we can build the restic/rest-server within a golang build environment then create a container for use (without the build environment) in a second build stage. The advantages are: 1. Building the rest-server is predictable in a pristine environment each time. 2. Container builds ensure we get the latest rest-server every time. Updated README with details of new docker build approach, and added changelog for unreleased changes. --- Dockerfile | 13 ++++++++++++- README.md | 7 ++++++- changelog/unreleased/issue-146 | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/issue-146 diff --git a/Dockerfile b/Dockerfile index 50fe70f..2debe41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,14 @@ +FROM golang:alpine AS builder + +ENV CGO_ENABLED 0 + +COPY . /build +WORKDIR /build +RUN go build -o rest-server ./cmd/rest-server + + + + FROM alpine ENV DATA_DIRECTORY /data @@ -8,7 +19,7 @@ RUN apk add --no-cache --update apache2-utils COPY docker/create_user /usr/bin/ COPY docker/delete_user /usr/bin/ COPY docker/entrypoint.sh /entrypoint.sh -COPY rest-server /usr/bin +COPY --from=builder /build/rest-server /usr/bin VOLUME /data EXPOSE 8000 diff --git a/README.md b/README.md index 00bd813..d8fc99d 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,15 @@ The required version of restic backup client to use with `rest-server` is [v0.7. For building the `rest-server` binary run `CGO_ENABLED=0 go build -o rest-server ./cmd/rest-server` + ## Docker ### Build image -Put the `rest-server` binary in the current directory, then run: +The docker image build process will build a fresh version of the rest-server and package that into +a usable container. + +To build the binary along with the container run: docker build -t restic/rest-server:latest . @@ -32,6 +36,7 @@ Put the `rest-server` binary in the current directory, then run: docker pull restic/rest-server + ## Usage To learn how to use restic backup client with REST backend, please consult [restic manual](https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html#rest-server). diff --git a/changelog/unreleased/issue-146 b/changelog/unreleased/issue-146 new file mode 100644 index 0000000..5772ea2 --- /dev/null +++ b/changelog/unreleased/issue-146 @@ -0,0 +1,5 @@ +Build rest-server at docker container build time + +Add a build stage such that the latest rest-server is always built and packaged. +This is done in a standard golang container to ensure a clean build environment +and only the final binary is shipped rather than the whole build environment.