sonic-buildimage/dockers/dockerfile-macros.j2
Saikrishna Arcot 932d0f5391
[202205] Remove apt package lists and make macro to clean up apt and python cache (#14377)
* Remove apt package lists and make macro to clean up apt and python cache

Remove the apt package lists (`/var/lib/apt/lists`) from the docker
containers. This saves about 100MB.

Also, make a macro to clean up the apt and python cache that can then be
used in all of the containers. This helps make the cleanup be consistent
across all containers.

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
2023-03-22 14:51:25 -07:00

47 lines
1.4 KiB
Django/Jinja

{% macro install_debian_packages(packages) -%}
RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return 1; }; \
{%- for deb in packages %}
dpkg_apt /debs/{{ deb }} {%- if not loop.last %} && \ {%- endif %}
{%- endfor %}
{%- endmacro %}
{% macro install_python2_wheels(packages) -%}
RUN cd /python-wheels/ && pip2 install {{ packages | join(' ') }}
{%- endmacro %}
{% macro install_python3_wheels(packages) -%}
RUN cd /python-wheels/ && pip3 install {{ packages | join(' ') }}
{%- endmacro %}
{% macro install_python_wheels(packages) -%}
{%- set py2_pkgs, py3_pkgs = [], [] %}
{%- for pkg in packages %}
{%- if 'py3' in pkg %}
{{- py3_pkgs.append(pkg) or '' }}
{%- else %}
{{- py2_pkgs.append(pkg) or '' }}
{%- endif %}
{%- endfor %}
{%- if py3_pkgs | length %}
{{ install_python3_wheels(py3_pkgs) }}
{%- endif %}
{%- if py2_pkgs | length %}
{{ install_python2_wheels(py2_pkgs) }}
{%- endif %}
{%- endmacro %}
{% macro copy_files(prefix, files, dest) -%}
COPY \
{%- for file in files %}
{{ prefix }}/{{ file }} \
{%- endfor %}
{{ dest }}
{%- endmacro %}
{% macro cleanup_apt_and_python_cache() -%}
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs /python-wheels /tmp/* ~/.cache /var/lib/apt/lists/*
{%- endmacro %}