ProxyForge Download

Running Python Scrapers in Docker with Proxy Support

Running Python Scrapers in Docker with Proxy Support

Developers often need to run web scrapers and automation scripts at scale. Docker provides an excellent environment for managing these tasks, offering isolation, consistency, and scalability. However, one of the common challenges is setting up proxy support within a Docker container. This guide will walk you through various methods to configure proxies in Docker, focusing on Python-based scrapers using libraries like requests, httpx, Playwright, and Chrome.

Introduction

Running web scraping tasks with proxies is crucial for bypassing IP restrictions, avoiding detection by target websites, and managing request limits. Docker can encapsulate these workflows into containers, but setting up proxy support requires careful configuration to ensure that all components within the container communicate through the desired proxy servers.

In this guide, we'll explore three main patterns for setting up proxies in a Docker environment:

We'll also discuss specific configurations for Playwright and Chrome headless browsers, as well as handling proxy authentication.

Environment Variables for Proxies

The simplest way to set up proxies in Docker is through environment variables. Most Python libraries like requests and httpx automatically pick these up from the environment, making it straightforward to configure:

docker run -e HTTP_PROXY=http://proxy.example.com:8080 \
           -e HTTPS_PROXY=http://proxy.example.com:8080 \
           -e NO_PROXY=localhost,127.0.0.1,example.com \
           my-python-scraper-image

Example with Docker Compose

For more complex setups involving multiple services, using docker-compose.yml is beneficial:

version: '3'
services:
  scraper:
    image: my-python-scraper-image
    environment:
      - HTTP_PROXY=http://proxy.example.com:8080
      - HTTPS_PROXY=http://proxy.example.com:8080
      - NO_PROXY=localhost,127.0.0.1,example.com

Limitations

While this method is convenient, it doesn't work for browser-based scraping tools like Playwright and Chrome headless. These require explicit configuration of the proxy server via command-line flags:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(proxy={
        "server": "http://proxy.example.com:8080",
        "username": "user",
        "password": "pass"
    })
    page = browser.new_page()
    page.goto('https://example.com')
    print(page.title())
    browser.close()

Build-Time Proxy Configuration

For tasks that require proxy settings during the build process, such as installing packages via pip or apt, you can use Docker's build arguments (--build-arg). This is particularly useful for multi-stage builds where you want to keep sensitive information out of the final image.

Example Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.9-slim AS builder

# Set environment variables for proxy settings
ARG HTTP_PROXY=http://proxy.example.com:8080
ARG HTTPS_PROXY=http://proxy.example.com:8080
ENV HTTP_PROXY=${HTTP_PROXY}
ENV HTTPS_PROXY=${HTTPS_PROXY}

# Install dependencies
RUN pip install requests playwright

# Copy the rest of your application code here
COPY . /app

FROM python:3.9-slim AS runtime
WORKDIR /app

# Copy only the dependencies installation from the 1st stage image.
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY --from=builder /app .

CMD ["python", "scraper.py"]

Building with Proxy

docker build --build-arg HTTP_PROXY=http://proxy.example.com:8080 \
             --build-arg HTTPS_PROXY=http://proxy.example.com:8080 \
             -t my-python-scraper-image .

Docker Daemon Proxy Settings

You can configure the Docker daemon itself to use proxies for pulling images. This is done by editing the /etc/docker/daemon.json file:

{
  "proxies": {
    "default": {
      "httpProxy": "http://proxy.example.com:8080",
      "httpsProxy": "http://proxy.example.com:8080",
      "noProxy": "localhost,127.0.0.1,example.com"
    }
  }
}

After modifying daemon.json, restart the Docker daemon:

sudo systemctl restart docker

Note: These settings apply only to image pulls and not to container traffic.

Proxy Authentication

When using proxies that require authentication, you can include credentials directly in the URL for libraries like requests:

import requests

response = requests.get('https://example.com', proxies={
    'http': 'http://user:[email protected]:8080',
    'https': 'http://user:[email protected]:8080'
})
print(response.text)

For Playwright, you can set extra HTTP headers or use the proxy configuration in the launch arguments:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(proxy={
        "server": "http://proxy.example.com:8080",
        "username": "user",
        "password": "pass"
    })
    page = browser.new_page()
    page.goto('https://example.com')
    print(page.title())
    browser.close()

Base Images for Scrapers

When choosing a base image, consider pre-installed browsers that support headless mode. Two popular choices are:

Multi-Stage Builds for Proxy Rotation

To avoid baking sensitive proxy credentials into your final image, use a multi-stage build approach. Configure proxies only during the build stage:

# Stage 1: Build environment with proxy settings
FROM python:3.9-slim AS builder

ARG HTTP_PROXY=http://proxy.example.com:8080
ARG HTTPS_PROXY=http://proxy.example.com:8080
ENV HTTP_PROXY=${HTTP_PROXY}
ENV HTTPS_PROXY=${HTTPS_PROXY}

RUN pip install requests playwright

COPY . /app

# Stage 2: Runtime environment without proxy settings
FROM python:3.9-slim AS runtime
WORKDIR /app

COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY --from=builder /app .

CMD ["python", "scraper.py"]

Conclusion

Setting up proxy support in Docker for Python scrapers involves careful configuration of environment variables, build arguments, and daemon settings. By understanding these patterns, you can effectively manage network traffic and ensure that your scrapers operate smoothly with the desired proxy settings.

For those who need a more robust solution to manage proxies without manually handling proxy lists, consider ProxyForge. It auto-tests and rotates proxies locally on your machine, offering a one-off unlock at $29. Download it here: https://proxyforge.dev/#download.

If you require professional or managed proxy services for production use, Bright Data and SmartProxy are established providers in this space, offering reliable rotating proxies and residential IP addresses.

This guide should provide a solid foundation for setting up proxies in Docker for your Python scraping projects.

Free proxies break. ProxyForge doesn't.

The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.

Download ProxyForge free →