Docker build error: “could not connect to server”

2019-06-07 10:31发布

问题:

Context: OS: Windows 10 Pro; Docker ver: 18.09.0 (build 4d60db4); Behind corporate proxy, using CNTLM to solve this issue. (currently pulling / running image works fine)

Problem: I was trying to build the following Dockerfile:

FROM alpine:3.5
RUN apk add --update \
    python3
RUN pip3 install bottle
EXPOSE 8000
COPY main.py /main.py
CMD python3 /main.py

This is what I got:

Sending build context to Docker daemon  11.26kB
Step 1/6 : FROM alpine:3.5
 ---> dc496f71dbb5
Step 2/6 : RUN apk add --update     python3
 ---> Running in 7f5099b20192
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.5/main: could not connect to server (check repositories file)
WARNING: Ignoring APKINDEX.c51f8f92.tar.gz: No such file or directory
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.5/community: could not connect to server (check repositories file)
WARNING: Ignoring APKINDEX.d09172fd.tar.gz: No such file or directory
ERROR: unsatisfiable constraints:
  python3 (missing):
    required by: world[python3]
The command '/bin/sh -c apk add --update     python3' returned a non-zero code: 1

I was able to access the URL from a browser, so there is no problem with the server itself.

I suspected that it has something to do with the proxy not being propagated to the container, as explained in this question, since I also did not get the http_proxy line when running docker run alpine env. However, after entering the proxies into the config file, it finally appeared. Yet the problem still exists.

I also tried to change the DNS as instructed here, but the problem is still unsolved.

回答1:

I finally managed to solve this problem, and the culprit was my setting in the CNTLM. For a background story, please check this post.

The root cause of this problem is that the docker container could not access the internet from inside the VM due to wrong IP setting inside the CNTLM.ini.

Normally CNTLM listens to 127.0.0.1:3128 by default to forward the proxy. I followed the default, and thus set the proxy setting on Docker (for the daemon - through the GUI, and for the container - through config.json) is also set into that address and port. It turns out that this "localhost" does not apply to the VM where docker sits, since the VM has its own localhost. Long story short, the solution is to change that address into dockerNAT IP address (10.0.75.1:3128) in all of the following locations:

  • CNTLM.ini (on the Listen line. Actually if we use CNTLM for other purposes as well, it is possible to supply more than one Listen line)
  • Docker daemon's proxy (through the Docker setting GUI)
  • Docker container config.json (usually in C:\Users\<username>\.docker), by adding the following lines:

    "proxies":
     {
       "default":
       {
         "httpProxy": "http://10.0.75.1:3128",
         "httpsProxy": "http://10.0.75.1:3128",
         "noProxy": <your no_proxy>
       }
     }
    

also check these related posts:

  • Building a docker image for a node.js app fails behind proxy
  • Docker client ignores HTTP_PROXY envar and build args
  • Beginner having trouble with docker behind company proxy