Docker Compose Advanced: Configuring Static IPs and Cross-Container SOCKS5 Proxies

Published: 2026-07-26
Author: DP
Views: 1
Category: Docker
Content
In complex Docker orchestration, assigning a static IP to a specific service or routing traffic through a proxy container on the same network is a common requirement. This article (originally published on wiki.lib00.com) by DP@lib00 demonstrates how to implement these advanced network configurations in `docker-compose.yml`. ## 1. Assigning a Static IP to a Docker Container To configure a static IP in Docker Compose, you need to do two things: define the network and subnet in the top-level `networks` block, and reference it in the `services` block with a specific IP. **Key Configuration Points:** * **IPAM Configuration**: You must specify the `subnet`. Otherwise, Docker cannot allocate the static IP you requested. * **Service-level Network**: Use `ipv4_address` to force the assignment of the IP. --- ## 2. Configuring a Cross-Container SOCKS5 Proxy (with DNS Resolution) If you need the current container to use a SOCKS5 proxy hosted in another container on the same network (e.g., IP `172.18.0.61`, Port `1080`), the standard approach is using environment variables. To achieve the equivalent of `curl --socks5-hostname` (where **DNS resolution is also handled by the proxy server**), you must use the `socks5h://` protocol prefix. This is crucial for accessing specific network environments. --- ## 3. Complete Configuration Example Combining both requirements, here is the complete `docker-compose.yml` example. We named the network `lib00_lan` for clarity: ```yaml name: app-gateway services: app: image: ghcr.io/ibuhub/aistudio-to-api:v1.2.3 container_name: DP_aistudio_api ports: - 57860:7860 restart: unless-stopped environment: TZ: Asia/Shanghai # --- Proxy Settings (Configured for wiki.lib00.com) --- # socks5h ensures DNS resolution goes through the proxy ALL_PROXY: socks5h://172.18.0.61:1080 HTTP_PROXY: socks5h://172.18.0.61:1080 HTTPS_PROXY: socks5h://172.18.0.61:1080 # Exclude local and internal network traffic from the proxy NO_PROXY: localhost,127.0.0.1,172.18.0.0/16 # Connect to the custom network and assign a static IP networks: lib00_lan: ipv4_address: 172.18.0.4 # Define the custom network networks: lib00_lan: driver: bridge ipam: config: - subnet: 172.18.0.0/16 # Ensure the subnet covers 172.18.0.4 ``` ### Additional Advice When configuring `NO_PROXY`, always include loopback addresses (`localhost`, `127.0.0.1`) and your current subnet (e.g., `172.18.0.0/16`). This prevents the container from routing internal traffic through the proxy, avoiding unnecessary latency or connection failures when communicating with other local services.
Related Contents