如何解决Visual Studio与Docker容器环境下API连接拒绝问题?
Hey there, let's work through this problem step by step. It's frustrating when your code works for everyone else but you—especially when the only obvious difference is that proxy setting. Since you can already curl the auth API from the container but your app/Swagger can't, the network path is open, so the issue is likely in how your app or development environment is handling the connection. Here are the most likely fixes to try:
1. Explicitly Clear Proxy Variables in Your Docker Container
Even if you cleared http_proxy on your host, Docker might be passing old proxy environment variables to your container when it starts. Your .NET Core app could still be picking up these variables and trying to route auth requests through a proxy that doesn't exist anymore.
Add these environment variables to your openhostservice in docker-compose.yml to force-clear proxies and whitelist your local auth API:
openhostservice: build: context: . dockerfile: OpenHostService/Dockerfile image: ${DOCKER_REGISTRY-}openhostservice depends_on: - db2server ports: - 32780:80 environment: - http_proxy= - https_proxy= - no_proxy=localhost,127.0.0.1,<YOUR_AUTH_API_MACHINE_IP/HOSTNAME>
Replace <YOUR_AUTH_API_MACHINE_IP/HOSTNAME> with the actual IP or hostname of the machine running the Authentication API. This ensures your app doesn't try to use a proxy for internal network requests.
2. Debug Network Access Directly from the Container
Let's confirm your app has the same network access as curl does. Jump into your running app container and test the auth API from inside, while checking your app's logs:
- Get your container ID with
docker ps - Enter the container:
docker exec -it <YOUR_OPENHOSTSERVICE_CONTAINER_ID> /bin/bash - First, re-test the curl command to confirm it still works:
curl <YOUR_AUTH_API_URL> - Then, start your app manually inside the container (if possible) and try hitting the auth endpoint via Swagger or a direct HTTP request. Check the app's console output for specific errors—look for mentions of proxy servers, DNS failures, or connection timeouts that curl doesn't hit.
3. Disable Visual Studio's Automatic Proxy Configuration
Visual Studio has a habit of auto-configuring proxies for Docker containers during debugging, even if you've cleared them on your host. Here's how to check:
- Go to Tools > Options > Container Tools > Docker Compose
- Look for an option like "Enable proxy support" and uncheck it
- Restart Visual Studio to make sure the new settings take effect
Also, double-check that Visual Studio isn't picking up old proxy environment variables. Close all instances, open a fresh command prompt (where you've cleared http_proxy), and launch Visual Studio from that prompt to ensure it inherits the clean environment.
4. Verify Your Docker Network Configuration
You're using an external network in your docker-compose.yml—make sure this network is properly connected to your VPN. Try switching to Docker's default bridge network temporarily to rule out external network issues:
- Comment out the
networkssection indocker-compose.yml - Run
docker-compose down && docker-compose up --buildto recreate the containers on the default network - Test the auth API again
If this fixes the issue, your external network's routing rules might be conflicting with your VPN connection. You may need to reconfigure the external network to allow traffic to your local auth API machine.
5. Check DNS Resolution in the Container
If your app uses a hostname (not an IP) to reach the Authentication API, the container might be having trouble resolving that hostname via DNS. Test this by pinging the hostname from inside the container:
ping <AUTH_API_MACHINE_HOSTNAME>
If the ping fails, add your company's VPN DNS server to your container's DNS settings in docker-compose.yml:
openhostservice: # ... other config ... dns: - <YOUR_COMPANY_VPN_DNS_SERVER_IP> - 8.8.8.8
This ensures the container uses the correct DNS server to resolve local network hostnames.
6. Rule Out Windows Firewall or VPN Routing Issues
Even though you can ping the auth API machine, Windows Firewall might be blocking traffic on the specific port used by the Authentication API. Try temporarily disabling Windows Firewall to test if that's the issue. If it works, add an inbound rule to allow traffic from your Docker network's subnet to the auth API port.
You can also check your host's routing table to ensure traffic to the auth API machine is going through the VPN:
route print
Look for the entry for your auth API machine's IP range—it should show your VPN adapter as the gateway.
内容的提问来源于stack exchange,提问作者user7195420

