I’m trying to build a docker image of a python project. My project has one dependency package, "my_package"
which needs to be install first. It’s a namespace package.
I’m having this error when building docker image of this python project. I can pretty much install everything with RUN pip install XYZ
inside Dockerfile but I’m not sure what’s the proper way to install local python namespace packages, here, my_package
, properly.
I followed this exact same steps on a normal python package that is NOT A NAMESPACE PACKAGE and it works absolutely fine. Any pointers to install python namespace packages in Docker?
Dockerfile:
ARG BASE_IMAGE=ubuntu:18.04 FROM ${BASE_IMAGE} AS compile-image ARG BASE_IMAGE=ubuntu:18.04 ENV PYTHONUNBUFFERED TRUE RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y ca-certificates g++ python3.8-dev python3.8-distutils python3.8-venv openjdk-11-jre-headless curl && rm -rf /var/lib/apt/lists/* && cd /tmp && curl -O https://bootstrap.pypa.io/get-pip.py && python3.8 get-pip.py RUN python3.8 -m venv /home/venv ENV PATH="/home/venv/bin:$PATH" RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 RUN update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1 COPY my_package/* /tmp/my_package/ WORKDIR /tmp/my_package/ RUN python setup.py install
Docker build command:
$ DOCKER_BUILDKIT=1 docker build --file Dockerfile .
Error:
[+] Building 0.6s (12/12) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 907B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/ubuntu:18.04 0.1s => [internal] load build context 0.0s => => transferring context: 3.11kB 0.0s => [compile-image 1/8] FROM docker.io/library/ubuntu:18.04@sha256:4bc3ae6596938cb0d9e5ac51a1152ec9dcac2a1c50829c74abd9c4361e321b26 0.0s => CACHED [compile-image 2/8] RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install 0.0s => CACHED [compile-image 3/8] RUN python3.8 -m venv /home/venv 0.0s => CACHED [compile-image 4/8] RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 0.0s => CACHED [compile-image 5/8] RUN update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1 0.0s => [compile-image 6/8] COPY my_package/* /tmp/my_package/ 0.0s => [compile-image 7/8] WORKDIR /tmp/my_package/ 0.0s => ERROR [compile-image 8/8] RUN python setup.py install 0.4s ------ > [compile-image 8/8] RUN python setup.py install: #12 0.370 error in my_package setup command: Distribution contains no modules or packages for namespace package 'my_package' ------ executor failed running [/bin/sh -c python setup.py install]: exit code: 1
my_package structure:
my_package ├── [4.0K] my_package │ └── [ 56] __init__.py └── [ 162] setup.py
setup.py:
from setuptools import setup, find_packages setup( name='my_package', version='0.0.1', namespace_packages=['my_package'], packages=find_packages() )
__init__.py file:
__import__('pkg_resources').declare_namespace(__name__)
Advertisement
Answer
In your Dockerfile, you need to put :
COPY my_package/. /tmp/my_package/