You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Docker容器突然出现Python socket.gaierror:无法解析容器地址问题排查求助

Troubleshooting & Fixes for Docker Container DNS Resolution Failure in Local Python Tests

Let’s break down why your socket.gethostbyname("container_name") is throwing socket.gaierror locally (but working in GitLab CI) and how to diagnose and fix it:

First, Verify Basic DNS Resolution

Start with simple manual checks to isolate whether the issue is with Docker’s DNS or your Python code:

  • Run nslookup container_name or ping container_name in your local terminal. If this fails to return the container’s IP, the problem lies with Docker’s built-in DNS service, not your test script.
  • If the ping works but Python still throws an error, skip ahead to the Python-specific debugging steps.

Check Docker Network & DNS Configuration

Docker Compose creates a dedicated network for your services by default, which handles container name resolution. Here’s what to inspect:

  • Confirm the container is in the expected network: Run docker inspect container_name | grep Networks to see which network your container uses. Then run docker network inspect [network-name] and look for your container’s name under the Containers section—it should have a DNS entry mapped to its IP address.
  • Restart Docker: Network cache glitches are a common culprit. Restart Docker Desktop (or the Docker daemon on Linux) and retry your test.
  • Validate Docker daemon DNS settings: For Linux, edit /etc/docker/daemon.json and ensure it includes valid DNS servers (e.g., {"dns": ["8.8.8.8", "8.8.4.4"]}). On Docker Desktop, go to Settings > Docker Engine to verify the same. After making changes, restart the daemon.

Clear Local DNS Cache & Hosts File

Stale local DNS data or incorrect hosts entries can block container name resolution:

  • Flush DNS cache:
    • Mac: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
    • Windows: ipconfig /flushdns
    • Linux: sudo systemd-resolve --flush-caches
  • Inspect hosts file: Check /etc/hosts (Linux/Mac) or C:\Windows\System32\drivers\etc\hosts (Windows) for any stale or incorrect entries for container_name—delete them if present.

Debug Python’s DNS Resolution

Python might be using unexpected DNS settings that differ from your terminal’s configuration:

  • Add debug code to your test to see how Python is attempting to resolve the name:
    import socket
    try:
        print("Resolving container_name...")
        addr = socket.gethostbyname("container_name")
        print(f"Resolved to: {addr}")
    except socket.gaierror as e:
        print(f"Error details: {e}")
        # Check environment variables that might affect DNS
        import os
        print(f"System DNS (from env): {os.environ.get('DNS_SERVER', 'Not set')}")
    
  • Test with the container’s direct IP: Run docker inspect container_name | grep IPAddress to get the container’s IP, then replace container_name with this IP in your test. If this works, the issue is definitely related to name resolution.

Quick Workarounds If You Need Immediate Results

If you can’t pinpoint the root cause right away:

  • Assign a static IP to the container: Update your docker-compose.yml to set a fixed IP address for the container:
    version: "2.3"
    networks:
      default:
        ipam:
          config:
            - subnet: 172.20.0.0/16
    services:
      container_name:
        image: container_image:latest
        build:
          context: ./
        container_name: container_name
        ports:
          - "10021:10021"
          - "10022:10022"
          - "10023:10023"
        restart: unless-stopped
        networks:
          default:
            ipv4_address: 172.20.0.10
    
    Then use 172.20.0.10 directly in your test, or add 172.20.0.10 container_name to your local hosts file to restore name-based access.

内容的提问来源于stack exchange,提问作者Iustinian Olaru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:57:38