1

Hello docker specialists, I start to dockerize my python3 application. The simple Dockerfile contain

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN python -m pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
RUN mkdir ./src ./setup ./logfiles 
COPY src ./src

the requirements.txt file contains only one line simplekml. In the build process with docker build -t my_container_name . the simplekml module was installed (in user root context). If I use my container with

MY_DOCKER_DATA_PATH=/SomewhereOnMyHost/docker_data
docker run  -v $MY_DOCKER_DATA_PATH/setup:/usr/src/app/setup \
            -v $MY_DOCKER_DATA_PATH/logfiles:/usr/src/app/logfiles \
            -e TIMESTAMP_FOLDER='../logfiles/12345678-123456' \
            -e SETUP_FILE='../setup/setup.json' \
            -it my_container_name bash

if I start myApplication.py I got the following error message:

root@70f12323172c:/usr/src/app/src# ./myApplication.py
Traceback (most recent call last):
  File "/usr/src/app/src/./myApplication.py", line 12, in <module>
    import Google_KML
  File "./Modules/Google_KML.py", line 5, in <module>
    import simplekml
ModuleNotFoundError: No module named 'simplekml'

The first line import Google_KML is my own module that wraps all simplekml related tasks. This module imports simplekml, but can't find it. Why couldn't my application find the simplekml module? I try to recreate the docker image to log the build process. But docker uses the cache, which I can't prevent. It would be nice if you can provide some helpful solutions.

knigge
  • 31
  • 3

1 Answers1

0

I found the problem :-) My application and all my own modules are used in the first line #!/usr/bin/python3. On the development machine, this refers to /usr/bin/python3 -> python3.8 (python 3.8.8 is used there). In the container, the link refers to an older version /usr/bin/python3 -> python3.7 and which python3 shows /usr/local/bin/python3 . So my application uses Python3.7 instead of 3.8. Now I change the Dockerfile a bit

FROM python:3
WORKDIR /usr/src/app
RUN python -m pip install --upgrade pip \
    && rm /usr/bin/python3 \
    && ln -s /usr/local/bin/python3 /usr/bin/python3
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
RUN mkdir ./src ./setup ./logfiles 
COPY src ./src

The important line is ln -s /usr/local/bin/python3 /usr/bin/python3. Now I can let my python scripts untouched, they use /usr/bin/python3, but really they use /usr/local/bin/python3 and all is fine.

knigge
  • 31
  • 3