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

Update dockerfiles to do staged builds #19952

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b288a99
Upgrade most of the Dockerfiles to do staged builds
saiarcot895 Aug 19, 2024
d910457
Remove the Dockerfile.cleanup for docker-dash-engine
saiarcot895 Aug 19, 2024
5e61a87
Fix typos/duplicate lines in dockerfiles
saiarcot895 Aug 19, 2024
1cb7f15
Fix version control for dockers not working
saiarcot895 Aug 19, 2024
1e2a34b
Don't simulate removal, actually do it
saiarcot895 Aug 20, 2024
edf701d
Re-add purge command that was accidentally removed for orchagent
saiarcot895 Aug 20, 2024
e153fa4
Update gbsyncd Dockerfiles, and use macros for docker-syncd-brcm
saiarcot895 Aug 20, 2024
939af50
Update docker-dash-engine
saiarcot895 Aug 20, 2024
21c9591
Fix macro includes
saiarcot895 Aug 21, 2024
74cc236
Make sure environment variables are present in the second (final) stage
saiarcot895 Sep 1, 2024
ef23450
Fix the docker root cleanup and creation commands
saiarcot895 Sep 28, 2024
1e31a68
Set the nofile ulimit for the slave container to 1024:1048576
saiarcot895 Sep 28, 2024
7ea1005
Marvell: Update Dockerfiles for staged builds (#21)
krismarvell Oct 16, 2024
0b85785
Work around Docker overlayfs inconsistencies by using rsync
saiarcot895 Jan 9, 2025
0f81931
Fix docker builds
saiarcot895 Jan 10, 2025
6443892
Fix armhf and arm64 build
saiarcot895 Jan 10, 2025
1678fbd
Remove armhf and arm64 development packages as well
saiarcot895 Jan 10, 2025
2bdcd69
Fix base container for Broadcom syncd
saiarcot895 Jan 10, 2025
da5c8a8
Fix exclude argument to not exclude /usr/include/<triple>/sys
saiarcot895 Jan 10, 2025
07f8c02
Merge remote-tracking branch 'origin/master' into update-dockerfiles
saiarcot895 Jan 15, 2025
803314c
Remove documentation about SONIC_USE_DOCKER_BUILDKIT
saiarcot895 Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile.work
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ endif
DOCKER_LOCKFILE_SAVE := $(DOCKER_LOCKDIR)/docker_save.lock
$(shell mkdir -m 0777 -p $(DOCKER_LOCKDIR))
$(shell [ -f $(DOCKER_LOCKFILE_SAVE) ] || (touch $(DOCKER_LOCKFILE_SAVE) && chmod 0777 $(DOCKER_LOCKFILE_SAVE)))
$(shell [ -d $(DOCKER_ROOT) ] && docker run --rm -v $(DOCKER_ROOT)\:/mount debian sh -c 'rm -rf /mount/*')
$(mkdir -p $(DOCKER_ROOT))
$(shell [ -d $(DOCKER_ROOT) ] && docker run --rm -v $(DOCKER_ROOT)\:/mount $(DEFAULT_CONTAINER_REGISTRY)debian:bookworm sh -c 'rm -rf /mount/*')
$(shell mkdir -p $(DOCKER_ROOT))

ifeq ($(DOCKER_BUILDER_MOUNT),)
override DOCKER_BUILDER_MOUNT := "$(PWD):/sonic"
Expand All @@ -302,6 +302,8 @@ ifeq ($(DOCKER_BUILDER_WORKDIR),)
override DOCKER_BUILDER_WORKDIR := "/sonic"
endif

# Consider removing the --ulimit flag once nothing older
# than Bullseye is being used as a slave container.
DOCKER_RUN := docker run --rm=true --privileged --init \
-v $(DOCKER_BUILDER_MOUNT) \
-v "$(DOCKER_LOCKDIR):$(DOCKER_LOCKDIR)" \
Expand All @@ -310,6 +312,7 @@ DOCKER_RUN := docker run --rm=true --privileged --init \
-e "https_proxy=$(https_proxy)" \
-e "no_proxy=$(no_proxy)" \
-i$(shell { if [ -t 0 ]; then echo t; fi }) \
--ulimit nofile=524288:524288 \
$(SONIC_BUILDER_EXTRA_CMDLINE)

# Mount the $(DOCKER_ROOT) to /var/lib/docker in the slave container, the overlay fs is not supported as dockerd root folder.
Expand Down
33 changes: 10 additions & 23 deletions dockers/docker-base-bookworm/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
{% set prefix = DEFAULT_CONTAINER_REGISTRY %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% if CONFIGURED_ARCH == "armhf" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
FROM --platform=linux/arm/v7 {{ prefix }}debian:bookworm
ARG BASE=--platform=linux/arm/v7 {{ prefix }}debian:bookworm
{% elif CONFIGURED_ARCH == "arm64" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
FROM --platform=linux/arm64 {{ prefix }}debian:bookworm
ARG BASE=--platform=linux/arm64 {{ prefix }}debian:bookworm
{% else %}
FROM {{ prefix }}{{DOCKER_BASE_ARCH}}/debian:bookworm
ARG BASE={{ prefix }}{{DOCKER_BASE_ARCH}}/debian:bookworm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saiarcot895
You can use debian slim images to reduce final image size as it was suggested here: #19008.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I want to keep the focus of this PR on unblocking Docker upgrades, but I had to bring in some space optimization stuff (see the COPY at the end of this file) to get things to work.

{% endif %}

# Clean documentation in FROM image
RUN find /usr/share/doc -depth \( -type f -o -type l \) ! -name copyright | xargs rm || true

# Clean doc directories that are empty or only contain empty directories
RUN while [ -n "$(find /usr/share/doc -depth -type d -empty -print -exec rmdir {} +)" ]; do :; done && \
rm -rf \
/usr/share/man/* \
/usr/share/groff/* \
/usr/share/info/* \
/usr/share/lintian/* \
/usr/share/linda/* \
/var/cache/man/* \
/usr/share/locale/*
FROM $BASE AS base

# Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive
Expand All @@ -47,6 +35,8 @@ RUN apt update && \
python-is-python3 \
vim-tiny \
rsyslog \
# Install rsync for copying over only changes between layers
rsync \
# Install redis-tools
redis-tools \
# common dependencies
Expand Down Expand Up @@ -98,17 +88,14 @@ RUN apt-get -y purge \
{{ install_debian_packages(docker_base_bookworm_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

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

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

COPY ["etc/supervisor/supervisord.conf", "/etc/supervisor/"]

FROM scratch

COPY --from=base / /
33 changes: 10 additions & 23 deletions dockers/docker-base-bullseye/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
{% set prefix = DEFAULT_CONTAINER_REGISTRY %}
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
{% if CONFIGURED_ARCH == "armhf" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
FROM {{ prefix }}multiarch/debian-debootstrap:armhf-bullseye
ARG BASE={{ prefix }}multiarch/debian-debootstrap:armhf-bullseye
{% elif CONFIGURED_ARCH == "arm64" and (MULTIARCH_QEMU_ENVIRON == "y" or CROSS_BUILD_ENVIRON == "y") %}
FROM {{ prefix }}multiarch/debian-debootstrap:arm64-bullseye
ARG BASE={{ prefix }}multiarch/debian-debootstrap:arm64-bullseye
{% else %}
FROM {{ prefix }}{{DOCKER_BASE_ARCH}}/debian:bullseye
ARG BASE={{ prefix }}{{DOCKER_BASE_ARCH}}/debian:bullseye
{% endif %}

# Clean documentation in FROM image
RUN find /usr/share/doc -depth \( -type f -o -type l \) ! -name copyright | xargs rm || true

# Clean doc directories that are empty or only contain empty directories
RUN while [ -n "$(find /usr/share/doc -depth -type d -empty -print -exec rmdir {} +)" ]; do :; done && \
rm -rf \
/usr/share/man/* \
/usr/share/groff/* \
/usr/share/info/* \
/usr/share/lintian/* \
/usr/share/linda/* \
/var/cache/man/* \
/usr/share/locale/*
FROM $BASE AS base

# Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive
Expand All @@ -45,6 +33,8 @@ RUN apt-get update && \
python3-pip \
python-is-python3 \
vim-tiny \
# Install rsync for copying over only changes between layers
rsync \
# Install redis-tools
redis-tools \
# common dependencies
Expand Down Expand Up @@ -100,17 +90,14 @@ 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

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

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

COPY ["etc/supervisor/supervisord.conf", "/etc/supervisor/"]

FROM scratch

COPY --from=base / /
25 changes: 17 additions & 8 deletions dockers/docker-config-engine-bookworm/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-base-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}
ARG BASE=docker-base-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE as base

## Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive
Expand Down Expand Up @@ -50,10 +52,17 @@ COPY ["files/container_startup.py", "/usr/share/sonic/scripts/"]
COPY ["00-load-omprog.conf", "/etc/rsyslog.d/"]

## 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

{%- if CONFIGURED_ARCH == "armhf" or CONFIGURED_ARCH == "arm64" %}
RUN apt-get purge -y \
libxslt-dev \
libz-dev
{%- endif %}

RUN apt-get purge -y \
python3-dev \
build-essential

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=resolv.conf /changes-to-image/ /
8 changes: 7 additions & 1 deletion dockers/docker-config-engine-bullseye/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-base-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}
ARG BASE=docker-base-bullseye-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE as base

## Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive
Expand Down Expand Up @@ -60,3 +62,7 @@ RUN apt-get purge -y \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs /python-wheels ~/.cache

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=resolv.conf /changes-to-image/ /
17 changes: 10 additions & 7 deletions dockers/docker-database/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}
ARG BASE=docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE as base

ARG docker_container_name

Expand All @@ -23,12 +25,8 @@ RUN pip3 install click
{{ 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 ""$/save ""/g; \
# Configure redis settings
RUN sed -ri 's/^# save ""$/save ""/g; \
s/^daemonize yes$/daemonize no/; \
s/^logfile .*$/logfile ""/; \
s/^# syslog-enabled no$/syslog-enabled no/; \
Expand All @@ -50,4 +48,9 @@ COPY ["files/update_chassisdb_config", "/usr/local/bin/"]
COPY ["flush_unused_database", "/usr/local/bin/"]
COPY ["multi_database_config.json.j2", "/usr/share/sonic/templates/"]

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=resolv.conf /changes-to-image/ /

ENV DEBIAN_FRONTEND=noninteractive
ENTRYPOINT ["/usr/local/bin/docker-database-init.sh"]
19 changes: 11 additions & 8 deletions dockers/docker-dhcp-relay/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}
ARG BASE=docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE as base

ARG docker_container_name
ARG image_version

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

# Pass the image_version to container
ENV IMAGE_VERSION=$image_version

# Update apt's cache of available packages
RUN apt-get update

Expand Down Expand Up @@ -38,10 +37,6 @@ RUN pip3 install psutil
# Clean up
RUN apt-get remove -y build-essential \
python3-dev
RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs

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 All @@ -60,4 +55,12 @@ RUN rm -f /etc/rsyslog.d/rsyslog_plugin.conf.j2
RUN rm -f /etc/rsyslog.d/events_info.json
{% endif %}

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=resolv.conf /changes-to-image/ /

# Pass the image_version to container
ENV IMAGE_VERSION=$image_version

ENV DEBIAN_FRONTEND=noninteractive
ENTRYPOINT ["/usr/bin/docker_init.sh"]
20 changes: 11 additions & 9 deletions dockers/docker-dhcp-server/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}
ARG BASE=docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE as base

ARG docker_container_name
ARG image_version

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

# Pass the image_version to container
ENV IMAGE_VERSION=$image_version

RUN apt-get update && \
apt-get install -f -y \
tcpdump \
Expand Down Expand Up @@ -41,11 +40,6 @@ RUN pip3 install psutil
RUN apt-get remove -y build-essential \
python3-dev

RUN apt-get clean -y && \
apt-get autoclean -y && \
apt-get autoremove -y && \
rm -rf /debs

COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
Expand All @@ -55,4 +49,12 @@ COPY ["lease_update.sh", "/etc/kea/"]
COPY ["kea-dhcp4-init.conf", "/etc/kea/kea-dhcp4.conf"]
COPY ["cli", "/cli/"]

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=resolv.conf /changes-to-image/ /

# Pass the image_version to container
ENV IMAGE_VERSION=$image_version

ENV DEBIAN_FRONTEND=noninteractive
ENTRYPOINT ["/usr/bin/docker_init.sh"]
22 changes: 11 additions & 11 deletions dockers/docker-eventd/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}
ARG BASE=docker-config-engine-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE as base

ARG docker_container_name
ARG image_version
RUN [ -f /etc/rsyslog.conf ] && sed -ri "s/%syslogtag%/$docker_container_name#%syslogtag%/;" /etc/rsyslog.conf

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

# Pass the image_version to container
ENV IMAGE_VERSION=$image_version

# Update apt's cache of available packages
RUN apt-get update

Expand All @@ -22,15 +20,17 @@ 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

COPY ["start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["critical_processes", "/etc/supervisor"]

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=resolv.conf /changes-to-image/ /

# Pass the image_version to container
ENV IMAGE_VERSION=$image_version

ENV DEBIAN_FRONTEND=noninteractive
ENTRYPOINT ["/usr/local/bin/supervisord"]
Loading
Loading