Guide

How to Use Mobile Proxies in Docker and CI/CD Pipelines

How to route only your application's traffic through a mobile proxy in Docker, Docker Compose, GitHub Actions and GitLab CI, keep proxy credentials out of images and logs, and avoid the common pitfalls with HTTP_PROXY variables. Tested with Docker Engine 29.

Narmin Kamilsoy
Narmin Kamilsoy 作者
阅读时间 19 分钟
How to Use Mobile Proxies in Docker and CI/CD Pipelines

Scrapers, monitoring scripts and browser tests often run in containers or in a CI pipeline. When they need a mobile proxy, two questions come up quickly: where should the proxy be configured, and how do you keep the proxy credentials out of images, logs and repositories?

This guide answers both. It explains which Docker settings affect which traffic, why a dedicated variable is safer than the standard proxy variables, how to pass credentials at build time and at run time, and how to use them in GitHub Actions and GitLab CI. We ran the Docker and environment variable examples in a local test setup, and the results are summarised at the end.

1
Scope the proxy
Only your app's requests use the mobile proxy.
→
2
Keep it out of the image
No proxy URL in ENV, ARG or layers.
→
3
Pass it at run time
Use secrets in Compose and CI.
→
4
Check before the job
Fail early if the proxy is empty or down.

Decide Which Traffic Needs the Mobile Proxy

In most setups, only the requests your application sends to its target sites need a mobile IP. Pulling images, installing packages and talking to internal services do not. Sending them through a mobile proxy uses mobile data, adds latency, and spreads your proxy credentials to places that do not need them. Docker has three main places where a proxy can be set, and they affect different traffic:

Where you set it What it affects Use it for the mobile proxy?
Docker daemon (daemon.json, "proxies"; Docker Engine only) How the daemon reaches Docker Hub and other registries, for example when pulling images Usually not. Image pulls do not need a mobile IP
Docker client (~/.docker/config.json, "proxies") Sets the proxy environment variables in every container you start with that Docker client and pre-fills the proxy build arguments Usually not. Every tool that reads those variables, in those containers and builds, then uses the proxy
Your application Only the requests your code sends with an explicit proxy setting Yes. This is the one place you need it

The Docker documentation describes the daemon setting as the proxy the daemon uses "to access images stored on Docker Hub and other registries." For the client configuration, it says that when you start a container, "its proxy-related environment variables are set to reflect your proxy configuration," and that proxy build arguments are filled in automatically for builds. The daemon.json setting applies to Docker Engine. Docker Desktop ignores it, so on Docker Desktop, configure the proxy in Docker Desktop settings. Both are useful for a company proxy that all traffic must pass through. For a mobile proxy, configure it in the application instead.

Use a Dedicated Variable, Not HTTP_PROXY

The standard proxy variables, such as HTTP_PROXY, HTTPS_PROXY and NO_PROXY in their uppercase and lowercase forms, are read by many tools and libraries, including curl and Python's requests, though not always in the same way. If you set them in a container, every tool in it that honours them sends its traffic through the mobile proxy. The differences between tools matter too:

curl only accepts http_proxy in lowercase. Its documentation explains that the uppercase HTTP_PROXY is ignored because of the CGI protocol, where a request header named Proxy would show up as HTTP_PROXY, and that accepting the uppercase form "has been the source for many security problems." In our test with an http:// target, curl ignored HTTP_PROXY and connected directly. For https:// targets, curl reads https_proxy or HTTPS_PROXY, and ALL_PROXY applies when no variable for the protocol is set.
requests reads these variables too, and its documentation warns that they override session.proxies. In our test, a wrong password in HTTP_PROXY won over the correct one in session.proxies and produced a 407. Passing the proxy with proxies= on each request worked.

A dedicated variable avoids both problems. Store the proxy URL under a name of your own, such as PROXY_URL, and pass it explicitly in your code. Only the requests that should use the mobile proxy will use it. The snippet below also reads the value from a secret file if one is mounted, which the following sections use:

app.py
import os
from pathlib import Path

import requests


def proxy_url():
    secret = Path("/run/secrets/proxy_url")
    if secret.exists():
        return secret.read_text().strip()
    return os.environ.get("PROXY_URL", "").strip()


PROXY = proxy_url()
if not PROXY:
    raise SystemExit("Proxy URL is empty. Check the mounted secret or PROXY_URL.")
PROXIES = {"http": PROXY, "https": PROXY}

r = requests.get("https://api.ipify.org", proxies=PROXIES, timeout=(10, 30))
r.raise_for_status()
print("Exit IP:", r.text)

