Skip to content

Commit

Permalink
Better dev docker containers, Simpler production docker file, caddy f…
Browse files Browse the repository at this point in the history
…or ssl (#136)

* Better dev docker containers, Simpler production docker file, caddy for ssl

* Fix repeater docker image

* Add depends on

* Use recommended caddy volumes & change to using databag image

* move to using examples folder for installation

* lint

* Tested example linux on bare metal

* Add DATABAG_PORT env and fix caddy

* Add dev_database for local sqlite testing

* incorrect use -z

* Add platform goarch goos optional envs

* Add more caching for faster rebuilds
  • Loading branch information
lil5 authored Feb 5, 2025
1 parent 69755d1 commit 2b3052a
Show file tree
Hide file tree
Showing 39 changed files with 402 additions and 241 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev_database
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{go,sh}]
indent_style = tab

[{Makefile,Caddyfile}]
indent_style = tab

[LICENSE]
trim_trailing_whitespace = false
insert_final_newline = false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
dev_database/
45 changes: 45 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM node:22-alpine AS node
WORKDIR /app

# Download the node dependencies first before adding the rest for caching
COPY ./net/web/package.json ./net/web/yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn \
yarn --frozen-lockfile

COPY ./net/web/ ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn \
yarn run build

FROM golang:alpine AS go
EXPOSE 7000
WORKDIR /app/databag

RUN apk add --no-cache build-base imagemagick sqlite ffmpeg curl

RUN mkdir -p /opt/databag
RUN mkdir -p /var/lib/databag
RUN mkdir -p /app/databag/net

COPY ./net/server /app/databag/net/server
COPY ./net/transform /opt/databag/transform

WORKDIR /app/databag/net/server
RUN --mount=type=cache,target=/go/pkg/mod \
if [ -n "${DATABAG_GOARCH}" ]; then GOARCH=${DATABAG_GOARCH}; fi; \
if [ -n "${DATABAG_GOOS}" ]; then GOOS=${DATABAG_GOOS}; fi; \
go mod download

ARG DATABAG_GOARCH
ARG DATABAG_GOOS

RUN --mount=type=cache,target=/go/pkg/mod \
if [ -n "${DATABAG_GOARCH}" ]; then GOARCH=${DATABAG_GOARCH}; fi; \
if [ -n "${DATABAG_GOOS}" ]; then GOOS=${DATABAG_GOOS}; fi; \
CGO_ENABLED=1 go build -o databag .

COPY --from=node /app/build /app/databag/net/web/build

ENV DEV=0
ENV ADMIN=password

ENTRYPOINT /app/databag/net/server/entrypoint.sh
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
default:
@grep '^[^#[:space:].].*:' Makefile

dev-start:
docker compose -f docker-compose.dev.yml up -d
dev-stop:
docker compose -f docker-compose.dev.yml down

dev-restart-server:
docker compose -f docker-compose.dev.yml restart net-server
dev-restart-web:
docker compose -f docker-compose.dev.yml restart net-web
dev-restart-repeater:
docker compose -f docker-compose.dev.yml restart net-repeater

prod-docker-start:
docker compose up -d

prod-raw-build:
./build.sh
46 changes: 46 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: databag-dev

services:
net-web:
build:
context: ./net/web
dockerfile: Dockerfile.dev
working_dir: /app
volumes:
- ./net/web:/app
- cache_yarn:/root/.yarn
environment:
- YARN_CACHE_FOLDER=/root/.yarn
command: sh -c "yarn && chokidar '**/*.js' '**/*.ts' -c 'yarn run build' --debounce 18000 --initial --ignore node_modules --ignore build"
net-server:
build:
context: ./net/server
dockerfile: Dockerfile.dev
ports:
- 127.0.0.1:7000:7000
volumes:
- ./net/server:/app/databag/net/server
- ./dev_database:/var/lib/databag
- ./net/transform:/opt/databag/transform
- ./net/web/build:/app/databag/net/web/build
- cache_go:/go/pkg/mod
working_dir: /app
environment:
- ADMIN=password
- DEV=1
command: /app/databag/net/server/entrypoint.sh
net-repeater:
build:
context: ./net/repeater
dockerfile: Dockerfile.dev
working_dir: /app
volumes:
- ./net/repeater:/app
- cache_go:/go/pkg/mod
ports:
- 127.0.0.1:7878:7878
command: go run main.go

volumes:
cache_go:
cache_yarn:
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: databag

services:
app:
build: .

# # For building cross environment containers
# build:
# context: .
# args:
# DATABAG_GOARCH: arm64
# DATABAG_GOOS: linux
# platform: "linux/arm64"
ports:
- 127.0.0.1:7000:7000
volumes:
- database:/var/lib/databag
environment:
- ADMIN=password

