Angular项目通过dockerode管理Docker容器时查看容器报404错误求助
Hey there, let's walk through the most likely reasons you're hitting that 404 error when clicking your button to fetch Docker container details:
You're targeting the wrong server port
Angular runs onlocalhost:4200by default, but the Docker API doesn't live there. The Docker daemon uses a Unix socket (/var/run/docker.sockon Linux/macOS) by default, or if you've enabled TCP access, it typically uses port 2375 (unencrypted) or 2376 (encrypted). Your request is hitting the Angular dev server instead of the Docker API, hence the 404.Missing proxy configuration for CORS and routing
Even if you point to the correct Docker API port directly, you'll run into CORS issues. The fix here is setting up an Angular proxy to forward requests from your dev server to the Docker API. Create aproxy.conf.jsonfile in your project root:{ "/containers": { "target": "http://localhost:2375", "secure": false, "changeOrigin": true } }Then start your Angular app with
ng serve --proxy-config proxy.conf.json. This will route requests likehttp://localhost:4200/containers/...to the Docker API athttp://localhost:2375/containers/....The container no longer exists
Double-check if the container ID97bfae66b11fis valid. Rundocker ps -ain your terminal to list all containers (running or stopped). If this ID doesn't show up, the container was probably deleted, which would cause a 404 when trying to fetch its details.Docker API isn't accessible or configured properly
- On Linux/macOS: Make sure your user is in the
dockergroup (runsudo usermod -aG docker $USERand log out/in) to access the Unix socket. If using TCP, confirm the Docker daemon is configured to listen on the correct port. - On Windows (Docker Desktop): Go to Settings > Docker Engine and ensure you've enabled "Expose daemon on tcp://localhost:2375 without TLS" (note: this is unsafe for production, but fine for local development).
- On Linux/macOS: Make sure your user is in the
Typos in your request path
Double-check your Angular service code to make sure you're using the correct container ID and path. A missing slash, extra character, or misspelled container ID could easily lead to a 404.
内容的提问来源于stack exchange,提问作者user9300080

