v1.8.91-d84675c
← Back to Hex Proxies

Proxies for Docker Containers

Last updated: April 2026

By Hex Proxies Engineering Team

Learn how to configure proxy routing in Docker containers for web scraping, API integration, geo-testing, and distributed application deployment.

intermediate18 minutesdeveloper-qa

Prerequisites

  • Docker and Docker Compose
  • Basic containerization knowledge
  • Hex Proxies account

Steps

1

Choose configuration method

Decide between runtime env vars, Docker Compose env, or Docker secrets based on your security requirements.

2

Configure Docker Compose

Set up proxy environment variables in your docker-compose.yml with NO_PROXY for internal services.

3

Update application code

Modify your application to read proxy configuration from environment variables at startup.

4

Set up multi-region workers

Deploy container replicas with country-targeted proxy configurations for geo-distributed operations.

5

Add health checks

Configure container health checks that verify proxy connectivity and alert on failures.

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-image

Docker 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: 3

Performance 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.

Tips

  • *Always set NO_PROXY for internal container communication (Redis, Postgres, other services).
  • *Use Docker secrets or external secret managers for proxy credentials in production.
  • *Deploy geo-specific worker containers with country-targeted proxy URLs.
  • *Add proxy connectivity health checks to catch credential expiry or network issues early.
  • *Scale horizontally with container replicas — each gets a fresh proxy session automatically.

Ready to Get Started?

Put this guide into practice with Hex Proxies.

Cookie Preferences

We use cookies to ensure the best experience. You can customize your preferences below. Learn more