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:06DevOps Practice: How to Safely Clear Logs of a Running Docker Container?
Duration: 00:00 | DP | 2026-07-27 09:33:42Practical Guide: Translating Complex Docker Compose to Docker Run Commands
Duration: 00:00 | DP | 2026-07-29 09:44:07The Ultimate Guide to Docker Cron Logging: Host vs. Container Redirection - Are You Doing It Right?
Duration: 00:00 | DP | 2026-01-05 08:03:52Cron Job Failing? The Ultimate Guide to Fixing 'docker: command not found'
Duration: 00:00 | DP | 2026-08-01 09:59:44The 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:00Recommended
PHP 8 Upgrade Guide: Fixing Nullable Type Deprecation and Optimizing Composer Autoloading
00:00 | 87This article addresses two common challenges faced...
Bypassing MySQL's `ON UPDATE CURRENT_TIMESTAMP`: A Guide to 'Silent Updates'
00:00 | 28When using MySQL, `ON UPDATE CURRENT_TIMESTAMP` is...
Boost Your WebStorm Productivity: Mimic Sublime Text's Cmd+D Multi-Selection Shortcut
00:00 | 168Developers switching from Sublime Text to WebStorm...
Designing an Efficient Hash Identification Tool: A UI/UX Deep Dive from Wireframe to Best Practices
00:00 | 42This article provides a deep dive into the UI/UX d...