8

I have a Dockerfile in a directory called docker_test. The structure of docker_test is as follows:

M00618927A:docker_test i854319$ ls 
Dockerfile  hello_world.py

My dockerfile looks like below:

  ### Dockerfile 

# Created by Baktaawar 

# Pulling from base Python image 

FROM python:3.6.7-alpine3.6

# author of file
LABEL maintainer="Baktawar"

# Set the working directory of the docker image 
WORKDIR /docker_test
COPY . /docker_test


# packages that we need

RUN pip --no-cache-dir install numpy pandas jupyter


EXPOSE 8888

ENTRYPOINT ["python"]

CMD ["hello_world.py"]

I run the command

docker build -t dockerfile . 

It starts the building process but then gives the following error in not being able to get the numpy etc installed in the image

Sending build context to Docker daemon  4.096kB
Step 1/8 : FROM python:3.6.7-alpine3.6
 ---> 8f30079779ef
Step 2/8 : LABEL maintainer="Baktawar"
 ---> Running in 7cf081021b1e
Removing intermediate container 7cf081021b1e
 ---> 581cf24fa4e6
Step 3/8 : WORKDIR /docker_test
 ---> Running in 7c58855c4332
Removing intermediate container 7c58855c4332
 ---> dae70a34626b
Step 4/8 : COPY . /docker_test
 ---> 432b174b4869
Step 5/8 : RUN pip --no-cache-dir install numpy pandas jupyter
 ---> Running in 972efa9336ed
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/cf/8d/6345b4f32b37945fedc1e027e83970005fc9c699068d2f566b82826515f2/numpy-1.16.2.zip (5.1MB)
Collecting pandas
  Downloading https://files.pythonhosted.org/packages/81/fd/b1f17f7dc914047cd1df9d6813b944ee446973baafe8106e4458bfb68884/pandas-0.24.1.tar.gz (11.8MB)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 357, in get_provider
        module = sys.modules[moduleOrReq]
    KeyError: 'numpy'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-8c3o0ycd/pandas/setup.py", line 732, in <module>
        ext_modules=maybe_cythonize(extensions, compiler_directives=directives),
      File "/tmp/pip-install-8c3o0ycd/pandas/setup.py", line 475, in maybe_cythonize
        numpy_incl = pkg_resources.resource_filename('numpy', 'core/include')
      File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1142, in resource_filename
        return get_provider(package_or_requirement).get_resource_filename(
      File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 359, in get_provider
        __import__(moduleOrReq)
    ModuleNotFoundError: No module named 'numpy'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-8c3o0ycd/pandas/
You are using pip version 18.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The command '/bin/sh -c pip --no-cache-dir install numpy pandas jupyter' returned a non-zero code: 1

How can I get this basic setup done?

5
  • 2
    This may be different from your issue, but I've had problems with numpy and musl (alpine) in the past. You might want to try a glibc base image (e.g. python:3.6-slim) if that's within your constraints. Commented Mar 6, 2019 at 21:57
  • Let me try it and see Commented Mar 6, 2019 at 21:58
  • Is alpine absolutely needed? I had an experience in past trying to combine alpine with pandas/numpy and the size of final image was not less than non-alpine. Commented Mar 6, 2019 at 22:00
  • I don't know. I felt alpine is a smaller version of python. Is it advisable to use pre built base images for ubuntu or python or installing our own python is advisable? Commented Mar 6, 2019 at 22:02
  • I don't post it as answer but this docker was useful for me year ago: pandas/Dockerfile. You can see similarities with @andreas-lorenzen answer in apk commands Commented Mar 6, 2019 at 22:08

3 Answers 3

10

You basically need to install the following on alpine, in order to be able to install numpy:

apk --no-cache add musl-dev linux-headers g++

Try the following Dockerfile:

### Dockerfile
# Created by Baktawar
# Pulling from base Python image

FROM python:3.6.7-alpine3.6

# author of file
LABEL maintainer="Baktawar"

# Set the working directory of the docker image
WORKDIR /app
COPY . /app

# Install native libraries, required for numpy
RUN apk --no-cache add musl-dev linux-headers g++

# Upgrade pip
RUN pip install --upgrade pip

# packages that we need
RUN pip install numpy && \
    pip install pandas && \
    pip install jupyter

EXPOSE 8888

ENTRYPOINT ["python"]

CMD ["hello_world.py"]

You may find this gist interresting:

https://gist.github.com/orenitamar/f29fb15db3b0d13178c1c4dd611adce2

And this package on alpine, is also of interrest I think:

https://pkgs.alpinelinux.org/package/edge/community/x86/py-numpy

Update

In order to tag the image properly, use the syntax:

docker build -f <dockerfile> -t <tagname:tagversion> <buildcontext>

For you, this would be:

docker build -t mypythonimage:0.1 .
Sign up to request clarification or add additional context in comments.

3 Comments

Cool. I saw the other comment and it's an alpine issue. You have helped answer how to work with alpine. Gr8. One last question- my dockerfile is named dockerfile. How do I add tag to it. By default it adds latest tag. But I am not able to save the dockerfile txt file as dockerfile:test or something. It doesn't take : in file name while saving.
@Baktaawar it's not that "alpine has issues" - it's more about: after you apk all needed packages final image is not so thin.
Thanks - please mark the solution as accepted if it solves your issue :) otherwise, let me know what problem you are facing.
0

This is an issue with alpine. I replaced

FROM python:3.9-alpine

with FROM python:3.9-slim-buster . And it worked.

Ref; https://www.freecodecamp.org/news/how-to-dockerize-a-flask-app/

Comments

0

Just try to use Slim images instead of other images, It worked for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.