Proxies for Docker Containers
Containerized applications that need to reach external services through proxies require proper configuration at the Docker level. Whether you are building scraping services, running geo-targeted tests, or deploying applications that need residential IP addresses, Docker provides multiple proxy configuration methods.
Runtime Proxy Configuration
Pass proxy settings as environment variables when running a container:
docker run -e HTTP_PROXY=http://YOUR_USER:YOUR_PASS@gate.hexproxies.com:8080 \
-e HTTPS_PROXY=http://YOUR_USER:YOUR_PASS@gate.hexproxies.com:8080 \
-e NO_PROXY=localhost,127.0.0.1 \
your-scraper-imageDocker Compose Configuration
services: scraper: build: . environment: - HTTP_PROXY=http://${PROXY_USER}:${PROXY_PASS}@gate.hexproxies.com:8080 - HTTPS_PROXY=http://${PROXY_USER}:${PROXY_PASS}@gate.hexproxies.com:8080 - NO_PROXY=localhost,127.0.0.1,redis,postgres depends_on: - redis
geo-tester: build: ./geo-tests environment: - PROXY_USER=${PROXY_USER} - PROXY_PASS=${PROXY_PASS} profiles: ['test']
redis: image: redis:7-alpine ports: - '6379:6379' ```
Dockerfile with Proxy-Aware Application
WORKDIR /app
# Install dependencies (no proxy needed for pip if using internal registry) COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Application reads proxy config from environment ENV PROXY_HOST=gate.hexproxies.com ENV PROXY_PORT=8080
CMD ["python", "scraper.py"] ```
Application Code Inside Container
import os
import httpx@dataclass(frozen=True) class ProxyConfig: host: str = os.environ.get("PROXY_HOST", "gate.hexproxies.com") port: int = int(os.environ.get("PROXY_PORT", "8080")) username: str = os.environ.get("PROXY_USER", "") password: str = os.environ.get("PROXY_PASS", "")
@property def url(self) -> str: if self.username: return f"http://{self.username}:{self.password}@{self.host}:{self.port}" return f"http://{self.host}:{self.port}"
config = ProxyConfig()
def fetch_through_proxy(url: str) -> dict: with httpx.Client(proxy=config.url, timeout=30) as client: resp = client.get(url) return {"status": resp.status_code, "body": resp.text[:1000]} ```
Multi-Container Scraping Architecture
services: scheduler: build: ./scheduler environment: - REDIS_URL=redis://redis:6379 depends_on: - redis
worker-us: build: ./worker environment: - HTTP_PROXY=http://${PROXY_USER}-country-us:${PROXY_PASS}@gate.hexproxies.com:8080 - REDIS_URL=redis://redis:6379 - WORKER_REGION=US deploy: replicas: 3 depends_on: - redis
worker-eu: build: ./worker environment: - HTTP_PROXY=http://${PROXY_USER}-country-de:${PROXY_PASS}@gate.hexproxies.com:8080 - REDIS_URL=redis://redis:6379 - WORKER_REGION=EU deploy: replicas: 2 depends_on: - redis
redis: image: redis:7-alpine ```
Docker Secrets for Proxy Credentials
For production deployments, use Docker secrets instead of plain environment variables:
secrets: proxy_user: external: true proxy_pass: external: true
services: scraper: build: . secrets: - proxy_user - proxy_pass environment: - PROXY_HOST=gate.hexproxies.com ```
Health Checks with Proxy
Add a health check that verifies proxy connectivity:
services:
scraper:
healthcheck:
test: ["CMD", "curl", "-x", "http://gate.hexproxies.com:8080", "-U", "user:pass", "https://httpbin.org/ip"]
interval: 60s
timeout: 10s
retries: 3Performance Considerations
Docker adds minimal network overhead — typically under 1ms for container-to-host networking. Combined with Hex Proxies ISP latency of sub-50ms, your containerized applications experience negligible proxy overhead. Scale horizontally by adding container replicas, each routing through different proxy sessions.