amazon-riverbgc / trocas-herokuapp1

TROCAS Panel App
https://trocasdata-1.herokuapp.com/
MIT License
1 stars 0 forks source link

Slimmer docker / heroku slug size #2

Open emiliom opened 4 years ago

emiliom commented 4 years ago

@ocefpaf Thanks for your suggestions for trimming the size of the docker container / heroku slug! I've tried your suggestions, but first I'll paste them from your email to here (with minor edits) so I can refer back to them easily:

The strategies are:

  • reduce the number of layers by chaining commands instead of new docker lines;
  • install things in the base env;
  • clean up conda pkg dir cache;
  • clean up unused files like static libraries, etc.

Here is a slimmer Dockerfile base on yours. I did not test it though:

# try this image, it should be small than continuumio/miniconda3
FROM jcrist/alpine-conda:4.6.8  

COPY environment.yml environment.yml

RUN /opt/conda/bin/conda env update --name base --file environment.yml \
&& rm environment.yml \
&& conda clean --all --force-pkgs-dirs --yes \
&& find /opt/conda/ -follow -type f -name '*.a' -delete \
&& find /opt/conda/ -follow -type f -name '*.pyc' -delete \
&& find /opt/conda/ -follow -type f -name '*.js.map' -delete \
&& find /opt/conda/lib/python*/site-packages/bokeh/server/static -follow -type f -name '*.js' ! -name '*.min.js' -delete

ADD . /opt/apps
WORKDIR /opt/apps

RUN adduser --disabled-login myuser
USER myuser

ENTRYPOINT [ "/bin/bash", "-c" ]
CMD panel serve --port=${PORT} --address=0.0.0.0 trocas_pannelapp_1.ipynb --allow-websocket-origin=trocasdata-1.herokuapp.com
emiliom commented 4 years ago

I tried the original suggestions (with small corrections from the email wrap-arounds, etc), plus removing the last two find ... -delete statements. But I kept running into a version of these errors:

ERROR conda.core.link:_execute_post_link_actions(658): An error occurred while installing package 'conda-forge::widgetsnbextension-3.5.1-py37_0'.
FileNotFoundError(2, "No such file or directory: 'bash'")
Attempting to roll back.

failed
ERROR conda.core.link:_execute(568): An error occurred while installing package 'conda-forge::widgetsnbextension-3.5.1-py37_0'.
FileNotFoundError(2, "No such file or directory: 'bash'")
Attempting to roll back.

Rolling back transaction: ...working... done

[Errno 2] No such file or directory: 'bash': 'bash'

The command '/bin/sh -c /opt/conda/bin/conda env update --name base --file environment.yml && rm environment.yml && conda clean --all --force-pkgs-dirs --yes && find /opt/conda/ -follow -type f -name '*.a' -delete && find /opt/conda/ -follow -type f -name '*.pyc' -delete' returned a non-zero code: 1

The fact that the miniconda installation was fairly old and there was an error about bash not being found led me to keep all your steps in place except reverting to using the continuumio/miniconda3 image. That did it!

Now the image size is 2.4 GB instead of the original 4.1 GB -- a big trim. I suspect the main savings come from building on top of the base env and cleaning up the conda pkg cache dir.

I can live with this. It's a major improvement already. Thanks so much!! Hopefully my tests and experience will be of use to you too. FYI, I'm pushing to heroku right now, and will get around to pushing to this repo later on.

ocefpaf commented 4 years ago

plus removing the last two find ... -delete statements

Those can save a lot of space too depending on your dependencies. We are already removing most of this files in the packages already though so, as time passes, those lines will not be needed.

Now the image size is 2.4 GB instead of the original 4.1 GB -- a big trim.

:open_mouth:

I suspect the main savings come from building on top of the base env and cleaning up the conda pkg cache dir.

Yep.

FYI, I'm pushing to heroku right now, and will get around to pushing to this repo later on.

Glad it helped. Hopefully this will make your deployment more stable.