volumes:
database:
14 changes: 14 additions & 0 deletions examples/docker-basic/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: databag-example-http

services:
app:
image: balzack/databag:latest
restart: unless-stopped
ports:
- 127.0.0.1:7000:7000
volumes:
- database:/var/lib/databag
environment:
- ADMIN=password
volumes:
database:
3 changes: 3 additions & 0 deletions examples/docker-ssl/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
example.com {
reverse_proxy http://app:443
}
29 changes: 29 additions & 0 deletions examples/docker-ssl/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: databag-example-ssl

services:
caddy:
image: caddy
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
# Edit the Caddyfile and replace "example.com" with your own domain
- ./Caddyfile:/etc/caddy/Caddyfile:ro
# Recommended by Caddy
- caddy_data:/data
- caddy_config:/config
depends_on:
- app
app:
image: balzack/databag:latest
restart: unless-stopped
volumes:
- database:/var/lib/databag
environment:
- ADMIN=password
- DATABAG_PORT=443
volumes:
database:
caddy_data:
caddy_config:
17 changes: 17 additions & 0 deletions examples/linux/databag.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=databag server
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=databag
ExecStart=/app/databag/net/server/entrypoint.sh

# [Service]
# Environment="ADMIN=password"

[Install]
WantedBy=multi-user.target
85 changes: 85 additions & 0 deletions examples/linux/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash
set -e

function confirm() {
read -p "Are you sure you want to continue? [Y/n] " reply
if [ "$reply" != "Y" ] && [ "$reply" != "y" ]; then
echo "Aborting"
exit 1
fi
}

if ! id "databag" >/dev/null 2>&1; then
echo "User databag not found, creating..."
confirm
sudo useradd databag
fi

if [[ ! -d "/app/databag" ]]; then
echo "Creating app directory for databag, this requires sudo permissions"
sudo rm -rf /app/databag || true
sudo mkdir -p /app/databag

echo "Downloading databag repository into /app/databag"
git clone --depth 1 https://github.com/balzack/databag.git /app/databag
sudo chown -R databag:databag /app/databag
fi
cd /app/databag

# You might be running this script from the root of this repository
if [[ "/app/databag" != $(pwd) ]]; then
cd ../..
fi

if [[ "/app/databag" != $(pwd) ]]; then
echo "Install databag must be done from /app/databag"
echo "Please re-clone the github repository into /app/databag, like so:"
echo "mkdir -p /app; https://github.com/balzack/databag /app/databag"
exit 1
fi

if [[ -z $(which yarn) ]]; then
echo "Yarn is not installed, installing..."
npm install --global yarn
fi

echo "Building frontend files..."
cd net/web
yarn --frozen-lockfile
echo "Removing old frontend files, requires sudo permissions..."
sudo rm -rf build
yarn run build
sudo chown -R databag:databag build
cd ../..

echo "Building backend files..."
cd net/server
CGO_ENABLED=1 go build -o databag .
cd ../..

echo "Creating databag locations..."
sudo mkdir -p /opt/databag
sudo mkdir -p /var/lib/databag
sudo chown -R databag:databag /var/lib/databag

echo "Copying transform scripts..."
sudo mkdir -p /opt/databag/transform
sudo cp net/transform/*.sh /opt/databag/transform/
sudo chmod +x /opt/databag/transform/*.sh

if [[ ! -f /etc/systemd/system/databag.service ]]; then
function createService() {
echo "Creating databag service, requires sudo permissions..."
sudo cp examples/linux/databag.service /etc/systemd/system/databag.service
sudo chmod 664 /etc/systemd/system/databag.service
sudo systemctl daemon-reload
}
function startService() {
echo "Starting databag service..."
sudo systemctl start databag.service
}
createService $? "Failed to install databag service"
startService $? "Failed to start databag service"
fi
echo ""
echo "Done"
29 changes: 29 additions & 0 deletions examples/linux/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

function confirm() {
read -p "Are you sure you want to continue? [Y/n] " reply
if [ "$reply" != "Y" ] && [ "$reply" != "y" ]; then
echo "Aborting"
exit 1
fi
}

echo "Stopping, disabling and removing databag service..."
confirm
sudo systemctl stop databag.service
sudo systemctl disable databag.service
sudo rm /etc/systemd/system/databag.service
sudo systemctl reload

echo "Removing databag data..."
confirm
sudo rm -rf /app/databag /opt/databag /var/lib/databag
if [ -z "$(ls -A /app)" ]; then
sudo rmdir /app
fi

echo "Removing databag user..."
confirm
sudo userdel databag

echo "Done"
Loading

0 comments on commit 2b3052a

Please sign in to comment.