Docker-compose up卡在Attaching to,无法访问localhost:4200应用
Hey there, let's tackle this issue step by step! When docker-compose up gets stuck at the Attaching to stage and your Angular app isn't reachable on localhost:4200, there are several common fixes and checks we can run through:
1. First, confirm if your container is actually running
- Run
docker-compose psordocker psto check the status of yoursecure-cloud-storage-build-devcontainer. If it shows asExited, you need to dig into the logs to find why it failed to start:
Look for errors like missing dependencies, failed npm installs, or invalid startup commands in the output.docker-compose logs angular # Or directly check the container logs docker logs secure-cloud-storage-build-dev
2. Verify port mapping and internal service listening
- Check if port 4200 is occupied on your host machine:
- On Linux/macOS:
lsof -i :4200 - On Windows:
netstat -ano | findstr :4200
If another process is using the port, either change the host port in yourdocker-compose.yml(e.g.,"4201:4200") or shut down the conflicting process.
- On Linux/macOS:
- Check if the Angular service is listening inside the container:
Enter the container with:
Then run:docker exec -it secure-cloud-storage-build-dev bash
If you don't see any output here, the Angular dev server isn't binding to the right interface. By default,netstat -tulpn | grep 4200 # Or use ss if netstat isn't installed ss -tulpn | grep 4200ng serveonly listens onlocalhostinside the container—you need to make it listen on all interfaces with--host 0.0.0.0.
3. Fix your Dockerfile's startup command
- Open your
.docker/scs.dockerfileand check the finalCMDorENTRYPOINTline. If it looks like this:
Update it to:CMD ["ng", "serve"]
This ensures the Angular server accepts connections from outside the container (via your port mapping). Also, double-check that your Dockerfile runsCMD ["ng", "serve", "--host", "0.0.0.0"]npm installoryarn installcorrectly to pull all required dependencies before starting the server.
4. Tweak docker-compose.yml for interactive terminal support
- Angular's dev server sometimes requires an interactive terminal to run properly. Add these two lines to your
angularservice configuration:
This can prevent the process from hanging at the Attaching to stage.services: angular: # ... existing config stdin_open: true tty: true
5. Clean up old builds and restart fresh
- Cached images or containers can cause unexpected issues. Run these commands to start from scratch:
# Stop and remove existing containers docker-compose down # Delete the old built image docker rmi secure-cloud-storage # Rebuild and restart docker-compose up --build
内容的提问来源于stack exchange,提问作者PerlMonk92
相关产品推荐
相关产品推荐

