api-get "Error reading from server" under Docker

I'm running the following command in Bash:

DEBIAN_FRONTEND=noninteractive apt-get update -qq \
  && apt-get install -y build-essential git libncurses5-dev openssl \
     libssl-dev  fop xsltproc unixodbc-dev curl

It runs, but fails in the middle:

Get:96 http://security.debian.org/ jessie/updates/main linux-libc-dev amd64 3.16.7-ckt9-3~deb8u1 [991 kB]
Get:97 http://security.debian.org/ jessie/updates/main curl amd64 7.38.0-4+deb8u2 [200 kB]
Get:98 http://security.debian.org/ jessie/updates/main openjdk-7-jre amd64 7u79-2.5.5-1~deb8u1 [176 kB]
Get:99 http://http.debian.net/debian/ jessie/main libgtk2.0-0 amd64 2.24.25-3 [2301 kB]
Err http://http.debian.net/debian/ jessie/main dpkg-dev all 1.17.25
  Error reading from server. Remote end closed connection [IP: 176.9.184.93 80]
Get:100 http://http.debian.net/debian/ jessie/main libatk-wrapper-java all 0.30.5-1 [30.3 kB]
Get:101 http://http.debian.net/debian/ jessie/main libatk-wrapper-java-jni amd64 0.30.5-1 [24.8 kB]
Get:102 http://http.debian.net/debian/ jessie/main libatomic1 amd64 4.9.2-10 [8992 B]
Get:103 http://http.debian.net/debian/ jessie/main libavahi-glib1 amd64 0.6.31-5 [36.4 kB]

And therefore the whole operation fails with the error

E: Failed to fetch http://http.debian.net/debian/pool/main/d/dpkg/dpkg-dev_1.17.25_all.deb  Error reading from server. Remote end closed connection [IP: 176.9.184.93 80]

E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

I'm running this as part of a Docker build. My Dockerfile reads

FROM debian:jessie
RUN DEBIAN_FRONTEND=noninteractive  \
    apt-get update -qq \
    && apt-get install -y \
       build-essential \
       git \
       libncurses5-dev \
       openssl \
       libssl-dev \
       fop \
       xsltproc \
       unixodbc-dev \
       curl

and I'm running docker build -t my-base:latest .

The apt-get command succeeds some of the time, and fails some of the time without my having changed anything. It seems to always succeed when I run it on my local development machine but fail often (but not always!) when I run it on an EC2 machine. Further, it seems like running apt-get update twice in a row before the apt-get install helps. I'm not at all positive of those last two sentences, though.

Any ideas what could be happening? Could there be something in apt-get that's caching a timestamp and then expecting it to be current?


Solution 1:

This is a problem you'll see more frequently with Docker images because the repositories you're accessing change frequently but the base image (and it's cached metadata) doesn't.

Try running apt-get clean && apt-get update before you install packages.