axiros / terminal_markdown_viewer

Styled Terminal Markdown Viewer
Other
1.8k stars 105 forks source link

Compile MDV as a binary using pyinstaller (in Docker) #80

Open pascalandy opened 4 years ago

pascalandy commented 4 years ago

This one is for the folks who love Docker and love to optimize their docker images.

1) Works but it's heavy

It's easy to run mdv in Docker. Size: 123MO (compressed)

Dockerfile

FROM python:3.8.1-alpine3.11 as binary
RUN pip install mdv
ENTRYPOINT [ "mdv" ]

docker image ls

devmtl/mdv                                                      edge_2020-02-01_19H25   049c6215fcc1        About an hour ago   123MB

2) Optimizing

I'd like to compile MDV as a binary using pyinstaller. This will make the final image much smaller using a multi-stage build.

This is what I got so far

Dockerfile

FROM python:2.7.17-alpine3.11 as core

RUN set -eux && apk update && apk add --no-cache \
  'su-exec>=0.2' git nano bash upx curl wget && \
  \
### gcc / c compiler required by cython
  apk update && apk add --no-cache \
  build-base && \
  \
### pyinstaller will make mdv compile as a binary
  pip install pyinstaller && \
  \
### dep for mdv
  pip install markdown && \
  pip install pygments && \
  pip install pyyaml && \
  pip install docopt && \
  pip install tabulate && \
  \
### install from source
  git clone https://github.com/axiros/terminal_markdown_viewer.git --depth 1 && \
  cd terminal_markdown_viewer                                       && \
  pyinstaller -F setup.py

Everything seems to compile as expected

20 INFO: PyInstaller: 3.6
21 INFO: Python: 2.7.17
23 INFO: Platform: Linux-4.19.76-linuxkit-x86_64-with
23 INFO: wrote /terminal_markdown_viewer/setup.spec
27 INFO: UPX is available.
29 INFO: Extending PYTHONPATH with paths
['/terminal_markdown_viewer', '/terminal_markdown_viewer']
29 INFO: checking Analysis
29 INFO: Building Analysis because Analysis-00.toc is non existent
29 INFO: Initializing module dependency graph...
30 INFO: Caching module graph hooks...
34 INFO: Caching module dependency graph...
47 INFO: running Analysis Analysis-00.toc
54 INFO: Analyzing /terminal_markdown_viewer/setup.py
1092 INFO: Processing pre-find module path hook   distutils
1092 INFO: distutils: retargeting to non-venv dir '/usr/local/lib/python2.7'
2013 INFO: Processing pre-safe import module hook   _xmlplus
2106 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
2419 INFO: Processing pre-find module path hook   site
2419 INFO: site: retargeting to fake-dir '/usr/local/lib/python2.7/site-packages/PyInstaller/fake-modules'
2637 INFO: Processing module hooks...
2638 INFO: Loading module hook "hook-distutils.py"...
2639 INFO: Loading module hook "hook-sysconfig.py"...
2640 INFO: Loading module hook "hook-xml.py"...
2692 INFO: Loading module hook "hook-pkg_resources.py"...
2845 INFO: Processing pre-safe import module hook   win32com
3097 INFO: Excluding import '__main__'
3099 INFO:   Removing import of __main__ from module pkg_resources
3100 INFO: Loading module hook "hook-httplib.py"...
3100 INFO: Loading module hook "hook-setuptools.py"...
3636 INFO: Loading module hook "hook-encodings.py"...
4022 INFO: Looking for ctypes DLLs
Illegal option -p
4091 INFO: Analyzing run-time hooks ...
4095 INFO: Including run-time hook 'pyi_rth_pkgres.py'
4102 INFO: Looking for dynamic libraries
4253 INFO: Looking for eggs
4253 INFO: Using Python library /usr/local/lib/libpython2.7.so.1.0
4259 INFO: Warnings written to /terminal_markdown_viewer/build/setup/warn-setup.txt
4287 INFO: Graph cross-reference written to /terminal_markdown_viewer/build/setup/xref-setup.html
4344 INFO: checking PYZ
4344 INFO: Building PYZ because PYZ-00.toc is non existent
4344 INFO: Building PYZ (ZlibArchive) /terminal_markdown_viewer/build/setup/PYZ-00.pyz
4651 INFO: Building PYZ (ZlibArchive) /terminal_markdown_viewer/build/setup/PYZ-00.pyz completed successfully.
4699 INFO: checking PKG
4700 INFO: Building PKG because PKG-00.toc is non existent
4700 INFO: Building PKG (CArchive) PKG-00.pkg
9633 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
9646 INFO: Bootloader /usr/local/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
9647 INFO: checking EXE
9647 INFO: Building EXE because EXE-00.toc is non existent
9647 INFO: Building EXE from EXE-00.toc
9647 INFO: Appending archive to ELF section in EXE /terminal_markdown_viewer/dist/setup
9674 INFO: Building EXE from EXE-00.toc completed successfully.

cd dist && \
chmod +x setup

Let's run it:

/terminal_markdown_viewer/dist # echo; pwd; echo; ls -AlhF; echo; du -sh *; echo; du -sh;
/terminal_markdown_viewer/dist
total 10M
-rwxr-xr-x    1 root     root        9.5M Feb  2 01:58 setup*
9.5M    setup

/terminal_markdown_viewer/dist # setup
sh: setup: not found
/terminal_markdown_viewer/dist # ./setup
sh: ./setup: not found

Any ideas?