Keep the empty check. In GitHub Actions, a secret that is not set comes through as an empty string, and requests treats an empty proxy value as no proxy at all. Without the check, the job connects directly and prints the runner's own IP as if it were the proxy's. In our test, the version without the check did exactly that and still exited successfully. An empty secret file has the same effect.

If your proxy password contains characters such as @, :, / or %, percent-encode it in the URL.

If you do use the standard variables

Sometimes a tool only reads HTTP_PROXY and HTTPS_PROXY. In that case, set both the lowercase and uppercase forms, and list everything that should not go through the proxy in NO_PROXY:

Environment
NO_PROXY=localhost,127.0.0.1,.internal.example,10.0.0.0/8

In curl, a name that starts with a dot matches the whole domain, and since curl 7.86.0 the list can also contain IP ranges in CIDR notation. Other tools may handle these forms differently, so test the tools you use. In our test, requests sent a request directly when its host was listed in NO_PROXY.

This only applies when requests picks the proxy from the environment. A proxy passed explicitly with proxies=, as in the snippet above, is not affected: in our test, the request still went through it with its host listed in NO_PROXY. If you pass the proxy explicitly, decide in your code which requests use it.

Keep Credentials Out of the Image

Never write the proxy URL into a Dockerfile with ENV or pass it in a build argument of your own, such as --build-arg PROXY_URL. The Docker documentation says that "Build arguments and environment variables are inappropriate for passing secrets" because "they persist in the final image," and that build arguments "may persist in the image metadata, as provenance attestations and in the image history." Our tests confirmed this: a URL set with ENV was visible with docker inspect, and a PROXY_URL build argument used in a RUN step appeared in docker history, password included.

The predefined proxy arguments, such as HTTP_PROXY and HTTPS_PROXY, are handled differently. Docker says they are "automatically excluded from the build cache and the output of docker history by default," and that once a Dockerfile references them, "the proxy configuration ends up in the build cache." In our test, an HTTP_PROXY build argument was not in docker history while the Dockerfile left it undeclared, and appeared there once the Dockerfile declared it with ARG. Even undeclared, it sends every tool in the build that reads it through the mobile proxy, so a build secret is still the better choice.

In most cases the proxy is not needed during the build at all. Build the image without it, and pass the proxy when the container runs:

Dockerfile
FROM python:3.13-slim
RUN pip install requests
COPY app.py /app/app.py
CMD ["python", "/app/app.py"]

If a build step really needs the proxy

Use a build secret. A secret mount makes the value available to one RUN instruction, as a file or, with env=, as an environment variable. Here the extra RUN step runs app.py once during the build, as a simple way to show the mechanism. In a real project, that step would be whatever needs the proxy, such as a script that downloads data:

Dockerfile
FROM python:3.13-slim
RUN pip install requests
COPY app.py /app/app.py
RUN --mount=type=secret,id=proxy_url,env=PROXY_URL python /app/app.py
CMD ["python", "/app/app.py"]

Pass the value from an environment variable on the machine that runs the build:

Terminal
docker build --secret id=proxy_url,env=PROXY_URL -t scraper .

The env= option was added in Dockerfile syntax 1.10.0. In our test with Docker Engine 29.10 it worked without a syntax line. On an older setup, add # syntax=docker/dockerfile:1 as the first line of the Dockerfile.

The Docker documentation describes secret mounts as "temporarily available inside the build container, for the duration of the build instruction." In our test, the request in the RUN step went through the proxy, and a unique test value passed as a secret was not found in docker history, the image configuration or the saved image.

Secrets are not part of the build cache, and Docker notes that "Changing the value of a secret doesn't result in cache invalidation." In our test, a second build with a wrong password reused the cached step and finished successfully, without checking the new value. When the step must run again with a new password, build with --no-cache, or change a build argument used only to invalidate the cache, as the Docker documentation suggests.

Pass the Proxy at Run Time

The simplest way is an environment variable, for example with docker run --env-file. It works, but the Docker documentation warns, about proxy variables, that "Environment variables are stored as plain text in the container's configuration, and as such can be inspected through the remote API or committed to an image when using docker commit." In our test, the full proxy URL was visible with docker inspect. That can be acceptable on a machine only you control. On a shared host, anyone who can use Docker there can read it.

Docker Compose secrets

Compose secrets keep the value out of the container's environment. Compose mounts each secret as a file at /run/secrets/<secret_name>:

compose.yaml
services:
  scraper:
    image: scraper
    secrets:
      - proxy_url

secrets:
  proxy_url:
    file: ./proxy_url.txt

