Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size of docker containers #14214

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 3 additions & 8 deletions dockers/docker-base-bullseye/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set prefix = DEFAULT_CONTAINER_REGISTRY %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
{% if CONFIGURED_ARCH == "armhf" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
FROM {{ prefix }}multiarch/debian-debootstrap:armhf-bullseye
{% elif CONFIGURED_ARCH == "arm64" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
Expand Down Expand Up @@ -97,17 +97,12 @@ RUN apt-get -y purge \
{{ install_debian_packages(docker_base_bullseye_debs.split(' ')) }}
{%- endif %}

# Clean up apt
# Remove /var/lib/apt/lists/*, could be obsoleted for derived images
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/* /tmp/* ~/.cache
{{ cleanup_apt_and_python_cache() }}

COPY ["etc/rsyslog.d/*", "/etc/rsyslog.d/"]
COPY ["root/.vimrc", "/root/.vimrc"]

RUN ln /usr/bin/vim.tiny /usr/bin/vim
RUN ln -s /usr/bin/vim.tiny /usr/bin/vim

COPY ["etc/supervisor/supervisord.conf", "/etc/supervisor/"]
COPY ["etc/supervisor/containercfgd.conf", "/etc/supervisor/conf.d/"]
11 changes: 3 additions & 8 deletions dockers/docker-base-buster/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set prefix = DEFAULT_CONTAINER_REGISTRY %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
{% if CONFIGURED_ARCH == "armhf" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
FROM {{ prefix }}multiarch/debian-debootstrap:armhf-buster
{% elif CONFIGURED_ARCH == "arm64" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
Expand Down Expand Up @@ -110,17 +110,12 @@ RUN apt-get -y purge \
{{ install_debian_packages(docker_base_buster_debs.split(' ')) }}
{%- endif %}

# Clean up apt
# Remove /var/lib/apt/lists/*, could be obsoleted for derived images
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/* /tmp/* ~/.cache/
{{ cleanup_apt_and_python_cache() }}

COPY ["etc/rsyslog.d/*", "/etc/rsyslog.d/"]
COPY ["root/.vimrc", "/root/.vimrc"]

RUN ln /usr/bin/vim.tiny /usr/bin/vim
RUN ln -s /usr/bin/vim.tiny /usr/bin/vim

COPY ["etc/supervisor/supervisord.conf", "/etc/supervisor/"]
COPY ["etc/supervisor/containercfgd.conf", "/etc/supervisor/conf.d/"]
10 changes: 4 additions & 6 deletions dockers/docker-config-engine-bullseye/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-base-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

## Make apt-get non-interactive
Expand Down Expand Up @@ -45,8 +45,6 @@ COPY ["files/swss_vars.j2", "/usr/share/sonic/templates/"]
## Clean up
RUN apt-get purge -y \
python3-dev \
build-essential && \
apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs /python-wheels ~/.cache
build-essential

{{ cleanup_apt_and_python_cache() }}
10 changes: 4 additions & 6 deletions dockers/docker-config-engine-buster/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-base-buster-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

## Make apt-get non-interactive
Expand Down Expand Up @@ -45,8 +45,6 @@ COPY ["files/swss_vars.j2", "/usr/share/sonic/templates/"]
## Clean up
RUN apt-get purge -y \
python3-dev \
build-essential && \
apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs /python-wheels ~/.cache
build-essential

{{ cleanup_apt_and_python_cache() }}
12 changes: 5 additions & 7 deletions dockers/docker-database/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -20,12 +20,10 @@ RUN apt-get install -y redis-tools redis-server
{{ install_debian_packages(docker_database_debs.split(' ')) }}
{%- endif %}

# Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs ~/.cache && \
sed -ri 's/^(save .*$)/# \1/g; \
{{ cleanup_apt_and_python_cache() }}

# Configure redis
RUN sed -ri 's/^(save .*$)/# \1/g; \
s/^daemonize yes$/daemonize no/; \
s/^logfile .*$/logfile ""/; \
s/^# syslog-enabled no$/syslog-enabled no/; \
Expand Down
8 changes: 2 additions & 6 deletions dockers/docker-dhcp-relay/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -21,11 +21,7 @@ RUN apt-get update
{{ install_debian_packages(docker_dhcp_relay_debs.split(' ')) }}
{%- endif %}

# Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
COPY ["docker-dhcp-relay.supervisord.conf.j2", "port-name-alias-map.txt.j2", "wait_for_intf.sh.j2", "/usr/share/sonic/templates/"]
Expand Down
8 changes: 2 additions & 6 deletions dockers/docker-eventd/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -22,11 +22,7 @@ RUN apt-get update
{{ install_debian_packages(docker_eventd_debs.split(' ')) }}
{%- endif %}

# Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

COPY ["start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
Expand Down
8 changes: 2 additions & 6 deletions dockers/docker-fpm-frr/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-swss-layer-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand Down Expand Up @@ -39,11 +39,7 @@ RUN useradd -u ${frr_user_uid} -g ${frr_user_gid} -M -s /bin/false frr

RUN chown -R ${frr_user_uid}:${frr_user_gid} /etc/frr/

# Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs ~/.cache /python-wheels
{{ cleanup_apt_and_python_cache() }}

COPY ["frr", "/usr/share/sonic/templates"]
COPY ["docker_init.sh", "/usr/bin/"]
Expand Down
10 changes: 2 additions & 8 deletions dockers/docker-lldp/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand Down Expand Up @@ -29,13 +29,7 @@ RUN apt-get update
{{ install_python_wheels(docker_lldp_whls.split(' ')) }}
{% endif %}

# Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs \
/python-wheels \
~/.cache
{{ cleanup_apt_and_python_cache() }}

COPY ["docker-lldp-init.sh", "/usr/bin/"]
COPY ["start.sh", "/usr/bin/"]
Expand Down
7 changes: 2 additions & 5 deletions dockers/docker-macsec/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -16,10 +16,7 @@ RUN apt-get update
{{ install_debian_packages(docker_macsec_debs.split(' ')) }}
{%- endif %}

RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

COPY ["start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
Expand Down
8 changes: 2 additions & 6 deletions dockers/docker-mux/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -18,11 +18,7 @@ RUN apt-get update && \
{{ install_debian_packages(docker_mux_debs.split(' ')) }}
{%- endif %}

## Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

COPY ["docker-init.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
Expand Down
7 changes: 2 additions & 5 deletions dockers/docker-nat/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-swss-layer-buster-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name

RUN echo

## Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive

Expand All @@ -31,7 +29,6 @@ COPY ["restore_nat_entries.py", "/usr/bin/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["critical_processes", "/etc/supervisor"]

RUN apt-get clean -y; apt-get autoclean -y; apt-get autoremove -y
RUN rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

ENTRYPOINT ["/usr/local/bin/supervisord"]
10 changes: 4 additions & 6 deletions dockers/docker-orchagent/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-swss-layer-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand Down Expand Up @@ -59,11 +59,9 @@ RUN apt-get remove -y gcc
# Clean up
RUN apt-get purge -y \
build-essential \
python3-dev && \
apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs ~/.cache
python3-dev

{{ cleanup_apt_and_python_cache() }}

COPY ["files/arp_update", "/usr/bin"]
COPY ["arp_update.conf", "files/arp_update_vars.j2", "/usr/share/sonic/templates/"]
Expand Down
12 changes: 4 additions & 8 deletions dockers/docker-platform-monitor/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand Down Expand Up @@ -75,13 +75,9 @@ RUN pip3 install libpci
# Clean up
RUN apt-get purge -y \
build-essential \
python3-dev && \
apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs \
/python-wheels \
~/.cache
python3-dev

{{ cleanup_apt_and_python_cache() }}

COPY ["lm-sensors.sh", "/usr/bin/"]
COPY ["docker-pmon.supervisord.conf.j2", "docker_init.j2", "/usr/share/sonic/templates/"]
Expand Down
8 changes: 2 additions & 6 deletions dockers/docker-router-advertiser/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -25,11 +25,7 @@ RUN apt-get -y install radvd
{{ install_debian_packages(docker_router_advertiser_debs.split(' ')) }}
{%- endif %}

# Clean up
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

COPY ["start.sh", "/usr/bin/"]
COPY ["docker-init.sh", "/usr/bin/"]
Expand Down
7 changes: 2 additions & 5 deletions dockers/docker-sflow/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-swss-layer-buster-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -19,10 +19,7 @@ RUN apt-get update && \
{{ install_debian_packages(docker_sflow_debs.split(' ')) }}
{%- endif %}

RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs
{{ cleanup_apt_and_python_cache() }}

RUN sed -ri '/^DAEMON_ARGS=""/c DAEMON_ARGS="-c /var/log/hsflowd.crash"' /etc/init.d/hsflowd

Expand Down
11 changes: 4 additions & 7 deletions dockers/docker-snmp/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python3_wheels, copy_files %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python3_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand Down Expand Up @@ -59,12 +59,9 @@ RUN python3 -m sonic_ax_impl install
RUN apt-get -y purge \
python3-dev \
gcc \
make && \
apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y --purge && \
find / | grep -E "__pycache__" | xargs rm -rf && \
rm -rf /debs /python-wheels ~/.cache
make

{{ cleanup_apt_and_python_cache() }}

COPY ["start.sh", "/usr/bin/"]
COPY ["snmp_yml_to_configdb.py", "/usr/bin/"]
Expand Down
17 changes: 6 additions & 11 deletions dockers/docker-sonic-mgmt-framework/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files, cleanup_apt_and_python_cache %}
FROM docker-config-engine-buster-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

ARG docker_container_name
Expand All @@ -16,23 +17,17 @@ RUN pip3 install connexion==2.7.0 \
six==1.11.0 \
urllib3==1.26.5

COPY \
{% for deb in docker_sonic_mgmt_framework_debs.split(' ') -%}
debs/{{ deb }}{{' '}}
{%- endfor -%}
debs/
# Copy locally-built Debian package dependencies
{{ copy_files("debs/", docker_sonic_mgmt_framework_debs.split(' '), "/debs/") }}

RUN dpkg -i \
{% for deb in docker_sonic_mgmt_framework_debs.split(' ') -%}
debs/{{ deb }}{{' '}}
{%- endfor %}
# Install locally-built Debian packages and implicitly install their dependencies
{{ install_debian_packages(docker_sonic_mgmt_framework_debs.split(' ')) }}

COPY ["start.sh", "rest-server.sh", "/usr/bin/"]
COPY ["mgmt_vars.j2", "/usr/share/sonic/templates/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]

RUN apt-get remove -y g++ python3-dev
RUN apt-get clean -y; apt-get autoclean -y; apt-get autoremove -y
RUN rm -rf /debs ~/.cache
{{ cleanup_apt_and_python_cache() }}

ENTRYPOINT ["/usr/local/bin/supervisord"]