pubgate icon indicating copy to clipboard operation
pubgate copied to clipboard

Build static binaries possible with python?

Open pwFoo opened this issue 4 years ago • 1 comments

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.

  • https://stackoverflow.com/questions/39913847/is-there-a-way-to-compile-a-python-application-into-static-binary
  • https://github.com/mbachry/exxo (?)

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 avatar Jan 09 '21 12:01 pwFoo

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

pwFoo avatar Jan 09 '21 17:01 pwFoo