Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Pass backend url to docker image at start time (#1157)

1ilite4e696f2d2b1
diff --git a/Dockerfile b/Dockerfile
index 1f87fd39..df26ea7d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -10,6 +10,7 @@ RUN npm run build
# Stage 2: Setup the Nginx Server to serve the app
FROM docker.io/library/nginx:stable-alpine3.23 AS production
COPY --from=build /app/dist /usr/share/nginx/html
-RUN echo 'server { listen 80; server_name _; root /usr/share/nginx/html; location / { try_files $uri /index.html; } }' > /etc/nginx/conf.d/default.conf
+ENV VITE_BACKEND_URL="" VITE_GIST_BACKEND_URL=""
+RUN mkdir -p /etc/nginx/templates && echo 'server { listen 80; server_name _; root /usr/share/nginx/html; location = /config.js { default_type application/javascript; add_header Cache-Control "no-store"; return 200 "window.__DRAWDB_CONFIG__ = { backendUrl: \"${VITE_BACKEND_URL}\", gistBackendUrl: \"${VITE_GIST_BACKEND_URL}\" };"; } location / { try_files $uri /index.html; } }' > /etc/nginx/templates/default.conf.template
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
diff --git a/README.md b/README.md
index d96fc9c6..b150a687 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,12 @@ docker run -p 3000:80 drawdb
If you want to enable sharing, set up the [server](https://github.com/drawdb-io/drawdb-server) and environment variables according to `.env.sample`. This is optional unless you need to share files.
+With the Docker image, pass the server URL when starting the container. No rebuild is needed:
+
+```bash
+docker run -p 3000:80 -e VITE_BACKEND_URL=https://your-drawdb-server.example.com ghcr.io/drawdb-io/drawdb:latest
+```
+
## Contributing
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on how to contribute to this project.
diff --git a/index.html b/index.html
index dd19a675..baaa0ebf 100644
--- a/index.html
+++ b/index.html
@@ -55,6 +55,7 @@
<body theme-mode="light">
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
+ <script src="/config.js"></script>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
diff --git a/public/config.js b/public/config.js
new file mode 100644
index 00000000..22fbd291
--- /dev/null
+++ b/public/config.js
@@ -0,0 +1 @@
+window.__DRAWDB_CONFIG__ = {};
diff --git a/src/api/email.js b/src/api/email.js
index e0375c26..4a07a718 100644
--- a/src/api/email.js
+++ b/src/api/email.js
@@ -1,7 +1,8 @@
import axios from "axios";
+import { backendUrl } from "../config";
export async function send(subject, message, attachments) {
- return await axios.post(`${import.meta.env.VITE_BACKEND_URL}/email/send`, {
+ return await axios.post(`${backendUrl}/email/send`, {
subject,
message,
attachments,
diff --git a/src/api/gists.js b/src/api/gists.js
index 7f05bcf4..51e6064b 100644
--- a/src/api/gists.js
+++ b/src/api/gists.js
@@ -1,15 +1,11 @@
import axios from "axios";
+import { gistBackendUrl as baseUrl } from "../config";
export const SHARE_FILENAME = "share.json";
export const VERSION_FILENAME = "versionned.json";
const description = "drawDB diagram";
-const baseUrl =
- import.meta.env.VITE_GIST_BACKEND_URL ||
- import.meta.env.VITE_BACKEND_URL ||
- "http://localhost:5000";
-
export async function create(filename, content) {
const res = await axios.post(`${baseUrl}/gists`, {
public: false,
diff --git a/src/config/index.js b/src/config/index.js
new file mode 100644
index 00000000..89bf45c9
--- /dev/null
+++ b/src/config/index.js
@@ -0,0 +1,18 @@
+const runtime =
+ (typeof window !== "undefined" && window.__DRAWDB_CONFIG__) || {};
+
+const trimSlash = (url) => url.replace(/\/+$/, "");
+
+export const backendUrl = trimSlash(
+ runtime.backendUrl ||
+ import.meta.env.VITE_BACKEND_URL ||
+ "http://localhost:5000",
+);
+
+export const gistBackendUrl = trimSlash(
+ runtime.gistBackendUrl ||
+ runtime.backendUrl ||
+ import.meta.env.VITE_GIST_BACKEND_URL ||
+ import.meta.env.VITE_BACKEND_URL ||
+ "http://localhost:5000",
+);