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.48k stars 730 forks source link

slimmed version of python3.10 version seems to loose package(s) #317

Open quemeraisc opened 2 years ago

quemeraisc commented 2 years ago

Expected Behavior

docker run of my image runs perfectly my python code using various packages (important here: juniper pyez and cryptography)


Actual Behavior

docker run of the slim version crashes output here :

Error processing line 1 of /usr/local/lib/python3.10/site-packages/junos_eznc-2.6.3-py3.7-nspkg.pth:

  Traceback (most recent call last):
    File "/usr/local/lib/python3.10/site.py", line 186, in addpackage
      exec(line)
    File "<string>", line 1, in <module>
    File "<frozen importlib._bootstrap>", line 568, in module_from_spec
  AttributeError: 'NoneType' object has no attribute 'loader'

Remainder of file ignored
Traceback (most recent call last):
  File "/usr/src/app/./enrichment/ios_xr_interfaces.py", line 70, in <module>
    user, p = userManager(getpass.getuser(), "")
  File "/usr/local/lib/python3.10/site-packages/Routers/genericConnect.py", line 85, in userManager
    from cryptography.fernet import Fernet
ModuleNotFoundError: No module named 'cryptography.fernet'

Dockerfile:

FROM python:3

WORKDIR /usr/src/app
RUN mkdir /usr/src/app/conf
RUN mkdir /usr/src/app/Routers
RUN mkdir /usr/src/app/enrichement

COPY ./enrichment /usr/src/app/enrichment
COPY ./Routers /usr/src/app/Routers
WORKDIR /usr/src/app/Routers
RUN pip install --no-cache-dir -r requirements.txt
RUN python3 setup.py sdist bdist_wheel
RUN pip3 install ./
WORKDIR /usr/src/app/

CMD ["python", "./enrichment/ios_xr_interfaces.py"]
kcq commented 2 years ago

Thanks for sharing the issue @quemeraisc ! what does your requirements.txt look like? Is this a server app? How did you try to minify your image? What flags did you pass to the docker-slim build command? What's the console output when you ran docker-slim build?

quemeraisc commented 2 years ago

hello @kcq. I used the basic command docker-slim build --http-probe=false --target enrichment/iosx. The requirement file is the python requirement for my app, as follows:

junos-eznc>=2.4
ipaddress==1.0.23
netmiko==2.4.2
cryptography==2.6.1

those are some networking module and the crypto module. This app is connecting to a bunch a network equipement collecting information. Here is the output of the build:

cmd=build info=exec message='changing continue-after from probe to nothing because http-probe is disabled' 
cmd=build info=exec message='changing continue-after to enter' 
cmd=build state=started
cmd=build info=params rt.as.user='true' keep.perms='true' tags='' target.type='image' target='enrichment/iosxr' continue.mode='enter' 
cmd=build state=image.inspection.start
cmd=build info=image size.bytes='982843958' size.human='983 MB' id='sha256:9520ca407acba605a56ab30c30188b6d6cfc71648315790c98019615ec949124' 
cmd=build info=image.stack index='0' name='python:3' id='sha256:4dd615d90c9da26a8137ab95596c80093e7a36e92f2303af96236a5503f26e42' 
cmd=build info=image.stack name='enrichment/iosxr:latest' id='sha256:9520ca407acba605a56ab30c30188b6d6cfc71648315790c98019615ec949124' index='1' 
cmd=build state=image.inspection.done
cmd=build state=container.inspection.start
cmd=build info=container name='dockerslimk_6763_20220422090316' id='76e424f1f087f18a3caf9256e0b7606449b0b128d7a92a52d00bda713a7def92' status='created' 
cmd=build info=container status='running' name='dockerslimk_6763_20220422090316' id='76e424f1f087f18a3caf9256e0b7606449b0b128d7a92a52d00bda713a7def92' 
cmd=build info=container message='obtained IP address' ip='172.17.0.2' 
cmd=build info=cmd.startmonitor status='sent' 
cmd=build info=event.startmonitor.done status='received' 
cmd=build info=container name='dockerslimk_6763_20220422090316' id='76e424f1f087f18a3caf9256e0b7606449b0b128d7a92a52d00bda713a7def92' target.port.list='' target.port.info='' message='YOU CAN USE THESE PORTS TO INTERACT WITH THE CONTAINER' 
cmd=build info=continue.after mode='enter' message='provide the expected input to allow the container inspector to continue its execution' 
cmd=build prompt='USER INPUT REQUIRED, PRESS <ENTER> WHEN YOU ARE DONE USING THE CONTAINER'

cmd=build state=container.inspection.finishing
cmd=build state=container.inspection.artifact.processing
cmd=build state=container.inspection.done
cmd=build state=building message="building optimized image" 
cmd=build state=completed
cmd=build info=results size.original='983 MB' size.optimized='67 MB' status='MINIFIED' by='14.67X' 
cmd=build info=results image.name='enrichment/iosxr.slim' image.size='67 MB' has.data='true' 
cmd=build info=results artifacts.location='/home/sysadmin/bin/.docker-slim-state/images/9520ca407acba605a56ab30c30188b6d6cfc71648315790c98019615ec949124/artifacts' 
cmd=build info=results artifacts.report='creport.json' 
cmd=build info=results artifacts.dockerfile.reversed='Dockerfile.fat' 
cmd=build info=results artifacts.dockerfile.optimized='Dockerfile' 
cmd=build info=results artifacts.seccomp='enrichment-iosxr-seccomp.json' 
cmd=build info=results artifacts.apparmor='enrichment-iosxr-apparmor-profile' 
cmd=build state=done
cmd=build info=commands message='use the xray command to learn more about the optimize image' 
cmd=build info=report file='slim.report.json' 
quemeraisc commented 2 years ago

Maybe this can help. I have just tried to use the alpine derivative of python's docker and got to an error message also concerning the cryptography module. There seems to be a missing compiler in that case. There might be something similar happening when using docker-slim. No error came up at build time though

reikolydia commented 2 years ago

i'm getting a similar issue as well for SQLAlchemy

File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/langhelpers.py", line 343, in load
    raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqlite

and tornado

Could not load pycurl: No module named 'tornado.curl_httpclient'