Put the proxy URL in proxy_url.txt and add that file to both .gitignore and .dockerignore, so it stays out of the repository and out of the build context. The snippet above reads it from /run/secrets/proxy_url. In our test, the request went through the proxy and the value did not appear in the container environment shown by docker inspect. The Compose documentation explains the reason for this approach: "Environment variables are often available to all processes, and it can be difficult to track access." Compose secrets are not a vault, though. Outside Docker Swarm, Compose delivers each secret by bind-mounting the file, so the value is still stored in plain text in proxy_url.txt on the host, and anyone who can run Docker commands there can read it from the container. On a shared host, also limit who can use Docker and who can read the secret file.

Using the Proxy in CI/CD

GitHub Actions

Store the proxy URL as a repository secret, and pass it to the step that needs it as an environment variable. The example starts the workflow by hand with workflow_dispatch. Use the trigger that fits your project:

.github/workflows/scrape.yml
on: workflow_dispatch

jobs:
  scrape:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.13'
      - run: pip install requests
      - name: Run the job through the mobile proxy
        env:
          PROXY_URL: ${{ secrets.MOBILE_PROXY_URL }}
        run: python app.py

The env entry only applies to that step. If you split the proxy check and the main job into separate steps, give each step the same env entry, or set env at the job level. If MOBILE_PROXY_URL is not set, GitHub passes an empty string, which the empty check in the snippet above catches.

A few points from the GitHub documentation are worth knowing:

GitHub "automatically redacts the contents of all GitHub secrets that are printed to workflow logs," but because a value can be transformed in many ways, "this redaction is not guaranteed." Do not print the proxy URL, even partly.
By default, secrets "are not passed to the runner when a workflow is triggered from a forked repository." That applies to the standard pull_request trigger. In private and internal repositories, the Actions settings can allow fork pull requests to receive secrets. A pull_request_target workflow can access repository secrets, so keep untrusted fork code out of any job that uses the proxy secret.
Avoid passing secrets on the command line, where other users on the machine may see them, for example with the ps command. Use environment variables or standard input instead, as the documentation recommends.

GitLab CI

In GitLab, add the proxy URL as a CI/CD variable in the project settings and mark it as masked and protected. A masked value is replaced with [MASKED] in job logs. To be masked, the value must be a single line with no spaces and at least 8 characters long. If "Expand variable reference" is enabled, only a limited set of special characters is allowed, so GitLab recommends turning that option off for masked variables. Turn it off for the proxy variable: passwords often contain characters outside that set, and a percent-encoded password always contains %. A protected variable is only available in pipelines on protected branches or tags, so jobs on other branches will not find PROXY_URL. GitLab adds that merge request and merged results pipelines "can optionally access protected variables," so check how your project is configured.

GitLab also states that masking "is not a guaranteed way to prevent malicious users from accessing variable values." Treat masking as a safety net, not as protection, and limit who can change the pipeline.

Check the proxy before the job starts

A wrong password or an expired proxy should stop a pipeline at the start, not halfway through a long job. Run a quick check without retries as the first step, as described in How to Handle Retries, Rate Limits and Session State with Mobile Proxies. Log the exit IP it returns, never the proxy URL.

Parallel Jobs and IP Rotation

CI systems often run jobs in parallel, for example as a matrix. Jobs that use the same dedicated mobile proxy endpoint generally use the same exit IP at a given time, and a rotation on that endpoint affects all of them. Use separate proxy connections when jobs need to rotate independently, and confirm with your provider that separate endpoints rotate separately. Separate connections do not guarantee separate public IPs, though. Mobile carriers use CGNAT, so two connections on the same carrier can leave through the same public IP, as What Is CGNAT? explains. If the jobs need distinct public IPs, check each job's exit IP before it starts.

Scheduled rotations can also fall in the middle of a job. Check the exit IP at the start and end of each job, and for jobs that log in, plan rotations between jobs. How Mobile Proxy Rotation Works covers the mechanics.

What We Tested

We ran the examples with Docker Engine 29.10 and Docker Compose 5.7, a local Python base image with requests 2.34.2, and a local test proxy with Basic authentication. Docker Hub was not reachable from our test environment, so the tests used that local base image in place of python:3.13-slim. Builds and containers used the host network so they could reach the test proxy, and the requests went to a local test page instead of api.ipify.org. The curl and requests tests ran on the host with curl 8.5.0, the curl https:// tests used a local TLS test page, and for NO_PROXY we only tested a single IP address. The empty proxy URL tests ran on the host, in docker run and in a build. The dot and CIDR forms rest on the curl documentation. We did not run the GitHub Actions and GitLab CI examples. Those sections follow the official documentation. The results:

