autogestion/pubgate

Build static binaries possible with python?

pwFoo opened this issue · 1 comments

pwFoo commented

Hi,

I build a docker image with python / pubgate and I would like to reduce it. Found some information about generate binaries of python scripts.

Would it possible to reduce the docker image size and complexitiy?

REPOSITORY                TAG        IMAGE ID       CREATED             SIZE
pubgate                   latest     8123954eff02   9 minutes ago       428MB
pwFoo commented

Tried pyinstaller to generate binary: https://github.com/six8/pyinstaller-alpine

Start docker container with a shell

docker run --rm -ti -v "${PWD}:/src"   --entrypoint sh  six8/pyinstaller-alpine 

Install needed make and "hidden imports"

apk add make
pip install pycryptodome sanic sanic_openapi sanic_motor sanic_cors aiohttp msgpack aiocache simple_bcrypt

Build binary

pyinstaller --noconfirm     --onefile      --clean run_api.py --hidden-import=aiohttp --hidden-import=sanic --hidden-import=sanic_openapi --hidden-import=sanic_motor --hidden-import=sanic_cors --hidden-import=simple_bcrypt --hidden-import=aiocache --hidden-import=msgpack --hidden-import=Crypto.Math --name pubgate

Check binary

# ls -lh dist/pubgate
-rwxr-xr-x    1 root     root       11.4M Jan  9 17:38 dist/pubgate

Try to start binary (needs ./config/conf.cfg)

# ./dist/pubgate
Traceback (most recent call last):
  File "run_api.py", line 7, in <module>
    app = create_app('config/conf.cfg')
  File "pubgate/app.py", line 20, in create_app
    app = Sanic(error_handler=PGErrorHandler())
  File "site-packages/sanic/app.py", line 59, in __init__
sanic.exceptions.SanicException: Sanic instance cannot be unnamed. Please use Sanic(name='your_application_name') instead.
[32220] Failed to execute script run_api
/src # ./dist/pubgate
Traceback (most recent call last):
  File "run_api.py", line 7, in <module>
    app = create_app('config/conf.cfg')
  File "pubgate/app.py", line 20, in create_app
    app = Sanic(error_handler=PGErrorHandler())
  File "site-packages/sanic/app.py", line 59, in __init__
sanic.exceptions.SanicException: Sanic instance cannot be unnamed. Please use Sanic(name='your_application_name') instead.
[32238] Failed to execute script run_api