Allows you to specify a proxy server to use for apt communication during docker build

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.

Contents to be described in Dockerfile

Based on the contents specified in --build-arg, create a Dockerfile that sets the proxy server for apt.

When using the same proxy server as when docker build is performed when docker run is performed

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

If you use a proxy only for 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/*

How to build a container

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

Reference page

Recommended Posts

Allows you to specify a proxy server to use for apt communication during docker build
Build a docker container for a python simple web server
Use Jenkins to build inside Docker and then create a Docker image.
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Build an authentication proxy server using Docker
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
Build a container for Docker x Laravel phpMyAdmin
Build a development environment for Docker, java, vscode
How to use nginx-ingress-controller with Docker for Mac
3 Tips to Improve Lead Time for Docker Build
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to build Docker + Springboot app (for basic learning)
How to use an array for a TreeMap key
How to build docker environment with Gradle for intelliJ
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
A memo to check when you try to use Lombok
If you want to make a Java application a Docker image, it is convenient to use jib.
(For myself) Build an IDE that you can touch from a browser with docker (trial)