Test Result
Proxy URL set with ENV in the Dockerfile Visible with docker inspect and docker history
Proxy URL passed as --build-arg PROXY_URL, declared with ARG and used in RUN Visible in docker history
Proxy URL passed as --build-arg HTTP_PROXY, not declared in the Dockerfile, then declared with ARG HTTP_PROXY Not in docker history while undeclared. Visible in docker history once declared
Secret passed with --secret and RUN --mount=type=secret Request went through the proxy during the build. The value was not found in docker history, the image config or the saved image
Same build again with a wrong password in the secret, then with --no-cache Step reused from the cache and the build succeeded. With --no-cache: 407 and the build failed
Same Dockerfile built without --secret Stopped with "Proxy URL is empty" and the build failed
Empty PROXY_URL, empty secret file or whitespace-only secret file, without the empty check Request went direct, the host's own IP was printed, exit code 0
Same cases with the empty check, on the host and with docker run -e PROXY_URL= Stopped with "Proxy URL is empty" before any request, exit code 1
Proxies set in the Docker client config.json HTTP_PROXY, HTTPS_PROXY and NO_PROXY, in uppercase and lowercase, set in a new container, credentials included, and visible with docker inspect
Proxy URL passed with --env-file Request went through the proxy. Value visible with docker inspect
Proxy URL passed as a Compose secret Request went through the proxy. Value not in the container environment, mounted at /run/secrets/proxy_url
curl to an http:// target with lowercase http_proxy, then uppercase HTTP_PROXY Lowercase used. Uppercase ignored, request went direct
curl to an https:// target with HTTPS_PROXY, https_proxy, and only ALL_PROXY Proxy used in all three cases
requests with HTTP_PROXY set, then with NO_PROXY for the target Proxy used. With NO_PROXY, request went direct
requests with NO_PROXY for the target and the proxy passed with proxies= Proxy used: NO_PROXY did not apply
requests with a wrong password in HTTP_PROXY and the right one in session.proxies 407: the environment variable won
Same, with the right proxy passed with proxies= on the request Request went through the proxy

Checklist

Send only your application's target requests through the mobile proxy, not image pulls or package installs.
Use a dedicated variable such as PROXY_URL and pass the proxy explicitly in code.
Stop the job if the proxy URL is empty, before the first request.
Never put the proxy URL in a Dockerfile with ENV or --build-arg.
Use build secrets if a build step really needs the proxy. If that step must run again with a changed secret, invalidate its cache or rebuild with --no-cache.
Add secret files such as proxy_url.txt to both .gitignore and .dockerignore.
At run time, prefer Compose secrets or your platform's secret store over plain environment variables on shared hosts.
Store the URL as a CI secret or masked, protected variable, and never print it.
Check the proxy at the start of each job and log the exit IP, not the URL.
Use separate connections for jobs that need independent rotation, and check each job's exit IP if they need distinct public IPs.

FAQ

Should I set the mobile proxy in ~/.docker/config.json?

Usually not. That setting adds the proxy variables, with credentials, to every container you start and fills in the proxy build arguments, so every tool that reads them uses the proxy. Configure the proxy in your application instead.

Is it safe to pass the proxy URL as an environment variable?

It works, but the value is stored in plain text in the container configuration and can be read with docker inspect. Compose secrets keep it out of the environment and the inspect output. On shared hosts, also limit who can use Docker.

Why does curl ignore my HTTP_PROXY variable?

curl only reads http_proxy in lowercase, for security reasons linked to the CGI protocol. Set the lowercase form, or pass the proxy with curl's -x option.

Can a pull request from a fork use my proxy in GitHub Actions?

Not by default with the standard pull_request trigger: GitHub does not pass secrets to workflows started from a fork, unless a private or internal repository has been set to allow it. A pull_request_target workflow can access repository secrets, so keep untrusted fork code out of any job that uses the proxy secret.

Platform terms of service still apply. Running jobs through mobile proxies changes how your requests reach a website. It does not authorize activity that a platform prohibits.

Related Reading

● 99.9% Uptime ⚡ 4G/5G Carrier SIMs HTTP · SOCKS5 · OpenVPN

Dedicated Mobile Proxies for Automated Jobs

PowerProxy provides dedicated mobile connections on real carrier SIMs, with HTTP, SOCKS5, and OpenVPN support. In Amsterdam, PowerProxy offers Odido, KPN, and Vodafone SIMs.

✓HTTP, SOCKS5 and OpenVPN
✓A physical modem and SIM reserved for you alone
✓Rotation at set times or via API
✓Odido, KPN and Vodafone SIMs in Amsterdam

No credit card required  ·  4.3 / 5 on Trustpilot ★★★★★  ·  1,200+ businesses

喜欢这篇文章? 分享给您的朋友和同事
Narmin Kamilsoy
作者

Narmin Kamilsoy

特约作者,在我们的博客上分享见解与故事。

WeChat Telegram