slimtoolkit / slim

Slim(toolkit): Don't change anything in your container image and minify it by up to 30x (and for compiled languages even more) making it secure too! (free and open source)
Apache License 2.0
19.34k stars 727 forks source link

Python: ModuleNotFoundError: No module named 'traceback' #162

Open jurgonaut opened 4 years ago

jurgonaut commented 4 years ago

I'm trying to minimize a docker container that uses python and I am getting an error. My Dockerfile looks like this:

FROM amazonlinux:latest

RUN yum install -y make gcc nano openssl-devel bzip2-devel libffi-devel tar zip unzip gzip wget mysql mariadb-devel python-devel sqlite-devel aws-cli \
    && cd /tmp \
    && wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz \
    && tar -xzf Python-3.7.3.tgz \
    && cd Python-3.7.3 \
    && ./configure \
    && make \
    && make altinstall \
    && cd .. \
    && rm -r Python-3.7.3 \
    && rm -r Python-3.7.3.tgz \
    && pip3.7 install scrapy \
    && pip3.7 install mysqlclient \
    && cd /

When I run the container I get the error:

Error processing line 1 of /usr/local/lib/python3.7/site-packages/zope.interface-5.1.0-py3.7-nspkg.pth:

Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site.py", line 168, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 580, in module_from_spec
AttributeError: 'NoneType' object has no attribute 'loader'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site.py", line 586, in <module>
    main()
  File "/usr/local/lib/python3.7/site.py", line 573, in main
    known_paths = addsitepackages(known_paths)
  File "/usr/local/lib/python3.7/site.py", line 349, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/usr/local/lib/python3.7/site.py", line 207, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/usr/local/lib/python3.7/site.py", line 178, in addpackage
    import traceback
ModuleNotFoundError: No module named 'traceback'

The command I am using for building the slim container is:

docker-slim build --http-probe=false python-test-slim

Is there a flag I am missing or something similar? I am new to this project so I am not sure if this issue related to docker-slim. But I would be really happy I could get some help because the results amazing.

kcq commented 4 years ago

Is it your complete Dockerfile @jurgonaut ? Is it just one RUN instruction (and FROM)? This looks like a custom base image... Do you have another image with your application code where you use this custom base image?

jurgonaut commented 4 years ago

Yes this is all. There is also a COPY which copies a simple python script to test if all the packages are installed. The python script only has a few imports. The base image is an official AWS image. What do you suggest, will it work with another base image?

kcq commented 4 years ago

@jurgonaut is that COPY instruction in the same Dockerfile? Do you set the ENTRYPOINT and/or CMD instructions? What do they look like if you do? If not, how do you invoke your simple python script in the container?

jurgonaut commented 4 years ago

@kcq This is the rest of the Dockerfile

ADD ./entry.sh /usr/local/bin/entry.sh
ADD ./test.py /usr/local/bin/test.py

RUN ["chmod", "+x", "/usr/local/bin/entry.sh"]
RUN ["chmod", "+x", "/usr/local/bin/test.py"]

WORKDIR /usr/local/bin
ENTRYPOINT ["/bin/bash", "/usr/local/bin/entry.sh"]

The file entry.sh content is:

python3.7 ./test.py

And test.py content is:

import MySQLdb

print("SUCCESS")
michaelcukier commented 9 months ago

@jurgonaut did you find a solution ? I have the exact same problem

jurgonaut commented 8 months ago

@michaelcukier No I didn't. I haven't work on this since 2020 so I don't know of any recent developments.