Docker build fails on CI

Hello,
Docker build fails in 50% of cases (during pipenv install), failure example:

1st run with error:

Step 14/21 : RUN pipenv install --system --skip-lock   && python3 -m nltk.downloader stopwords punkt wordnet   && python3 -m spacy download en_core_web_md
 ---> Running in fe6765bc92b9
Installing dependencies from Pipfile…
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/local/lib/python3.6/site-packages/nltk/__init__.py", line 129, in <module>
    from nltk.collocations import *
  File "/usr/local/lib/python3.6/site-packages/nltk/collocations.py", line 40, in <module>
    from nltk.metrics import ContingencyMeasures, BigramAssocMeasures, TrigramAssocMeasures
  File "/usr/local/lib/python3.6/site-packages/nltk/metrics/__init__.py", line 16, in <module>
    from nltk.metrics.scores import (
  File "/usr/local/lib/python3.6/site-packages/nltk/metrics/scores.py", line 18, in <module>
    from scipy.stats.stats import betai
  File "/usr/local/lib/python3.6/site-packages/scipy/__init__.py", line 61, in <module>
    from numpy import show_config as show_numpy_config
  File "/usr/local/lib/python3.6/site-packages/numpy/__init__.py", line 151, in <module>
    from . import random
  File "/usr/local/lib/python3.6/site-packages/numpy/random/__init__.py", line 179, in <module>
    from . import _pickle
  File "/usr/local/lib/python3.6/site-packages/numpy/random/_pickle.py", line 1, in <module>
    from .mtrand import RandomState
  File "mtrand.pyx", line 1, in init numpy.random.mtrand
  File "bit_generator.pyx", line 404, in init numpy.random.bit_generator
TypeError: 'errstate' object is not callable

2nd run with error:

Step 14/21 : RUN pipenv install --system --skip-lock   && python3 -m nltk.downloader stopwords punkt wordnet   && python3 -m spacy download en_core_web_md
 ---> Running in 77a7ad3ca006
Installing dependencies from Pipfile…
/usr/local/lib/python3.6/runpy.py:125: RuntimeWarning: 'nltk.downloader' found in sys.modules after import of package 'nltk', but prior to execution of 'nltk.downloader'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Unzipping corpora/stopwords.zip.
[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt.zip.
[nltk_data] Downloading package wordnet to /root/nltk_data...
[nltk_data]   Unzipping corpora/wordnet.zip.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.6/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/local/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/local/lib/python3.6/site-packages/spacy/__init__.py", line 10, in <module>
    from thinc.neural.util import prefer_gpu, require_gpu
  File "/usr/local/lib/python3.6/site-packages/thinc/__init__.py", line 5, in <module>
    import numpy  # noqa: F401
  File "/usr/local/lib/python3.6/site-packages/numpy/__init__.py", line 140, in <module>
    from . import core
  File "/usr/local/lib/python3.6/site-packages/numpy/core/__init__.py", line 59, in <module>
    from . import numeric
  File "/usr/local/lib/python3.6/site-packages/numpy/core/numeric.py", line 18, in <module>
    from .multiarray import (
ImportError: cannot import name 'int_asbuffer'

3rd run without errors;

There is no error with a local build, about half of the cases are successful, another half I need to rebuild workflow twice to create the image.
Can somebody suggest something?

Hello @nenych! Welcome to our CircleCI Community!

This could be due to a conflict in the packages you are installing within your build. I just did a quick test of the pipenv install command and it was successful. (My build link is further below.)

Which image are you using in your config.yml?

version: 2.1
jobs:
  build:
    docker:
    - image: circleci/python:3.6.1
    steps:
    - run:
        name: Test
        command: |
          sudo pip install pipenv
          pipenv install flake8 --skip-lock
workflows:
  build-and-deploy:
    jobs:
    - build

Reference: https://app.circleci.com/pipelines/github/JonathanC503/circleci-sandbox/91/workflows/f616f6a5-367e-4435-a61f-c320475a79ee/jobs/155

Hello @JonathanC503 ,
I am using alpine 3.9, here is my circle config:

---
version: 2.1
jobs:
  docker:
    working_directory: /opt/build
    docker:
      - image: alpine:3.9
        environment:
          ECR_REPOSITORY: <repository>
    steps:
      - run:
        name: Install system dependencies
        command: |
          apk add -U git curl openssh tar gzip python3
          pip3 install awscli
      - checkout
      - setup_remote_docker:
          version: 19.03.8
      - run:
        name: Install Docker client
        command: |
          VER="19.03.8"
          curl -L -o /tmp/docker-$VER.tgz https://download.docker.com/linux/static/stable/x86_64/docker-$VER.tgz
          tar -xz -C /tmp -f /tmp/docker-$VER.tgz
          mv /tmp/docker/* /usr/bin
      - run:
        name: Build docker image
        command: |
          eval $(aws ecr get-login --no-include-email)
          echo ${DH_PASSWORD} | docker login --username=${DH_USER} --password-stdin
          if [ -n "${CIRCLE_TAG}" ];
            then DOCKER_TAG=${CIRCLE_TAG};
          else DOCKER_TAG=sha-${CIRCLE_SHA1::7};
          fi
          docker build -t ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${ECR_REPOSITORY}:${DOCKER_TAG} .
          docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${ECR_REPOSITORY}:${DOCKER_TAG}
workflows:
  version: 2
  docker:
    jobs:
    - docker:
        context: org-global
        filters:
          tags:
            only: /.*/

I am building my docker image based on python:3.6-slim-buster and as I said the problem is reproducible only with circle ci on the docker build command.

I replaced my circle image and also had no luck. Executor:

version: 2.1
jobs:
  docker:
    working_directory: /opt/build
    docker:
      - image: cimg/python:3.6.12

I double-checked and I have no common modules on the base image and my Pipfile.
My docker file:

FROM python:3.6-slim-buster
ENV DEBIAN_FRONTEND="noninteractive"
RUN apt-get update \
    && apt-get install --no-install-recommends -y git locales locales-all tidy tzdata gcc g++ \
    && pip3 install pipenv

# Install confd
ARG CONFD_VER="0.16.0"
ARG BINARIES_URL="https://github.com/kelseyhightower/confd/releases/download"
ADD ${BINARIES_URL}/v${CONFD_VER}/confd-${CONFD_VER}-linux-amd64 /usr/local/bin/confd
RUN chmod +x /usr/local/bin/confd

RUN locale-gen en_US.UTF-8
ENV LANG="en_US.UTF-8" LC_ALL="en_US.UTF-8"

RUN mkdir -p /opt/proj
WORKDIR /opt/proj

COPY ./ ./

RUN pipenv install --system --skip-lock \
    && python3 -m nltk.downloader stopwords punkt wordnet \
    && python3 -m spacy download en_core_web_md

# Add confd configs
COPY docker/conf.d/* /etc/confd/conf.d/
COPY docker/templates/* /etc/confd/templates/

# Add entrypoint
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["bash", "-c", "python3 ${COMMAND}"]