Aller au contenu principal

Utilisation de Docker avec un proxy

Soumis par lulu le lun 20/02/2023 - 21:08

Pour récupérer des images

Un grand nombre d'images pour Docker peuvent être récupérées sur le Docker Hub (https://hub.docker.com/) au moyen de la commande docker pull.

Pour que cette commande puisse se connecter sur Internet, il peut être nécessaire de configurer un proxy pour Docker. Les étapes pour ce faire sont les suivantes :

 

Créer ou éditer le fichier /etc/systemd/system/docker.service.d/http-proxy.conf

$ vi /etc/systemd/system/docker.service.d/http-proxy.conf

  [Service]                                                                                                                
  Environment="HTTPS_PROXY=http://myproxy.mydomain.fr:3127/"                                                       
  Environment="HTTP_PROXY=http://myproxy.mydomain.fr:3127/"                                                        
  Environment="NO_PROXY=*.myintranet.fr,localhost,127.0.0.1"

 

Prendre en compte les modifications et redémarrer Docker

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

 

Vérifier que la configuration a bien été prise en compte

$ systemctl show --property Environment docker
Environment=HTTPS_PROXY=http://myproxy.mydomain.fr:3127/
HTTP_PROXY=http://myproxy.mydomain.fr:3127/
NO_PROXY=*.myintranet.fr,localhost,127.0.0.1

 

La commande pull peut maintenant être lancée pour récupérer une image

$ docker pull debian

Using default tag: latest
latest: Pulling from library/debian
57df1a1f1ad8: Pull complete
Digest: sha256:439a6bae1ef351ba9308fc9a5e69ff7754c14516f6be8ca26975fb564cb7fb76
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest

 

Pour lancer des commandes (apt, wget...) dans un dokerfile

Si le projet utilise docker-compose, on crée un fichier .env à la racine du projet, au même niveau que le fichier docker-compose.yml :

$ vi .env

http_proxy=http://myproxy.mydomain.fr:3127/
https_proxy=http://myproxy.mydomain.fr:3128/

 

Ensuite, on récupère ces variables d'environnement dans le fichier Dockerfile :

FROM debian:bullseye

ARG HTTP_PROXY
ARG HTTPS_PROXY

ENV http_proxy=$HTTP_PROXY
ENV https_proxy=$HTTPS_PROXY

# System dependencies
RUN apt-get update -qq

# Gestion du proxy pour PECL
RUN pear config-set http_proxy ${http_proxy} &&\
    pear config-set php_ini $PHP_INI_DIR/php.ini