From cc6c625401a5140240787f0184131fd73d9c4b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20G=C4=85sior?= <60886162+adriang-90@users.noreply.github.com> Date: Wed, 13 Apr 2022 22:03:34 +0200 Subject: [PATCH] chore: refactor Dockerfiles to reduce build time (#9081) Signed-off-by: Adrian --- Dockerfile | 71 ++++++++++++++++++++------------------- Dockerfile.dev | 12 +++---- test/container/Dockerfile | 7 ++-- test/remote/Dockerfile | 4 +-- ui-test/Dockerfile | 10 +++--- 5 files changed, 51 insertions(+), 53 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3378e3d259968..1001778f12b25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,13 +4,14 @@ ARG BASE_IMAGE=docker.io/library/ubuntu:21.10 # Initial stage which pulls prepares build dependencies and CLI tooling we need for our final image # Also used as the image in CI jobs so needs all dependencies #################################################################################################### -FROM docker.io/library/golang:1.18 as builder +FROM docker.io/library/golang:1.18 AS builder RUN echo 'deb http://deb.debian.org/debian buster-backports main' >> /etc/apt/sources.list -RUN apt-get update && apt-get install -y \ +RUN apt-get update && apt-get install --no-install-recommends -y \ openssh-server \ nginx \ + unzip \ fcgiwrap \ git \ git-lfs \ @@ -24,18 +25,17 @@ RUN apt-get update && apt-get install -y \ WORKDIR /tmp -ADD hack/install.sh . -ADD hack/installers installers -ADD hack/tool-versions.sh . +COPY hack/install.sh hack/tool-versions.sh ./ +COPY hack/installers installers -RUN ./install.sh helm-linux -RUN INSTALL_PATH=/usr/local/bin ./install.sh kustomize -RUN ./install.sh awscli-linux +RUN ./install.sh helm-linux && \ + INSTALL_PATH=/usr/local/bin ./install.sh kustomize && \ + ./install.sh awscli-linux #################################################################################################### # Argo CD Base - used as the base for both the release and dev argocd images #################################################################################################### -FROM $BASE_IMAGE as argocd-base +FROM $BASE_IMAGE AS argocd-base USER root @@ -48,7 +48,8 @@ RUN groupadd -g 999 argocd && \ chmod g=u /home/argocd && \ apt-get update && \ apt-get dist-upgrade -y && \ - apt-get install -y git git-lfs tini gpg tzdata && \ + apt-get install -y \ + git git-lfs tini gpg tzdata && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* @@ -59,19 +60,20 @@ COPY --from=builder /usr/local/bin/kustomize /usr/local/bin/kustomize COPY --from=builder /usr/local/aws-cli/v2/current/dist /usr/local/aws-cli/v2/current/dist COPY entrypoint.sh /usr/local/bin/entrypoint.sh # keep uid_entrypoint.sh for backward compatibility -RUN ln -s /usr/local/bin/entrypoint.sh /usr/local/bin/uid_entrypoint.sh -RUN ln -s /usr/local/aws-cli/v2/current/dist/aws /usr/local/bin/aws +RUN ln -s /usr/local/bin/entrypoint.sh /usr/local/bin/uid_entrypoint.sh && \ + ln -s /usr/local/aws-cli/v2/current/dist/aws /usr/local/bin/aws # support for mounting configuration from a configmap -RUN mkdir -p /app/config/ssh && \ - touch /app/config/ssh/ssh_known_hosts && \ - ln -s /app/config/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts +WORKDIR /app/config/ssh +RUN touch ssh_known_hosts && \ + ln -s ssh_known_hosts /etc/ssh/ssh_known_hosts -RUN mkdir -p /app/config/tls -RUN mkdir -p /app/config/gpg/source && \ - mkdir -p /app/config/gpg/keys && \ - chown argocd /app/config/gpg/keys && \ - chmod 0700 /app/config/gpg/keys +WORKDIR /app/config +RUN mkdir -p tls && \ + mkdir -p gpg/source && \ + mkdir -p gpg/keys && \ + chown argocd gpg/keys && \ + chmod 0700 gpg/keys ENV USER=argocd @@ -81,14 +83,15 @@ WORKDIR /home/argocd #################################################################################################### # Argo CD UI stage #################################################################################################### -FROM --platform=$BUILDPLATFORM docker.io/library/node:12.18.4 as argocd-ui +FROM --platform=$BUILDPLATFORM docker.io/library/node:12.18.4 AS argocd-ui WORKDIR /src -ADD ["ui/package.json", "ui/yarn.lock", "./"] +COPY ["ui/package.json", "ui/yarn.lock", "./"] -RUN yarn install --network-timeout 200000 +RUN yarn install --network-timeout 200000 && \ + yarn cache clean -ADD ["ui/", "."] +COPY ["ui/", "."] ARG ARGO_VERSION=latest ENV ARGO_VERSION=$ARGO_VERSION @@ -97,13 +100,11 @@ RUN HOST_ARCH='amd64' NODE_ENV='production' NODE_ONLINE_ENV='online' NODE_OPTION #################################################################################################### # Argo CD Build stage which performs the actual build of Argo CD binaries #################################################################################################### -FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.18 as argocd-build +FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.18 AS argocd-build WORKDIR /go/src/github.com/argoproj/argo-cd -COPY go.mod go.mod -COPY go.sum go.sum - +COPY go.* ./ RUN go mod download # Perform the build @@ -120,12 +121,12 @@ FROM argocd-base COPY --from=argocd-build /go/src/github.com/argoproj/argo-cd/dist/argocd* /usr/local/bin/ USER root -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-server -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-repo-server -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-cmp-server -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-application-controller -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-dex -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-notifications -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-applicationset-controller +RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-server && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-repo-server && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-cmp-server && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-application-controller && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-dex && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-notifications && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-applicationset-controller USER 999 diff --git a/Dockerfile.dev b/Dockerfile.dev index 8c8d5e6a247d8..978304f80fc44 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -5,11 +5,11 @@ FROM argocd-base COPY argocd /usr/local/bin/ USER root -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-server -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-repo-server -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-application-controller -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-dex -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-notifications -RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-applicationset-controller +RUN ln -s /usr/local/bin/argocd /usr/local/bin/argocd-server && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-repo-server && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-application-controller && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-dex && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-notifications && \ + ln -s /usr/local/bin/argocd /usr/local/bin/argocd-applicationset-controller USER 999 diff --git a/test/container/Dockerfile b/test/container/Dockerfile index 24146fb926437..1001e90fb9311 100644 --- a/test/container/Dockerfile +++ b/test/container/Dockerfile @@ -35,11 +35,8 @@ ENV GOROOT /usr/local/go ENV GOPATH /go # Install build and test dependencies -ADD ./hack/tool-versions.sh . -ADD ./hack/install.sh . -ADD ./hack/installers installers -ADD go.mod go.mod -ADD go.sum go.sum +COPY hack/install.sh hack/tool-versions.sh go.* ./ +COPY hack/installers installers RUN ./install.sh helm-linux && \ ./install.sh kubectl-linux && \ diff --git a/test/remote/Dockerfile b/test/remote/Dockerfile index 18419cbbcd69d..5b3d5d7ccc7ce 100644 --- a/test/remote/Dockerfile +++ b/test/remote/Dockerfile @@ -6,7 +6,7 @@ RUN go install github.com/mattn/goreman@latest && \ FROM ubuntu:21.10 ENV DEBIAN_FRONTEND=noninteractive -RUN apt-get update && apt-get install -y \ +RUN apt-get update && apt-get install --no-install-recommends -y \ ca-certificates \ curl \ openssh-server \ @@ -23,7 +23,7 @@ RUN apt-get update && apt-get install -y \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* -RUN mkdir -p /etc/certs +WORKDIR /etc/certs # These are required for running end-to-end tests COPY ./test/fixture/testrepos/id_rsa.pub /root/.ssh/authorized_keys diff --git a/ui-test/Dockerfile b/ui-test/Dockerfile index de959466035b1..a58d05f23fda4 100644 --- a/ui-test/Dockerfile +++ b/ui-test/Dockerfile @@ -1,7 +1,7 @@ -FROM node:12.18.4 as node +FROM node:12.18.4 AS node -RUN apt-get update && apt-get install -y \ - software-properties-common +RUN apt-get update && apt-get install --no-install-recommends -y \ + software-properties-common #Chrome browser to run the tests ARG CHROME_VERSION=86.0.4240.75 @@ -9,7 +9,7 @@ RUN curl https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add \ && wget https://www.slimjet.com/chrome/download-chrome.php?file=files%2F$CHROME_VERSION%2Fgoogle-chrome-stable_current_amd64.deb \ && dpkg -i download-chrome*.deb || true -RUN apt-get install -y -f \ +RUN apt-get install --no-install-recommends -y -f \ && rm -rf /var/lib/apt/lists/* #Disable the SUID sandbox so that chrome can launch without being in a privileged container @@ -20,6 +20,6 @@ RUN dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /o WORKDIR /usr/src/app COPY package*.json ./ COPY yarn.lock ./ -RUN yarn install +RUN yarn install && yarn cache clean COPY . .