When building the container, I wanted only apt to go through the proxy server, so I investigated how to pass only apt communication through the proxy.
Based on the contents specified in --build-arg
, create a Dockerfile that sets the proxy server for apt.
docker build
is performed when docker run
is performedIn my case, I want the proxy to be used when running the container with docker run
, so I set the proxy in /etc/apt/apt.conf.d/
.
Dockerfile
FROM ubuntu:20.04
ARG APT_PROXY
RUN if [ "$APT_PROXY" != "" ]; then echo "Acquire::http { Proxy \"$APT_PROXY\"; };" > /etc/apt/apt.conf.d/01proxy; fi
RUN apt update && \
apt install --no-install-recommends -y curl && \
rm -rf /var/lib/apt/lists/*
First, the ARG on the second line allows you to set a build argument named APT_PROXY
. The proxy specified in APT_PROXY
will be used for apt communication.
In the first RUN, the proxy is set in /etc/apt/apt.conf.d/01proxy
. Since 01proxy
is created only when APT_PROXY
is specified at build time, if APT_PROXY
is not specified, apt will get the repository on the Internet directly.
The second RUN installs curl as an example.
docker build
and do not use a proxy when doing docker run
If you use a proxy only when building a container and you don't want it to be used when docker run
, you can set the http_proxy environment variable as follows instead of setting it in apt.conf.d. I think you can use the specified proxy only at build time.
Dockerfile
FROM ubuntu:20.04
ARG APT_PROXY
RUN export http_proxy="$APT_PROXY"; \
apt update && \
apt install --no-install-recommends -y curl && \
rm -rf /var/lib/apt/lists/*
When using the docker
command, use --build-arg
at build time to specify the proxy and build as shown below.
$ docker build --build-arg APT_PROXY=http://proxy:8080/ -t apt-proxy-test .
When using docker-compose, set APT_PROXY in args as follows.
docker-compose.yml
version: "3"
services:
apt-proxy-test:
build:
context: .
args:
- APT_PROXY=http://proxy:8080/
restart: unless-stopped
Recommended Posts