Skip to content

Commit a94eccb

Browse files
authored
new dockerfile version (#316)
This commit proposes a solution to the Docker container size problem. The image is built extracting from an intermediate layer only the executables (already compiled) and libraries needed. This way we reduced the image size from 2.5GB to about 250MB. Signed-off-by: Simone Magnani <[email protected]>
1 parent fd3dff5 commit a94eccb

File tree

6 files changed

+62
-61
lines changed

6 files changed

+62
-61
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ swagger-codegen*
88
cmake-build-debug/*
99
Vagrantfile
1010
libyang
11+
src/libs/prometheus-cpp/_build/
1112

1213
cmake-build-debug/

CI/Jenkinsfile.groovy

-29
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,6 @@ pipeline {
9696
checkout scm
9797
}
9898
}
99-
stage('Build Base Image') {
100-
when {
101-
triggeredBy 'TimerTrigger'
102-
}
103-
agent {
104-
label "docker"
105-
}
106-
steps {
107-
script {
108-
docker.withRegistry("", 'polycube-repo') {
109-
sh """
110-
export DOCKER_BUILDKIT=1
111-
docker system prune --all --force
112-
docker build . -t "polycubebot/base_image:${image_tag}" -f Dockerfile_base_image
113-
docker tag polycubebot/base_image:${image_tag} polycubebot/base_image:latest
114-
docker push polycubebot/base_image:${image_tag}
115-
docker push polycubebot/base_image:latest
116-
"""
117-
}
118-
}
119-
}
120-
post {
121-
failure {
122-
script {
123-
setGithubStatus("${commit_id}", "pending", github_token, "polycube-network/polycube", "Docker Image Build", "Docker Image Build")
124-
}
125-
}
126-
}
127-
}
12899
stage('Parallel Docker Build') {
129100
parallel {
130101
stage("Build Docker Mode: default") {

Dockerfile

+56-19
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,74 @@
1-
# syntax = tonistiigi/dockerfile:runmount20180618 # enables --mount option for run
2-
FROM polycubebot/base_image:latest
1+
# syntax = tonistiigi/dockerfile:runmount20180618
2+
###############################################
3+
# STEP 1: using an Ubuntu 18.04 image to install the entire Polycube framework with all dependencies.
4+
#
5+
# During this step an ubuntu image is used to compile Polycube with all its dependencies.
6+
# This dockerfile is reused for all the DEFAULT_MODE (default, pcn-iptables, pcn-k8s) which, thanks to install.sh and pre-requirements.sh scripts,
7+
# enable/disable Polycube services and install specific tools.
8+
FROM ubuntu:18.04 AS builder
9+
310
ARG DEFAULT_MODE=default
411
ENV MODE=$DEFAULT_MODE
5-
RUN echo "The mode is $MODE"
6-
RUN rm -rf /tmp/polycube
7-
RUN --mount=target=/polycube cp -r /polycube /tmp/polycube && \
8-
cd /tmp/polycube && \
12+
RUN --mount=target=/polycube cp -r /polycube /tmp/polycube && cd /tmp/polycube && \
13+
mkdir -p /usr/local/share/polycube /usr/local/include/polycube /opt/cni/bin /cni && touch /cni-conf /pcn_k8s /opt/cni/bin/polycube && \
914
SUDO="" USER="root" WORKDIR="/tmp/dev" ./scripts/install.sh $MODE && \
10-
# install pcn-kubernetes only components
15+
# install pcn-k8s only components
1116
if [ "$MODE" = "pcn-k8s" ] ; then \
1217
cd /tmp && mkdir -p tmp && cd tmp && \
1318
curl -sS -L https://storage.googleapis.com/kubernetes-release/network-plugins/cni-0799f5732f2a11b329d9e3d51b9c8f2e3759f2ff.tar.gz -o cni.tar.gz && \
1419
tar -xvf cni.tar.gz && \
15-
mkdir /cni && \
1620
cp bin/loopback /cni && \
1721
cd /tmp/polycube/src/components/k8s/cni/polycube && \
1822
GOOS=linux go build -o /opt/cni/bin/polycube . && \
1923
cd /tmp/polycube/src/components/k8s/cni/conf && \
2024
GOOS=linux go build -o /cni-conf . && \
2125
cd /tmp/polycube/src/components/k8s/pcn_k8s/ && \
2226
GOOS=linux go build -o /pcn_k8s . ; \
23-
fi && \
24-
apt-get purge --auto-remove -y git bison cmake flex \
25-
libllvm5.0 llvm-5.0-dev libclang-5.0-dev uuid-dev autoconf software-properties-common golang-go \
26-
libtool curl && \
27-
apt-get clean && \
28-
rm -fr /root /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/local/go/*
29-
# TODO: which other apt-get packages can be removed?
27+
fi
28+
29+
# copying cni scripts (will be removed if not needed)
30+
ADD src/components/k8s/cni/cni-install.sh src/components/k8s/cni/cni-uninstall.sh src/components/k8s/pcn_k8s/cleanup.sh src/components/k8s/pcn_k8s/init.sh /
31+
32+
###############################################
33+
# STEP 2: using an Ubuntu 18.04 image and extracting executables obtained by the image created at STEP 1.
34+
#
35+
# During this final step a new fresh ubuntu image is used and all the previously generated executables/libraries are copied.
36+
# This way, this final image contains only the result of the compilation and NOT the entire softwares/scripts needed at compile-time.
37+
# Moreover, depending on the DEFAULT_MODE, some tools that are needed at runtime (like iptables and iproute2 for the DEFAULT_MODE=pcn-k8s) are
38+
# installed.
39+
40+
FROM ubuntu:18.04
3041

31-
ADD src/components/k8s/cni/cni-install.sh /cni-install.sh
32-
ADD src/components/k8s/cni/cni-uninstall.sh /cni-uninstall.sh
33-
ADD src/components/k8s/pcn_k8s/cleanup.sh /cleanup.sh
34-
ADD src/components/k8s/pcn_k8s/init.sh /init.sh
42+
ARG DEFAULT_MODE=default
43+
ENV MODE=$DEFAULT_MODE
44+
# copying binaries
45+
COPY --from=builder /usr/local/bin /usr/local/bin
46+
COPY --from=builder /usr/local/share/polycube /usr/local/share/polycube
47+
# copying polycube services
48+
COPY --from=builder /usr/lib/lib*.so /usr/lib/
49+
# copying libpistache libyang libtins libprometheus .so
50+
COPY --from=builder /usr/local/lib/lib*.so /usr/local/lib/
51+
# copying libyang folder containing plugins (needed for yanglint)
52+
COPY --from=builder /usr/local/lib/libyang /usr/local/lib/libyang
53+
# copying main OS libraries
54+
COPY --from=builder /usr/lib/x86_64-linux-gnu/libnl*.so /usr/lib/x86_64-linux-gnu/libcrypto*.so\
55+
/usr/lib/x86_64-linux-gnu/libelf*.so /usr/lib/x86_64-linux-gnu/libssl*.so \
56+
/usr/lib/x86_64-linux-gnu/libpcap*.so /usr/lib/x86_64-linux-gnu/
57+
COPY --from=builder /lib/x86_64-linux-gnu/libnl*.so /lib/x86_64-linux-gnu/
58+
# copying base yang model
59+
COPY --from=builder /usr/local/include/polycube /usr/local/include/polycube
60+
# copying k8s scripts and cni if present
61+
COPY --from=builder /*.sh /cni* /pcn_k8s /
62+
COPY --from=builder /opt/cni/bin/polycube /opt/cni/bin/
63+
# creating log dir and file + configuring links for libraries + installing essential runtime tools
64+
RUN mkdir -p /var/log/polycube && touch /var/log/polycube/polycubed.log && \
65+
# install pcn-k8s only essential tools, else remove useless scripts
66+
if [ "$MODE" = "pcn-k8s" ] ; then \
67+
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq iptables iproute2 && \
68+
apt-get purge --auto-remove && apt-get clean ; \
69+
else \
70+
rm -rf /opt/cni/bin/polycube /*.sh /pcn_k8s /cni*; \
71+
fi && ldconfig
3572

3673
# by running nsenter --mount=/host/proc/1/ns/mnt polycubed, the daemon has a complete view of the namespaces of the host and it is able to manipulate them (needed for shadow services)
3774
CMD ["nsenter","--mount=/host/proc/1/ns/mnt","polycubed"]

Dockerfile_base_image

-7
This file was deleted.

Documentation/installation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ The list of available kernels is available at https://kernel.ubuntu.com/~kernel-
5858
Automatic installation from sources
5959
-----------------------------------
6060

61-
If you are running ``Ubuntu >= 18.04`` and you do not want to manually install Polycube and its dependencies, you can use the install script available under the ``scripts`` folder.
62-
This scripts has been tested on ``Ubuntu 18.04``, ``Ubuntu 19.04`` and ``Ubuntu 20.04``.
61+
If you are running ``Ubuntu >= 18.04`` and you do not want to manually install Polycube and its dependencies, you can use the install script available under the ``scripts`` folder as follows: ``./scripts/install.sh``. **DO NOT** run with sudo privileges, it will automatically ask you the password when needed, but the entire operations needs to be run by a default user.
62+
The scripts has been tested on ``Ubuntu 18.04``, ``Ubuntu 19.04`` and ``Ubuntu 20.04``.
6363

6464
Please notice that this script does not update the kernel version.
6565

scripts/install.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ set -e
6363

6464
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6565

66+
source scripts/pre-requirements.sh
67+
6668
echo "Install Prometheus-cpp"
67-
cd src/libs/prometheus-cpp
69+
cd $DIR/../src/libs/prometheus-cpp
6870
set +e
6971
if [ -d _build ]; then
7072
$SUDO rm -rf _build
@@ -75,9 +77,6 @@ cmake .. -DBUILD_SHARED_LIBS=ON
7577
make -j $(getconf _NPROCESSORS_ONLN)
7678
$SUDO make install
7779

78-
cd $DIR/..
79-
80-
source scripts/pre-requirements.sh
8180

8281
echo "Install polycube"
8382
cd $DIR/..

0 commit comments

Comments
 (0)