Skip to content

Commit 81cf332

Browse files
stepanblyschaklguohan
authored andcommitted
[build]: Improve dockerfile instructions (#3048)
- create a dockerfile-marcros.j2 file with all common operations written as j2 macro - use single dockerfile instruction for COPY and RUN commands when possible to improve build time - reorganize dockerfile instructions to make more cache friendly (in case someday we will remove --no-cache to build docker images) Signed-off-by: Stepan Blyschak <[email protected]>
1 parent 817c637 commit 81cf332

File tree

15 files changed

+309
-291
lines changed

15 files changed

+309
-291
lines changed
Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
12
FROM debian:stretch
23

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

67
# Clean doc directories that are empty or only contain empty directories
7-
RUN while [ -n "$(find /usr/share/doc -depth -type d -empty -print -exec rmdir {} +)" ]; do :; done
8-
RUN rm -rf \
8+
RUN while [ -n "$(find /usr/share/doc -depth -type d -empty -print -exec rmdir {} +)" ]; do :; done && \
9+
rm -rf \
910
/usr/share/man/* \
1011
/usr/share/groff/* \
1112
/usr/share/info/* \
@@ -21,28 +22,22 @@ ENV DEBIAN_FRONTEND=noninteractive
2122
COPY ["dpkg_01_drop", "/etc/dpkg/dpkg.cfg.d/01_drop"]
2223
COPY ["sources.list", "/etc/apt/sources.list"]
2324
COPY ["no_install_recommend_suggest", "/etc/apt/apt.conf.d"]
24-
RUN apt-get update
25-
26-
# Pre-install fundamental packages
27-
RUN apt-get -y install \
28-
less \
29-
perl \
30-
procps \
31-
python \
32-
rsyslog \
33-
vim-tiny
34-
35-
COPY ["etc/rsyslog.conf", "/etc/rsyslog.conf"]
36-
COPY ["etc/rsyslog.d/*", "/etc/rsyslog.d/"]
37-
COPY ["root/.vimrc", "/root/.vimrc"]
3825

26+
# Update apt cache and
27+
# pre-install fundamental packages
28+
RUN apt-get update && \
29+
apt-get -y install \
30+
less \
31+
perl \
32+
procps \
33+
python \
34+
rsyslog \
35+
vim-tiny \
3936
# Install dependencies of supervisor
40-
RUN apt-get -y install python-pkg-resources python-meld3
41-
42-
RUN mkdir -p /etc/supervisor
43-
RUN mkdir -p /var/log/supervisor
37+
python-pkg-resources \
38+
python-meld3
4439

45-
COPY ["etc/supervisor/supervisord.conf", "/etc/supervisor/"]
40+
RUN mkdir -p /etc/supervisor /var/log/supervisor
4641

4742
RUN apt-get -y purge \
4843
exim4 \
@@ -51,29 +46,29 @@ RUN apt-get -y purge \
5146
exim4-daemon-light
5247

5348
{% if docker_base_stretch_debs.strip() -%}
54-
# Copy built Debian packages
55-
{%- for deb in docker_base_stretch_debs.split(' ') %}
56-
COPY debs/{{ deb }} debs/
57-
{%- endfor %}
49+
# Copy locally-built Debian package dependencies
50+
{{ copy_files("debs/", docker_base_stretch_debs.split(' '), "/debs/") }}
5851

5952
# Install built Debian packages and implicitly install their dependencies
60-
{%- for deb in docker_base_stretch_debs.split(' ') %}
61-
RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return 1; }; dpkg_apt debs/{{ deb }}
62-
{%- endfor %}
53+
{{ install_debian_packages(docker_base_stretch_debs.split(' ')) }}
6354
{%- endif %}
6455

6556
{% if docker_base_stretch_dbgs.strip() -%}
6657
# Install common debug-packages
67-
{%- for dbg_pkg in docker_base_stretch_dbgs.split(' ') %}
68-
RUN apt-get -y install {{ dbg_pkg }}
69-
{%- endfor %}
58+
RUN apt-get -y install docker_base_stretch_dbgs.split(' ') | join(' ')
7059
{% else %}
7160
RUN ln /usr/bin/vim.tiny /usr/bin/vim
7261
{%- endif %}
7362

7463
# Clean up apt
7564
# Remove /var/lib/apt/lists/*, could be obsoleted for derived images
76-
RUN apt-get clean -y; apt-get autoclean -y; apt-get autoremove -y
77-
RUN rm -rf /var/lib/apt/lists/*
65+
RUN apt-get clean -y && \
66+
apt-get autoclean -y && \
67+
apt-get autoremove -y && \
68+
rm -rf /var/lib/apt/lists/* /tmp/*
69+
70+
COPY ["etc/rsyslog.conf", "/etc/rsyslog.conf"]
71+
COPY ["etc/rsyslog.d/*", "/etc/rsyslog.d/"]
72+
COPY ["root/.vimrc", "/root/.vimrc"]
7873

79-
RUN rm -rf /tmp/*
74+
COPY ["etc/supervisor/supervisord.conf", "/etc/supervisor/"]
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1+
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
12
FROM docker-base-stretch
23

34
## Make apt-get non-interactive
45
ENV DEBIAN_FRONTEND=noninteractive
56

6-
RUN apt-get update
7-
8-
# Dependencies for sonic-cfggen
9-
RUN apt-get install -y python-lxml python-yaml python-bitarray python-pip python-dev python-natsort python-setuptools
7+
RUN apt-get update && \
8+
apt-get install -y \
9+
# Dependencies for sonic-cfggen
10+
python-lxml \
11+
python-yaml \
12+
python-bitarray \
13+
python-pip \
14+
python-dev \
15+
python-natsort \
16+
python-setuptools
1017

1118
RUN pip install --upgrade pip
1219

13-
RUN pip install netaddr ipaddr jinja2 pyangbind==0.5.10
20+
RUN pip install \
21+
netaddr \
22+
ipaddr \
23+
jinja2 \
24+
pyangbind==0.5.10
1425

1526
{% if docker_config_engine_stretch_debs.strip() %}
16-
COPY \
17-
{% for deb in docker_config_engine_stretch_debs.split(' ') -%}
18-
debs/{{ deb }}{{' '}}
19-
{%- endfor -%}
20-
debs/
21-
{%- endif -%}
27+
# Copy locally-built Debian package dependencies
28+
{{ copy_files("debs/", docker_config_engine_stretch_debs.split(' '), "/debs/") }}
2229

23-
{% if docker_config_engine_stretch_debs.strip() %}
24-
RUN dpkg -i \
25-
{% for deb in docker_config_engine_stretch_debs.split(' ') -%}
26-
debs/{{ deb }}{{' '}}
27-
{%- endfor %}
28-
{%- endif -%}
30+
# Install locally-built Debian packages and implicitly install their dependencies
31+
{{ install_debian_packages(docker_config_engine_stretch_debs.split(' ')) }}
32+
{% endif %}
2933

3034
{% if docker_config_engine_stretch_whls.strip() %}
31-
COPY \
32-
{% for whl in docker_config_engine_stretch_whls.split(' ') -%}
33-
python-wheels/{{ whl }}{{' '}}
34-
{%- endfor -%}
35-
python-wheels/
36-
{%- endif -%}
35+
# Copy locally-built Python wheel dependencies
36+
{{ copy_files("python-wheels/", docker_config_engine_stretch_whls.split(' '), "/python-wheels/") }}
3737

38-
{% if docker_config_engine_stretch_whls.strip() %}
39-
RUN pip install \
40-
{% for whl in docker_config_engine_stretch_whls.split(' ') -%}
41-
python-wheels/{{ whl }}{{' '}}
42-
{%- endfor %}
43-
{%- endif -%}
38+
# Install locally-built Python wheel dependencies
39+
{{ install_python_wheels(docker_config_engine_stretch_whls.split(' ')) }}
40+
{% endif %}
4441

4542
## Clean up
46-
RUN apt-get purge -y python-pip python-dev; apt-get clean -y; apt-get autoclean -y; apt-get autoremove -y
47-
RUN rm -rf /debs /python-wheels
43+
RUN apt-get purge -y \
44+
python-pip \
45+
python-dev && \
46+
apt-get clean -y && \
47+
apt-get autoclean -y && \
48+
apt-get autoremove -y && \
49+
rm -rf /debs /python-wheels

dockers/docker-database/Dockerfile.j2

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
12
FROM docker-config-engine-stretch
23

34
ARG docker_container_name
@@ -11,23 +12,18 @@ RUN apt-get update
1112

1213
{% if docker_database_debs.strip() -%}
1314
# Copy locally-built Debian package dependencies
14-
{%- for deb in docker_database_debs.split(' ') %}
15-
COPY debs/{{ deb }} /debs/
16-
{%- endfor %}
15+
{{ copy_files("debs/", docker_database_debs.split(' '), "/debs/") }}
1716

1817
# Install locally-built Debian packages and implicitly install their dependencies
19-
{%- for deb in docker_database_debs.split(' ') %}
20-
RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return 1; }; dpkg_apt /debs/{{ deb }}
21-
{%- endfor %}
18+
{{ install_debian_packages(docker_database_debs.split(' ')) }}
2219
{%- endif %}
2320

2421
# Clean up
25-
RUN apt-get clean -y
26-
RUN apt-get autoclean -y
27-
RUN apt-get autoremove -y
28-
RUN rm -rf /debs ~/.cache
29-
30-
RUN sed -ri 's/^(save .*$)/# \1/g; \
22+
RUN apt-get clean -y && \
23+
apt-get autoclean -y && \
24+
apt-get autoremove -y && \
25+
rm -rf /debs ~/.cache && \
26+
sed -ri 's/^(save .*$)/# \1/g; \
3127
s/^daemonize yes$/daemonize no/; \
3228
s/^logfile .*$/logfile ""/; \
3329
s/^# syslog-enabled no$/syslog-enabled no/; \

dockers/docker-dhcp-relay/Dockerfile.j2

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
12
FROM docker-config-engine-stretch
23

34
ARG docker_container_name
@@ -11,19 +12,17 @@ RUN apt-get update
1112

1213
{% if docker_dhcp_relay_debs.strip() -%}
1314
# Copy built Debian packages
14-
{%- for deb in docker_dhcp_relay_debs.split(' ') %}
15-
COPY debs/{{ deb }} debs/
16-
{%- endfor %}
15+
{{ copy_files("debs/", docker_dhcp_relay_debs.split(' '), "/debs/") }}
1716

1817
# Install built Debian packages and implicitly install their dependencies
19-
{%- for deb in docker_dhcp_relay_debs.split(' ') %}
20-
RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return 1; }; dpkg_apt debs/{{ deb }}
21-
{%- endfor %}
18+
{{ install_debian_packages(docker_dhcp_relay_debs.split(' ')) }}
2219
{%- endif %}
2320

2421
# Clean up
25-
RUN apt-get clean -y; apt-get autoclean -y; apt-get autoremove -y
26-
RUN rm -rf /debs
22+
RUN apt-get clean -y && \
23+
apt-get autoclean -y && \
24+
apt-get autoremove -y && \
25+
rm -rf /debs
2726

2827
COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
2928
COPY ["docker-dhcp-relay.supervisord.conf.j2", "wait_for_intf.sh.j2", "/usr/share/sonic/templates/"]

dockers/docker-fpm-frr/Dockerfile.j2

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
12
FROM docker-config-engine-stretch
23

34
ARG docker_container_name
@@ -10,33 +11,37 @@ RUN [ -f /etc/rsyslog.conf ] && sed -ri "s/%syslogtag%/$docker_container_name#%s
1011
ENV DEBIAN_FRONTEND=noninteractive
1112

1213
# Update apt's cache of available packages
13-
RUN apt-get update
14-
1514
# Install required packages
16-
RUN apt-get install -y libdbus-1-3 libdaemon0 libjansson4 libc-ares2 iproute2 libpython2.7 libjson-c3 logrotate libunwind8
17-
18-
{% if docker_fpm_frr_debs.strip() -%}
19-
# Copy locally-built Debian package dependencies
20-
{%- for deb in docker_fpm_frr_debs.split(' ') %}
21-
COPY debs/{{ deb }} /debs/
22-
{%- endfor %}
15+
RUN apt-get update && \
16+
apt-get install -y \
17+
libdbus-1-3 \
18+
libdaemon0 \
19+
libjansson4 \
20+
libc-ares2 \
21+
iproute2 \
22+
libpython2.7 \
23+
libjson-c3 \
24+
logrotate \
25+
libunwind8
2326

2427
RUN groupadd -g ${frr_user_gid} frr
2528
RUN useradd -u ${frr_user_uid} -g ${frr_user_gid} -M -s /bin/false frr
2629

30+
{% if docker_fpm_frr_debs.strip() -%}
31+
# Copy locally-built Debian package dependencies
32+
{{ copy_files("debs/", docker_fpm_frr_debs.split(' '), "/debs/") }}
33+
2734
# Install locally-built Debian packages and implicitly install their dependencies
28-
{%- for deb in docker_fpm_frr_debs.split(' ') %}
29-
RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return 1; }; dpkg_apt /debs/{{ deb }}
30-
{%- endfor %}
35+
{{ install_debian_packages(docker_fpm_frr_debs.split(' ')) }}
3136
{%- endif %}
3237

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

3540
# Clean up
36-
RUN apt-get clean -y
37-
RUN apt-get autoclean -y
38-
RUN apt-get autoremove -y
39-
RUN rm -rf /debs ~/.cache
41+
RUN apt-get clean -y && \
42+
apt-get autoclean -y && \
43+
apt-get autoremove -y && \
44+
rm -rf /debs ~/.cache
4045

4146
COPY ["bgpcfgd", "start.sh", "/usr/bin/"]
4247
COPY ["*.j2", "/usr/share/sonic/templates/"]

dockers/docker-lldp-sv2/Dockerfile.j2

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
12
FROM docker-config-engine-stretch
23

34
ARG docker_container_name
@@ -9,37 +10,30 @@ ENV DEBIAN_FRONTEND=noninteractive
910
# Update apt's cache of available packages
1011
RUN apt-get update
1112

12-
1313
{% if docker_lldp_sv2_debs.strip() -%}
1414
# Copy locally-built Debian package dependencies
15-
{%- for deb in docker_lldp_sv2_debs.split(' ') %}
16-
COPY debs/{{ deb }} /debs/
17-
{%- endfor %}
15+
{{ copy_files("debs/", docker_lldp_sv2_debs.split(' '), "/debs/") }}
1816

1917
# Install locally-built Debian packages and implicitly install their dependencies
20-
{%- for deb in docker_lldp_sv2_debs.split(' ') %}
21-
RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return 1; }; dpkg_apt /debs/{{ deb }}
22-
{%- endfor %}
18+
{{ install_debian_packages(docker_lldp_sv2_debs.split(' ')) }}
2319
{%- endif %}
2420

2521
{% if docker_lldp_sv2_whls.strip() -%}
2622
# Copy locally-built Python wheel dependencies
27-
{%- for whl in docker_lldp_sv2_whls.split(' ') %}
28-
COPY python-wheels/{{ whl }} /python-wheels/
29-
{%- endfor %}
23+
{{ copy_files("python-wheels/", docker_lldp_sv2_whls.split(' '), "/python-wheels/") }}
3024

3125
# Install locally-built Python wheel dependencies
32-
{%- for whl in docker_lldp_sv2_whls.split(' ') %}
33-
RUN pip install /python-wheels/{{ whl }}
34-
{%- endfor %}
26+
{{ install_python_wheels(docker_lldp_sv2_whls.split(' ')) }}
3527
{% endif %}
3628

3729
# Clean up
38-
RUN apt-get purge -y python-pip
39-
RUN apt-get clean -y
40-
RUN apt-get autoclean -y
41-
RUN apt-get autoremove -y
42-
RUN rm -rf /debs /python-wheels ~/.cache
30+
RUN apt-get purge -y python-pip && \
31+
apt-get clean -y && \
32+
apt-get autoclean -y && \
33+
apt-get autoremove -y && \
34+
rm -rf /debs \
35+
/python-wheels \
36+
~/.cache
4337

4438
COPY ["start.sh", "/usr/bin/"]
4539
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]

0 commit comments

Comments
 (0)