github.com/secure-build/gitlab-runner@v12.5.0+incompatible/docs/configuration/proxy.md (about)

     1  # Running GitLab Runner behind a proxy
     2  
     3  This guide aims specifically to making GitLab Runner with Docker executor to
     4  work behind a proxy.
     5  
     6  Before proceeding further, you need to make sure that you've already
     7  [installed Docker](https://docs.docker.com/install/) and
     8  [GitLab Runner](../install/index.md) on the same machine.
     9  
    10  ## Configuring CNTLM
    11  
    12  NOTE: **Note:**
    13  If you already use a proxy without authentication, this section is optional and
    14  you can skip straight to [configuring Docker](#configuring-docker-for-downloading-images).
    15  Configuring CNTLM is only needed if you are behind a proxy with authentication,
    16  but it's recommended to use in any case.
    17  
    18  [CNTLM](https://github.com/Evengard/cntlm) is a Linux proxy which can be used
    19  as a local proxy and has 2 major advantages compared to adding the proxy details
    20  everywhere manually:
    21  
    22  - One single source where you need to change your credentials
    23  - The credentials can not be accessed from the Docker Runners
    24  
    25  Assuming you [have installed CNTLM](https://www.howtoforge.com/linux-ntlm-authentication-proxy-isa-server-with-cntlm),
    26  you need to first configure it.
    27  
    28  ### Make CNTLM listen to the `docker0` interface
    29  
    30  For extra security, and to protect your server from the outside world, you can
    31  bind CNTLM to listen on the `docker0` interface which has an IP that is reachable
    32  from inside the containers. If you tell CNTLM on the Docker host to bind only
    33  to this address, Docker containers will be able to reach it, but the outside
    34  world won't be able to.
    35  
    36  1. Find the IP that Docker is using:
    37  
    38     ```sh
    39     ip -4 -oneline addr show dev docker0
    40     ```
    41  
    42     This is usually `172.17.0.1`, let's call it `docker0_interface_ip`.
    43  
    44  1. Open the config file for CNTLM (`/etc/cntlm.conf`). Enter your username,
    45     password, domain and proxy hosts, and configure the `Listen` IP address
    46     which you found from the previous step. It should look like this:
    47  
    48     ```
    49     Username     testuser
    50     Domain       corp-uk
    51     Password     password
    52     Proxy        10.0.0.41:8080
    53     Proxy        10.0.0.42:8080
    54     Listen       172.17.0.1:3128 # Change to your docker0 interface IP
    55     ```
    56  
    57  1. Save the changes and restart its service:
    58  
    59     ```sh
    60     sudo systemctl restart cntlm
    61     ```
    62  
    63  ## Configuring Docker for downloading images
    64  
    65  NOTE: **Note:**
    66  The following apply to OSes that have systemd support.
    67  
    68  Follow [Docker's documentation](https://docs.docker.com/config/daemon/systemd/#httphttps-proxy)
    69  how to use a proxy.
    70  
    71  The service file should look like this:
    72  
    73  ```ini
    74  [Service]
    75  Environment="HTTP_PROXY=http://docker0_interface_ip:3128/"
    76  Environment="HTTPS_PROXY=http://docker0_interface_ip:3128/"
    77  ```
    78  
    79  ## Adding Proxy variables to the Runner config
    80  
    81  The proxy variables need to also be added the Runner's config, so that it can
    82  get builds assigned from GitLab behind the proxy.
    83  
    84  This is basically the same as adding the proxy to the Docker service above:
    85  
    86  1. Create a systemd drop-in directory for the `gitlab-runner` service:
    87  
    88     ```sh
    89     mkdir /etc/systemd/system/gitlab-runner.service.d
    90     ```
    91  
    92  1. Create a file called `/etc/systemd/system/gitlab-runner.service.d/http-proxy.conf`
    93     that adds the `HTTP_PROXY` environment variable(s):
    94  
    95     ```ini
    96     [Service]
    97     Environment="HTTP_PROXY=http://docker0_interface_ip:3128/"
    98     Environment="HTTPS_PROXY=http://docker0_interface_ip:3128/"
    99     ```
   100  
   101  1. Save the file and flush changes:
   102  
   103     ```sh
   104     systemctl daemon-reload
   105     ```
   106  
   107  1. Restart GitLab Runner:
   108  
   109     ```sh
   110     sudo systemctl restart gitlab-runner
   111     ```
   112  
   113  1. Verify that the configuration has been loaded:
   114  
   115     ```sh
   116     systemctl show --property=Environment gitlab-runner
   117     ```
   118  
   119     You should see:
   120  
   121     ```ini
   122     Environment=HTTP_PROXY=http://docker0_interface_ip:3128/ HTTPS_PROXY=http://docker0_interface_ip:3128/
   123     ```
   124  
   125  ## Adding the proxy to the Docker containers
   126  
   127  After you [registered your Runner](../register/index.md), you might want to
   128  propagate your proxy settings to the Docker containers (for `git clone` and other
   129  stuff).
   130  
   131  To do that, you need to edit `/etc/gitlab-runner/config.toml` and add the
   132  following to the `[[runners]]` section:
   133  
   134  ```toml
   135  pre_clone_script = "git config --global http.proxy $HTTP_PROXY; git config --global https.proxy $HTTPS_PROXY"
   136  environment = ["https_proxy=http://docker0_interface_ip:3128", "http_proxy=http://docker0_interface_ip:3128", "HTTPS_PROXY=docker0_interface_ip:3128", "HTTP_PROXY=docker0_interface_ip:3128"]
   137  ```
   138  
   139  Where `docker0_interface_ip` is the IP address of the `docker0` interface. You need to
   140  be able to reach it from within the Docker containers, so it's important to set
   141  it right.
   142  
   143  NOTE: **Note:**
   144  In our examples, we are setting both lower case and upper case variables
   145  because certain programs expect `HTTP_PROXY` and others `http_proxy`.
   146  Unfortunately, there is no
   147  [standard](https://unix.stackexchange.com/questions/212894/whats-the-right-format-for-the-http-proxy-environment-variable-caps-or-no-ca#212972)
   148  on these kinds of environment variables.
   149  
   150  ## Proxy settings when using dind service
   151  
   152  When using the [docker-in-docker executor](https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor) (dind),
   153  it can be necessary to specify `docker:2375,docker:2376` in the `NO_PROXY`
   154  environment variable. This is because the proxy intercepts the TCP connection between:
   155  
   156  - `dockerd` from the dind container.
   157  - `docker` from the client container.
   158  
   159  The ports can be required because otherwise `docker push` will be blocked
   160  as it originates from the IP mapped to docker. However, in that case, it is meant to go through the proxy.
   161  
   162  When testing the communication between `dockerd` from dind and a `docker` client locally
   163  (as described here: <https://hub.docker.com/_/docker/>),
   164  `dockerd` from dind is initially started as a client on the host system by root,
   165  and the proxy variables are taken from `/root/.docker/config.json`.
   166  
   167  For example:
   168  
   169  ```json
   170  {
   171      "proxies": {
   172          "default": {
   173              "httpProxy": "http://proxy:8080",
   174              "httpsProxy": "http://proxy:8080",
   175              "noProxy": "docker:2375,docker:2376"
   176          }
   177      }
   178  }
   179  ```
   180  
   181  However, the container started for executing `.gitlab-ci.yml` scripts will have
   182  the environment variables set by the settings of the `gitlab-runner` configuration (`/etc/gitlab-runner/config.toml`).
   183  These are available as environment variables as is (in contrast to `.docker/config.json` of the local test above)
   184  in the dind containers running `dockerd` as a service and `docker` client executing `.gitlab-ci.yml`.
   185  In `.gitlab-ci.yml`, the environment variables will be picked up by any program honouring the proxy settings from default environment variables. For example,
   186  `wget`, `apt`, `apk`, `docker info` and `docker pull` (but not by `docker run` or `docker build` as per:
   187  <https://github.com/moby/moby/issues/24697#issuecomment-366680499>).
   188  
   189  `docker run` or `docker build` executed inside the container of the docker executor
   190  will look for the proxy settings in `$HOME/.docker/config.json`,
   191  which is now inside the executor container (and initially empty).
   192  Therefore, `docker run` or `docker build` executions will have no proxy settings. In order to pass on the settings,
   193  a `$HOME/.docker/config.json` needs to be created in the executor container. For example:
   194  
   195  ```yaml
   196  before_script:
   197    - mkdir -p $HOME/.docker/
   198    - 'echo "{ \"proxies\": { \"default\": { \"httpProxy\": \"$HTTP_PROXY\", \"httpsProxy\": \"$HTTPS_PROXY\", \"noProxy\": \"$NO_PROXY\" } } }" > $HOME/.docker/config.json'
   199  ```
   200  
   201  Because it is confusing to add additional lines in a `.gitlab-ci.yml` file that are only needed in case of a proxy,
   202  it is better to move the creation of the `$HOME/.docker/config.json` into the
   203  configuration of the `gitlab-runner` (`/etc/gitlab-runner/config.toml`) that is actually affected:
   204  
   205  ```toml
   206  [[runners]]
   207    pre_build_script = "mkdir -p $HOME/.docker/ && echo \"{ \\\"proxies\\\": { \\\"default\\\": { \\\"httpProxy\\\": \\\"$HTTP_PROXY\\\", \\\"httpsProxy\\\": \\\"$HTTPS_PROXY\\\", \\\"noProxy\\\": \\\"$NO_PROXY\\\" } } }\" > $HOME/.docker/config.json"
   208  ```
   209  
   210  NOTE: **Note:**
   211  An additional level of escaping `"` is needed here because this is the creation of a
   212  JSON file with a shell specified as a single string inside a TOML file.
   213  Because it is not YAML anymore, do not escape the `:`.
   214  
   215  Note that if the `NO_PROXY` list needs to be extended, wildcards `*` only work for suffixes
   216  but not for prefixes or CIDR notation.
   217  For more information, see
   218  <https://github.com/moby/moby/issues/9145>
   219  and
   220  <https://unix.stackexchange.com/questions/23452/set-a-network-range-in-the-no-proxy-environment-variable>.