Skip to content

Commit

Permalink
Create combined image (#16)
Browse files Browse the repository at this point in the history
* Create combined Dockerfile and docker-compose file

* compose - mount the surveys directory to filesystem instead of docker volume

* compose & dockerfile - Also expose port 8080

* start.sh - exit if any background process finishes/crashes instead of waiting for all

combined with restart: unless-stopped this will cause the whole container to restart if a background job exits
  • Loading branch information
brtwrst authored Aug 14, 2024
1 parent e989950 commit 348fcbf
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Build API
FROM golang:1.22-alpine AS api_builder

RUN apk add build-base

WORKDIR /api

COPY --from=api go.mod .
COPY --from=api go.sum .

RUN go mod download

COPY --from=api ./cmd ./cmd
COPY --from=api ./pkg ./pkg
COPY --from=api ./migrations ./migrations
COPY --from=api ./surveys ./surveys-examples
RUN CGO_ENABLED=1 GOOS=linux go build -o api -tags enablecgo cmd/console-api/api.go


# Build UI
FROM node:20-alpine AS ui_base

FROM ui_base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY --from=ui package.json package-lock.json ./
RUN npm ci

FROM ui_base AS ui_builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=ui . .

ENV NODE_ENV=production
ARG NEXT_PUBLIC_CONSOLE_API_ADDR
ENV NEXT_PUBLIC_CONSOLE_API_ADDR=$NEXT_PUBLIC_CONSOLE_API_ADDR

RUN npm run build


# Final image
FROM alpine:latest AS runner

RUN apk --no-cache add ca-certificates tzdata nodejs

WORKDIR /app
ENV NODE_ENV=production

COPY --from=ui_builder /app/public ./public

RUN mkdir .next
RUN chown 1000:1000 .next

COPY --from=ui_builder --chown=1000:1000 /app/.next/standalone ./
COPY --from=ui_builder --chown=1000:1000 /app/.next/static ./.next/static

WORKDIR /api

COPY --from=api_builder --chown=1000:1000 /api/api ./api
COPY --from=api_builder --chown=1000:1000 /api/migrations ./migrations
COPY --from=api_builder --chown=1000:1000 /api/surveys-examples ./surveys-examples

RUN mkdir /data
RUN chown 1000:1000 /data

COPY start.sh /start.sh
RUN chmod +x /start.sh

USER 1000:1000
RUN mkdir /data/surveys
RUN mkdir /data/db

EXPOSE 3000
EXPOSE 8080

CMD ["sh", "/start.sh"]
29 changes: 29 additions & 0 deletions compose-combined.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
formulosity:
restart: unless-stopped
image: formulosity:latest
build:
context: .
additional_contexts:
api: ./api
ui: ./ui
args:
- NEXT_PUBLIC_CONSOLE_API_ADDR=http://127.0.0.1:8080
ports:
- "3000:3000"
- "8080:8080"
init: true
environment:
- LOG_LEVEL=debug
- DATABASE_TYPE=sqlite # postgres|sqlite
- DATABASE_URL=/data/db/formulosity.db
- SURVEYS_DIR=/data/surveys
- CONSOLE_API_ADDR=http://127.0.0.1:8080
- IRON_SESSION_SECRET=e75af92dffba8065f2730472f45f2046941fe35f361739d31992f42d88d6bf6c
- HTTP_BASIC_AUTH=user:pass
volumes:
- formulosity_db:/data/db
- ./api/surveys:/data/surveys

volumes:
formulosity_db:
3 changes: 3 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/api/api &
node /app/server.js &
wait -n

0 comments on commit 348fcbf

Please sign in to comment.