Docker Compose Advanced: Configuring Static IPs and Cross-Container SOCKS5 Proxies
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
Complete Guide to Installing and Configuring Git Server on Synology NAS: Basic to Advanced
Duration: 00:00 | DP | 2026-07-16 20:39:02Why Do .smbdelete Hidden Files Appear After Deleting on Mac SMB Shares? Causes & Ultimate Solutions
Duration: 00:00 | DP | 2026-06-27 19:10:00Complete Guide to Setting Docker Container Timezone to UTC+8 (Asia/Shanghai)
Duration: 00:00 | DP | 2026-06-30 20:43:30Fixing 'Unable to locate package openjdk-17-jdk' in PHP 8 Docker (Debian Trixie)
Duration: 00:00 | DP | 2026-07-25 09:23:18Nginx Reverse Proxy Guide: Elegantly Routing Specific Subdirectories to Docker Containers
Duration: 00:00 | DP | 2026-07-26 21:31:06The Ultimate Guide to Docker Cron Logging: Host vs. Container Redirection - Are You Doing It Right?
Duration: 00:00 | DP | 2026-01-05 08:03:52The Ultimate 'Connection Refused' Guide: A PHP PDO & Docker Debugging Saga of a Forgotten Port
Duration: 00:00 | DP | 2025-12-03 09:03:20Solving the MySQL Docker "Permission Denied" Error on Synology NAS: A Step-by-Step Guide
Duration: 00:00 | DP | 2025-12-03 21:19:10How Can a Docker Container Access the Mac Host? The Ultimate Guide to Connecting to Nginx
Duration: 00:00 | DP | 2025-12-08 23:57:30Docker Exec Mastery: The Right Way to Run Commands in Containers
Duration: 00:00 | DP | 2026-01-08 08:07:44How to Fix the "tsx: not found" Error During Vue Vite Builds in Docker
Duration: 00:00 | DP | 2026-01-10 08:10:19The Ultimate Guide to Docker Cron Jobs: Effortlessly Scheduling PHP Tasks in Containers from the Host
Duration: 00:00 | DP | 2025-12-29 10:30:50From Phantom Conflicts to Docker Permissions: A Deep Dive into Debugging an Infinite Loop in a Git Hook for an AI Assistant
Duration: 00:00 | DP | 2025-11-09 16:39:00How to Add Port Mappings to a Running Docker Container: 3 Proven Methods
Duration: 00:00 | DP | 2026-02-05 10:16:12PHP Stuck on Loading After Enabling Xdebug? Don't Panic, It Might Be Working Perfectly!
Duration: 00:00 | DP | 2025-11-15 07:03:00Connecting LobeChat with MinIO: A Simple Guide to Fixing S3 Path-Style Configuration
Duration: 00:00 | DP | 2026-01-28 08:33:32How to Automatically Run Git Clone on Docker Start? 3 Practical Methods Explained
Duration: 00:00 | DP | 2026-02-15 13:47:17How to Easily Fix the "error: externally-managed-environment" in Python
Duration: 00:00 | DP | 2026-01-29 08:34:50Recommended
The Ultimate Guide to CSS Colors: From RGBA to HSL for Beginners
00:00 | 107Confused by CSS color values like `rgba(8, 219, 21...
How to Fix Chrome Cannot Access Internal IPs (ERR_ADDRESS_UNREACHABLE) When Safari Works
00:00 | 6Experiencing an issue where Chrome cannot access l...
How to Fix Mac mini Not Receiving SMS Messages and iCloud Sync Stuck
00:00 | 14Having trouble receiving SMS on your Mac mini whil...
Why Your z-index Fails: The Definitive Guide to Fixing Dropdown Clipping with the Portal Pattern
00:00 | 213Have you ever faced the frustrating issue of a wel...