Docker中Directus 7仪表盘API URL无法修改该如何解决?
I've run into this exact issue with Directus 7's Docker setup before—those environment variables don't work for the app container because it's serving static files, not a dynamic app that reads env vars at runtime. Here are the reliable ways to fix it:
1. Use a Reverse Proxy (Recommended)
The cleanest approach is to put both the API and app behind a reverse proxy (like Nginx) so they share the same base domain. This way the app automatically uses the same domain for API requests without any config changes.
For example, in your docker-compose, add an Nginx service:
services: nginx: image: nginx:alpine ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf depends_on: - api - app api: image: directus/api:latest # ... your existing API config (env vars, volumes, etc.) app: image: directus/app:latest # ... your existing app config
Then create an nginx.conf file that proxies /api to the API container and serves the app from the root:
events {} http { server { listen 80; server_name your-domain.com; # Serve Directus App location / { proxy_pass http://app:80; proxy_set_header Host $host; } # Proxy API requests location /api/ { proxy_pass http://api:7000/; proxy_set_header Host $host; } } }
Now when you access http://your-domain.com, the app will send API requests to http://your-domain.com/api/ which gets routed to your API container.
2. Modify the App's Static Config File
Directus 7's app stores its API URL in a config.js file inside the container at /var/www/html/config.js. You can either mount a modified version of this file or edit it when the container starts.
Option A: Mount a Custom Config File
Create a local config.js with your API URL:
window.DIRECTUS_CONFIG = { apiUrl: "http://your-api-domain:7000/" };
Then update your docker-compose app service to mount this file:
app: image: directus/app:latest volumes: - ./config.js:/var/www/html/config.js # ... other config (ports, depends_on, etc.)
Option B: Edit the File on Container Startup
Add a command to the app service that replaces the default API URL using sed when the container starts:
app: image: directus/app:latest command: sh -c "sed -i 's|http://localhost:7000/|http://your-api-domain:7000/|g' /var/www/html/config.js && nginx -g 'daemon off;'" # ... other config
This replaces the default localhost:7000 URL with your actual API address before starting the web server.
3. Verify the Fix
After applying any of these methods, clear your browser cache (Directus app caches config sometimes) and reload the dashboard. You should see the app sending requests to your configured API URL instead of localhost.
内容的提问来源于stack exchange,提问作者pdsm

