Home > Mobile >  How to make dockerfile and image lightweight
How to make dockerfile and image lightweight

Time:12-07

I am trying to build docker image for my azuredevops build agent in a very restricted network environment and only few internet repos are enabled and not allowed to open any other firewall rules anymore.

So in order to get succeeded with the docker image build, I had to copy multiple deb files and other software repos and directories to the image layer and need to install them one by one and which resulted my docker image huge sized and build itself is taking long time.

So is there any way to segregate my image layers in better way, so that I can make the image size in lesser space and decrease the image build time.

FROM ubuntu:18.04
#2-Enable Ubuntu Packages
COPY ./sources.list /etc/apt/
#3- Install basic Softwares
ENV DEBIAN_FRONTEND=noninteractive
RUN echo "APT::Get::Assume-Yes \"true\";" > /etc/apt/apt.conf.d/90assumeyes
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    wget \
    jq \
    git \
    iputils-ping \
    libcurl4 \
    libicu60 \
    libunwind8 \
    netcat \
    telnet \
    libssl1.0 \
    python \
   python3 \
    nodejs \
    python3-setuptools \
    python3-pip \
    vim \
    openjdk-11-jdk-headless \
    gnupg \
    make \
    yarn\
    apt-transport-https \
    lsb-release \
  && rm -rf /var/lib/apt/lists/*

COPY ./sw/* /tmp/
#4-Install AzureCLI
RUN  curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
RUN AZ_REPO=$(lsb_release -cs) \
  && echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | tee /etc/apt/sources.list.d/azure-cli.list \
  && apt-get update \
  &&  apt-get install azure-cli

RUN  apt-get update &&  apt-get install build-essential
RUN apt --fix-broken install \
     && dpkg -i /tmp/containerd.io_1.6.9-1_amd64.deb \
     && dpkg -i /tmp/docker-ce-rootless-extras_20.10.9_3-0_ubuntu-bionic_amd64.deb \
     && dpkg -i /tmp/docker-ce-cli_20.10.9_3-0_ubuntu-bionic_amd64.deb \
     && apt-get install iptables && apt-get install libdevmapper1.02.1 \
     && dpkg -i /tmp/docker-compose-plugin_2.6.0_ubuntu-bionic_amd64.deb \
     && dpkg -i /tmp/docker-scan-plugin_0.9.0_ubuntu-bionic_amd64.deb

#8-install maven 3.8.6
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && tar -xzf /tmp/apache-maven-3.8.6-bin.tar.gz -C /usr/share/maven --strip-components=1 \
  \
  && echo "Cleaning and setting links" \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

## 8.1- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven \
    TARGETARCH=linux-x64 \
    MAVEN_CONFIG "$USER_HOME_DIR/.m2" \
    JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64

#Installl helm,docker, googlechrome and kubectl
RUN  tar zxvf /tmp/helm-v3.8.2-linux-amd64.tar.gz  && mv /linux-amd64/helm /usr/local/bin/ && mv /tmp/kubectl /usr/local/bin/ \
     &&  apt install /tmp/google-chrome-stable_current_amd64.deb

#11- Agent Installation
WORKDIR /azp
COPY ./vstsagent/ .
COPY ./start.sh .
COPY ./docker.sh .
RUN chmod  x start.sh docker.sh
CMD ["./docker.sh"]

CodePudding user response:

There are several ways to make a Dockerfile lightweight:

  1. Use a minimal base image: Instead of using a full-featured base image like Ubuntu or CentOS, consider using a minimal base image such as Alpine Linux.
  2. Use multi-stage builds: With multi-stage builds, you can use multiple FROM statements in your Dockerfile to create multiple intermediate images, each with its own set of dependencies. This allows you to separate the build environment (which may require a large set of dependencies) from the runtime environment (which only needs a minimal set of dependencies). This reduces the size of the final image by removing the build dependencies.
  3. Avoid installing unnecessary packages: In the Dockerfile above, the RUN command installs a large number of packages, many of which may not be necessary for the image (double check them :) )
  4. Use a package manager that supports removing unused dependencies: Some package managers, such as apt, will automatically install dependencies of the packages you install. This can result in a large number of unused dependencies being included in the image. To avoid this, you can use a package manager that allows you to remove unused dependencies, such as apk from Alpine Linux.
  5. Use Docker images with pre-installed dependencies: If you are using a common application or framework, you may be able to find a pre-built Docker image on Docker hub per exemple

I hope this is helpful for you!

  • Related