From fcb9d6b31f823fc82bb9c33568d4d1e3e830dd2e Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 9 Oct 2022 11:08:23 -0700 Subject: [PATCH] working couchdb reverse proxy behind webapp binary. working dockerfile and dockercompose file added s6-overlay to base image, built ontop of couchdb now. Bootstrapping of couchdb now creates _users and other system databases. --- Dockerfile | 27 +++++++++++++++-- docker-compose.yml | 4 ++- docker/couchdb/Dockerfile | 14 ++++++++- docker/rootfs/etc/cont-init.d/01-timezone | 7 +++++ docker/rootfs/etc/cont-init.d/50-couchdb-init | 30 +++++++++++++++++++ docker/rootfs/etc/services.d/couchdb/run | 4 +++ docker/rootfs/etc/services.d/fasten/run | 7 +++++ 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 docker/rootfs/etc/cont-init.d/01-timezone create mode 100644 docker/rootfs/etc/cont-init.d/50-couchdb-init create mode 100644 docker/rootfs/etc/services.d/couchdb/run create mode 100644 docker/rootfs/etc/services.d/fasten/run diff --git a/Dockerfile b/Dockerfile index b2d0b72f..2f2bca7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,6 @@ +######################################################################################################### +# Frontend Build +######################################################################################################### FROM node:18.9.0 as frontend-build WORKDIR /usr/src/fastenhealth/frontend #COPY frontend/package.json frontend/yarn.lock ./ @@ -8,6 +11,9 @@ RUN yarn config set registry "http://registry.npmjs.org" \ COPY frontend/ ./ RUN yarn run build -- --configuration sandbox --output-path=../dist +######################################################################################################### +# Backend Build +######################################################################################################### FROM golang:1.18 as backend-build WORKDIR /go/src/github.com/fastenhealth/fastenhealth-onprem COPY . . @@ -26,12 +32,29 @@ RUN mkdir -p /opt/fasten/db \ && curl -o /opt/fasten/db/fasten.db -L https://github.com/fastenhealth/testdata/raw/main/fasten.db +######################################################################################################### +# Distribution Build +######################################################################################################### +FROM couchdb:3.2 + +ARG S6_ARCH=amd64 +RUN curl https://github.com/just-containers/s6-overlay/releases/download/v1.21.8.0/s6-overlay-${S6_ARCH}.tar.gz -L -s --output /tmp/s6-overlay-${S6_ARCH}.tar.gz \ + && tar xzf /tmp/s6-overlay-${S6_ARCH}.tar.gz -C / \ + && rm -rf /tmp/s6-overlay-${S6_ARCH}.tar.gz + +COPY /docker/couchdb/local.ini /opt/couchdb/etc/ +COPY /docker/rootfs / -FROM gcr.io/distroless/static-debian11 WORKDIR /opt/fasten/ COPY --from=backend-build /opt/fasten/ /opt/fasten/ COPY --from=frontend-build /usr/src/fastenhealth/dist /opt/fasten/web COPY --from=backend-build /go/bin/fasten /opt/fasten/fasten COPY LICENSE.md /opt/fasten/LICENSE.md COPY config.yaml /opt/fasten/config/config.yaml -CMD ["/opt/fasten/fasten", "start", "--config", "/opt/fasten/config/config.yaml"] + +ENTRYPOINT ["/init"] + + + + + diff --git a/docker-compose.yml b/docker-compose.yml index cc2fb3b2..07f9590e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,13 +2,15 @@ version: "3.9" services: couchdb: build: - context: ./docker/couchdb + context: . dockerfile: Dockerfile # environment: # - COUCHDB_USER=admin # - COUCHDB_PASSWORD=password ports: + - "9090:8080" - "5984:5984" volumes: - ./.couchdb/data:/opt/couchdb/data - ./.couchdb/config:/opt/couchdb/etc/local.d +# - ./config.example.yaml:/opt/fasten/config/config.yaml diff --git a/docker/couchdb/Dockerfile b/docker/couchdb/Dockerfile index 7fbc2096..52311df5 100644 --- a/docker/couchdb/Dockerfile +++ b/docker/couchdb/Dockerfile @@ -1,3 +1,15 @@ +######################################################################################################### +# CouchDB Build +# NOTE: the context for this build should be the root of the repository. +######################################################################################################### FROM couchdb:3.2 -COPY local.ini /opt/couchdb/etc/ +ARG S6_ARCH=amd64 +RUN curl https://github.com/just-containers/s6-overlay/releases/download/v1.21.8.0/s6-overlay-${S6_ARCH}.tar.gz -L -s --output /tmp/s6-overlay-${S6_ARCH}.tar.gz \ + && tar xzf /tmp/s6-overlay-${S6_ARCH}.tar.gz -C / \ + && rm -rf /tmp/s6-overlay-${S6_ARCH}.tar.gz + +COPY /docker/couchdb/local.ini /opt/couchdb/etc/ +COPY /docker/rootfs / + +ENTRYPOINT ["/init"] diff --git a/docker/rootfs/etc/cont-init.d/01-timezone b/docker/rootfs/etc/cont-init.d/01-timezone new file mode 100644 index 00000000..83fb30c2 --- /dev/null +++ b/docker/rootfs/etc/cont-init.d/01-timezone @@ -0,0 +1,7 @@ +#!/usr/bin/with-contenv bash + +if [ -n "${TZ}" ] +then + ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime + echo "${TZ}" > /etc/timezone +fi diff --git a/docker/rootfs/etc/cont-init.d/50-couchdb-init b/docker/rootfs/etc/cont-init.d/50-couchdb-init new file mode 100644 index 00000000..15bff19b --- /dev/null +++ b/docker/rootfs/etc/cont-init.d/50-couchdb-init @@ -0,0 +1,30 @@ +#!/usr/bin/with-contenv bash + +if [ -f "/opt/couchdb/data/.init_complete" ]; then + echo "Couchdb initialization has already completed, skipping" +else + + # start couchdb as a background process (store PID) + echo "Couchdb initialization: start couchdb in background mode" + /opt/couchdb/bin/couchdb & + COUCHDB_PID=$! + + # wait for couchdb to be ready + until $(curl --output /dev/null --silent --head --fail http://127.0.0.1:5984/_up); do echo "couchdb not ready" && sleep 5; done + + # create couch_peruser required system databases manually on startup + echo "couchdb ready, start creating system databases" + curl -X PUT http://127.0.0.1:5984/_users + curl -X PUT http://127.0.0.1:5984/_replicator + curl -X PUT http://127.0.0.1:5984/_global_changes + echo "system databases created successfully" + + + # gracefully stop couchdb process + echo "killing couchdb process" + kill -2 $COUCHDB_PID + + # create the init complete flag + echo "Couchdb initialization: complete" + touch /opt/couchdb/data/.init_complete +fi diff --git a/docker/rootfs/etc/services.d/couchdb/run b/docker/rootfs/etc/services.d/couchdb/run new file mode 100644 index 00000000..677da6c7 --- /dev/null +++ b/docker/rootfs/etc/services.d/couchdb/run @@ -0,0 +1,4 @@ +#!/usr/bin/with-contenv bash + +echo "starting couchdb" +/docker-entrypoint.sh /opt/couchdb/bin/couchdb diff --git a/docker/rootfs/etc/services.d/fasten/run b/docker/rootfs/etc/services.d/fasten/run new file mode 100644 index 00000000..e7874cb9 --- /dev/null +++ b/docker/rootfs/etc/services.d/fasten/run @@ -0,0 +1,7 @@ +#!/usr/bin/with-contenv bash + +# wait for couchdb to be ready +until $(curl --output /dev/null --silent --head --fail http://127.0.0.1:5984/_up); do echo "couchdb not ready" && sleep 5; done + +echo "starting fasten" +/opt/fasten/fasten start --config /opt/fasten/config/config.yaml