Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Refactor and simply dev docker environment. Closes #3102.

Kailash Nadh9ca898c286b9
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
index 26e4786a..89e5a3ab 100644
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -1,8 +1,7 @@
{
"name": "listmonk",
"dockerComposeFile": "../dev/docker-compose.yml",
- "service": "backend",
+ "service": "dev",
"workspaceFolder": "/app",
- "forwardPorts": [9000],
- "postStartCommand": "make dist && ./listmonk --install --idempotent --yes --config dev/config.toml"
+ "forwardPorts": [9000]
}
diff --git a/Makefile b/Makefile
index 0300f85b..3ab92ece 100644
--- a/Makefile
+++ b/Makefile
@@ -51,11 +51,16 @@ $(STUFFBIN):
$(BIN): $(SRC) go.mod go.sum schema.sql $(SQL) permissions.json
CGO_ENABLED=0 go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" ./cmd
-# Run the backend in dev mode. The SSR admin assets are loaded from disk from
-# static/admin/dist, so build them first.
+# Run the backend in dev mode. DEV_CONFIG is set by the dev container to use
+# its config and initialize a fresh DB.
+GO_RUN = CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" ./cmd
+
.PHONY: run
run: $(FRONTEND_DIST)
- CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" ./cmd
+ifneq ($(DEV_CONFIG),)
+ $(GO_RUN) --config="$(DEV_CONFIG)" --install --idempotent --yes
+endif
+ $(GO_RUN) $(if $(DEV_CONFIG),--config="$(DEV_CONFIG)")
# Install SSR admin frontend deps.
$(FRONTEND_DEPS_STAMP): $(FRONTEND)/package.json $(FRONTEND)/bun.lock
@@ -103,31 +108,23 @@ release-dry:
release:
goreleaser release --parallelism 1 --clean
-# Build local docker images for development.
+# Build the dev environmentt.
.PHONY: build-dev-docker
-build-dev-docker: build ## Build docker containers for the entire suite (Front/Core/PG).
- cd dev; \
- docker compose build ; \
-
-# Spin a local docker suite for local development.
-.PHONY: dev-docker
-dev-docker: build-dev-docker ## Build and spawns docker containers for the entire suite (Front/Core/PG).
- cd dev; \
- docker compose up
-
-# Run the backend in docker-dev mode. The SSR admin assets are loaded from disk from static/admin/dist.
-.PHONY: run-backend-docker
-run-backend-docker:
- CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" ./cmd --config=dev/config.toml
-
-# Tear down the complete local development docker suite.
+build-dev-docker:
+ docker compose -f dev/docker-compose.yml build
+
+# Start the dev services and open a shell.
+.PHONY: run-dev-docker
+run-dev-docker:
+ docker compose -f dev/docker-compose.yml up -d --wait
+ docker compose -f dev/docker-compose.yml exec dev bash
+
+# Stop the dev services (db is preserved).
+.PHONY: stop-dev-docker
+stop-dev-docker:
+ docker compose -f dev/docker-compose.yml down
+
+# Remove the the dev environment and storage.
.PHONY: rm-dev-docker
-rm-dev-docker: build ## Delete the docker containers including DB volumes.
- cd dev; \
- docker compose down -v ; \
-
-# Setup the db for local dev docker suite.
-.PHONY: init-dev-docker
-init-dev-docker: build-dev-docker ## Delete the docker containers including DB volumes.
- cd dev; \
- docker compose run --rm backend sh -c "make dist && ./listmonk --install --idempotent --yes --config dev/config.toml"
+rm-dev-docker:
+ docker compose -f dev/docker-compose.yml down -v
diff --git a/dev/README.md b/dev/README.md
index 4d41ba47..f27a89d3 100644
--- a/dev/README.md
+++ b/dev/README.md
@@ -1,62 +1,4 @@
-# Docker suite for development
+# Docker dev environment
-**NOTE**: This exists only for local development. If you're interested in using
-Docker for a production setup, visit the
-[docs](https://listmonk.app/docs/installation/#docker) instead.
+See https://listmonk.app/docs/developer-setup/
-### Objective
-
-The purpose of this Docker suite for local development is to isolate all the dev
-dependencies in a Docker environment. The containers have a host volume mounted
-inside for the entire app directory. This helps us to not do a full
-`docker build` for every single local change, only restarting the Docker
-environment is enough.
-
-## Setting up a dev suite
-
-To spin up a local suite of:
-
-- PostgreSQL
-- Mailhog
-- Node.js frontend app
-- Golang backend app
-
-### Verify your config file
-
-The config file provided at `dev/config.toml` will be used when running the
-containerized development stack. Make sure the values set within are suitable
-for the feature you're trying to develop.
-
-### Setup DB
-
-Running this will build the appropriate images and initialize the database.
-
-```bash
-make init-dev-docker
-```
-
-### Start frontend and backend apps
-
-Running this start your local development stack.
-
-```bash
-make dev-docker
-```
-
-Visit `http://localhost:8080` on your browser.
-
-### Tear down
-
-This will tear down all the data, including DB.
-
-```bash
-make rm-dev-docker
-```
-
-### See local changes in action
-
-- Backend: Anytime you do a change to the Go app, it needs to be compiled. Just
- run `make dev-docker` again and that should automatically handle it for you.
-- Frontend: Anytime you change the frontend code, you don't need to do anything.
- Since `yarn` is watching for all the changes and we have mounted the code
- inside the docker container, `yarn` server automatically restarts.
diff --git a/dev/app.Dockerfile b/dev/app.Dockerfile
index 3afcc8c4..0af6e5cb 100644
--- a/dev/app.Dockerfile
+++ b/dev/app.Dockerfile
@@ -1,11 +1,13 @@
-FROM golang:1.24.1 AS go
+FROM oven/bun:1.4.3 AS bun
-FROM node:16 AS node
+FROM golang:1.27.2-trixie
-COPY --from=go /usr/local/go /usr/local/go
-ENV GOPATH /go
+RUN apt-get update && apt-get install -y --no-install-recommends python3 \
+ && rm -rf /var/lib/apt/lists/* \
+ && git config --system --add safe.directory /app
+
+COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun
ENV CGO_ENABLED=0
-ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
WORKDIR /app
-CMD [ "sleep infinity" ]
+CMD ["sleep", "infinity"]
diff --git a/dev/docker-compose.yml b/dev/docker-compose.yml
index 0e6f70e3..4c21333b 100644
--- a/dev/docker-compose.yml
+++ b/dev/docker-compose.yml
@@ -1,55 +1,59 @@
-version: "3"
-
services:
adminer:
image: adminer:4.8.1-standalone
restart: always
ports:
- - 8070:8080
- networks:
- - listmonk-dev
+ - "127.0.0.1:8070:8080"
mailhog:
image: mailhog/mailhog:v1.0.1
ports:
- - "1025:1025" # SMTP
- - "8025:8025" # UI
- networks:
- - listmonk-dev
+ - "127.0.0.1:1025:1025" # SMTP
+ - "127.0.0.1:8025:8025" # UI
db:
- image: postgres:13
- ports:
- - "5432:5432"
- networks:
- - listmonk-dev
+ image: postgres:18
environment:
- POSTGRES_PASSWORD=listmonk-dev
- POSTGRES_USER=listmonk-dev
- POSTGRES_DB=listmonk-dev
restart: unless-stopped
+ healthcheck:
+ test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U $$POSTGRES_USER -d $$POSTGRES_DB"]
+ interval: 2s
+ timeout: 5s
+ retries: 30
volumes:
- type: volume
source: listmonk-dev-db
- target: /var/lib/postgresql/data
+ target: /var/lib/postgresql
- backend:
+ dev:
build:
context: ../
dockerfile: dev/app.Dockerfile
- command: ["make", "run-backend-docker"]
+ init: true
+ environment:
+ DEV_CONFIG: dev/config.toml
ports:
- - "9000:9000"
+ - "127.0.0.1:9000:9000"
depends_on:
- - db
+ db:
+ condition: service_healthy
volumes:
- ../:/app
- - ${GOPATH:-${HOME}/go}/pkg/mod/cache:/go/pkg/mod/cache
- networks:
- - listmonk-dev
+ - go-mod:/go/pkg/mod
+ - go-build:/root/.cache/go-build
+ - admin-dist:/app/static/admin/dist
+ - type: volume
+ source: node-modules
+ target: /app/static/admin/node_modules
+ volume:
+ nocopy: true
volumes:
listmonk-dev-db:
-
-networks:
- listmonk-dev:
+ go-mod:
+ go-build:
+ admin-dist:
+ node-modules:
diff --git a/docs/docs/content/developer-setup.md b/docs/docs/content/developer-setup.md
index 4e256dfb..989fb6a9 100644
--- a/docs/docs/content/developer-setup.md
+++ b/docs/docs/content/developer-setup.md
@@ -17,28 +17,15 @@ The app is a Go backend that server-renders the admin UI (HTML templates with JS
> [mailhog](https://github.com/mailhog/MailHog) is an excellent standalone mock SMTP server (with a UI) for testing and dev.
-### Running the dev environment
-You can run your dev environment locally or inside containers.
+### Running the Docker dev environment
-After setting up the dev environment, you can visit `http://localhost:9000`.
+- Run `make build-dev-docker` once to build the dev container which bundles Go, Postgres, Bun etc.
+- Run `make run-dev-docker` to start it and enter its shell.
+- Inside the shell, run `make run` which builds static assets, initializes a fresh DB, and starts listmonk on `:9000`.
+- For subsequent changes, simply Ctrl-C and re-run `make run` for changes to Go and static files to be picked up.
-1. Locally
-
- - Run `make run` to start the listmonk dev server on `:9000`. It builds the SSR admin assets (from `static/admin/`) and serves the admin at `/admin`. To rebuild admin assets on change while developing, run `cd static/admin && bun run watch` in a separate terminal.
-
-2. Inside containers (Using Makefile)
-
- - Run `make init-dev-docker` to setup container for db.
- - Run `make dev-docker` to setup docker container suite.
- - Run `make rm-dev-docker` to clean up docker container suite.
-
-3. Inside containers (Using devcontainer)
-
- - Open repo in vscode, open command palette, and select "Dev Containers: Rebuild and Reopen in Container".
-
-It will set up db, and start frontend/backend for you.
-
+Run `make stop-dev-docker` to stop the container and `make rm-dev-docker` to completely remove it.
# Production build
Run `make dist` to build the SSR admin frontend and the Go binary, embedding the static assets into a single self-contained binary, `listmonk`.
diff --git a/static/admin/build.mjs b/static/admin/build.mjs
index dbfb46e5..86ba624f 100644
--- a/static/admin/build.mjs
+++ b/static/admin/build.mjs
@@ -16,9 +16,11 @@ const watch = process.argv.includes('--watch');
const COMPRESS_EXT = ['.js', '.css', '.svg'];
async function build() {
- // Fresh /dist dir.
- await rm(dist, { recursive: true, force: true });
+ // Clear the contents, preserving dist itself so it can be a Docker volume.
await mkdir(dist, { recursive: true });
+ for (const entry of await readdir(dist)) {
+ await rm(path.join(dist, entry), { recursive: true, force: true });
+ }
// Verbatim static assets (icons, images) and stylesheets.
await cp(path.join(root, 'assets/static'), dist, { recursive: true });