Skip to content

Commit

Permalink
Multi-Stage Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
YoussefBayouli authored Sep 30, 2024
1 parent e1884bb commit 704f118
Showing 1 changed file with 82 additions and 70 deletions.
152 changes: 82 additions & 70 deletions build/build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,16 @@

# ForceUpdate 12 -- change here if you need to force a rebuild
# Base Stage - Build Tools
# Build Stage - Build Tools and Dependencies
FROM debian:bookworm AS build-tools

# Install dependencies for building the dummy package
# Install base build tools and dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
gnupg \
curl \
git \
wget \
rsync \
make \
python3 \
bash-completion \
zip \
nano \
jq \
graphviz \
gettext-base \
plantuml \
software-properties-common \
ruby-full \
&& apt-get clean && rm -rf /var/lib/apt/lists/*

# Create the dummy python2.7 package and specify the output location as /opt
apt-get install -y build-essential gnupg curl git wget psmisc rsync make python3 bash-completion \
zip nano jq graphviz gettext-base plantuml software-properties-common ruby-full && \
apt-get clean

# Create a dummy python2.7 package using fpm
RUN gem install fpm && \
fpm -s empty -t deb -p /opt/python2.7-0.0.0-0-any.deb --name python2.7 --license MIT --version 0.0.0 --architecture all --description 'Dummy package to satisfy google-cloud-cli dependencies.' --maintainer 'Yourself' && \
dpkg -i /opt/python2.7-0.0.0-0-any.deb && \
Expand All @@ -47,86 +32,113 @@ RUN gem install fpm && \
# Install Go
WORKDIR /usr/local
ENV GO_VERSION=1.21.12
RUN wget -q https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && \
tar -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
rm go${GO_VERSION}.linux-amd64.tar.gz && \
mkdir /go

ENV GOPATH /go
ENV GO111MODULE=on
ENV PATH /usr/local/go/bin:/go/bin:$PATH
RUN wget -q https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && \
tar -xzf go${GO_VERSION}.linux-amd64.tar.gz && rm go${GO_VERSION}.linux-amd64.tar.gz && mkdir ${GOPATH}

# Install Go tools
# Install Google Cloud SDK and dependencies
WORKDIR /opt
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - && \
apt-get update -y && \
apt-get install -y google-cloud-cli google-cloud-cli-gke-gcloud-auth-plugin google-cloud-cli-app-engine-go && \
echo "source /usr/share/google-cloud-sdk/completion.bash.inc" >> /root/.bashrc

# Install Go tools and additional binaries
ENV PATH /usr/local/go/bin:/go/bin:$PATH
RUN go install golang.org/x/tools/cmd/goimports@latest && \
go install golang.org/x/pkgsite/cmd/pkgsite@latest

# Install golangci-lint
RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Install specific kubectl version
ENV KUBERNETES_VER 1.29.7
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VER}/bin/linux/amd64/kubectl && \
chmod go+rx ./kubectl && mv ./kubectl /usr/local/bin/kubectl && \
echo "source <(kubectl completion bash)" >> /root/.bashrc

# Clone Kubernetes code generator tools
RUN mkdir -p /go/src/k8s.io && cd /go/src/k8s.io && \
git clone -b v0.30.0 --depth=3 https://github.com/kubernetes/code-generator.git

# Install Helm
# Install Helm package manager
ENV HELM_VER 3.10.3
RUN curl -L https://get.helm.sh/helm-v${HELM_VER}-linux-amd64.tar.gz | tar -xz -C /tmp && \
mv /tmp/linux-amd64/helm /usr/local/bin/helm && \
chmod go+rx /usr/local/bin/helm
RUN curl -L https://get.helm.sh/helm-v${HELM_VER}-linux-amd64.tar.gz -o /tmp/helm.tar.gz && \
tar -zxvf /tmp/helm.tar.gz -C /tmp && mv /tmp/linux-amd64/helm /usr/local/bin/helm && \
chmod go+rx /usr/local/bin/helm && rm /tmp/helm.tar.gz && rm -rf /tmp/linux-amd64

# Install golangci-lint
RUN go install github.com/golangci/golangci-lint/cmd/[email protected]

# Install yamllint
RUN apt-get update && apt-get install -y yamllint

# Install gotestsum
RUN go install gotest.tools/gotestsum@latest

# Install Hugo
ENV HUGO_VER 0.118.2
RUN wget -q https://github.com/gohugoio/hugo/releases/download/v${HUGO_VER}/hugo_extended_${HUGO_VER}_Linux-64bit.tar.gz -O /tmp/hugo.tar.gz && \
tar -xzvf /tmp/hugo.tar.gz -C /usr/local/bin/ hugo && rm /tmp/hugo.tar.gz

# Install Node.js (reduced layers)
RUN mkdir /tmp/hugo && \
wget -q -O /tmp/hugo/hugo.tar.gz https://github.com/gohugoio/hugo/releases/download/v${HUGO_VER}/hugo_extended_${HUGO_VER}_Linux-64bit.tar.gz && \
tar -zxvf /tmp/hugo/hugo.tar.gz -C /tmp/hugo/ && \
mv /tmp/hugo/hugo /usr/local/bin/ && \
rm -r /tmp/hugo

# Install Node.js
ARG NODE_MAJOR=20
RUN apt-get update && apt-get install -y ca-certificates curl gnupg && \
mkdir -p /etc/apt/keyrings && \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
apt-get update && apt-get install -y --no-install-recommends nodejs && \
apt-get clean && rm -rf /var/lib/apt/lists/*
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \
apt-get update && apt-get install -y nodejs

# Install API reference docs generator
RUN mkdir -p /go/src/github.com/ahmetb && \
cd /go/src/github.com/ahmetb && git clone -b v0.2.0 https://github.com/ahmetb/gen-crd-api-reference-docs && \
cd ./gen-crd-api-reference-docs && go build

# Install HTML checker
RUN mkdir /tmp/htmltest && \
wget -O /tmp/htmltest/htmltest.tar.gz https://github.com/wjdp/htmltest/releases/download/v0.13.0/htmltest_0.13.0_linux_amd64.tar.gz && \
tar -zxvf /tmp/htmltest/htmltest.tar.gz -C /tmp/htmltest && \
mv /tmp/htmltest/htmltest /usr/local/bin && rm -r /tmp/htmltest

# Install Terraform
RUN wget -nv -O terraform.zip https://releases.hashicorp.com/terraform/1.5.4/terraform_1.5.4_linux_amd64.zip && \
unzip ./terraform.zip && mv terraform /usr/local/bin/ && rm terraform.zip

# Final Stage - Application Runtime
# Copy scripts for the build stage
COPY *.sh /root/
RUN chmod +x /root/*.sh

# Final Stage - Runtime Environment
FROM debian:bookworm-slim AS runtime

# Installing only the necessary runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
psmisc \
rsync \
bash-completion \
ca-certificates \
curl \
gnupg \
&& apt-get clean && rm -rf /var/lib/apt/lists/*

# Copy the dummy python2.7 package from the build-tools stage to /tmp/
COPY --from=build-tools /opt/python2.7-0.0.0-0-any.deb /tmp/
# Install runtime dependencies
RUN apt-get update && apt-get install -y psmisc rsync bash-completion ca-certificates curl gnupg && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Install dummy python2.7 package
# Copy dummy python2.7 package from build-tools stage
COPY --from=build-tools /opt/python2.7-0.0.0-0-any.deb /tmp/
RUN dpkg -i /tmp/python2.7-0.0.0-0-any.deb && rm /tmp/python2.7-0.0.0-0-any.deb

# Copy necessary binaries from build-tools stage
# Copy Go, Helm, and Hugo binaries from build-tools stage
COPY --from=build-tools /usr/local/go /usr/local/go
COPY --from=build-tools /usr/local/bin/helm /usr/local/bin/helm
COPY --from=build-tools /usr/local/bin/hugo /usr/local/bin/hugo
COPY --from=build-tools /go/bin/golangci-lint /usr/local/bin/golangci-lint
COPY --from=build-tools /go/bin /go/bin

# Set environment variables
ENV GOPATH /go
ENV GO111MODULE=on
ENV PATH /usr/local/go/bin:/go/bin:$PATH

# Install Google Cloud CLI
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - && \
apt-get update && apt-get install -y google-cloud-cli google-cloud-cli-gke-gcloud-auth-plugin google-cloud-cli-app-engine-go && \
echo "source /usr/share/google-cloud-sdk/completion.bash.inc" >> /root/.bashrc

# Install specific kubectl version
ENV KUBERNETES_VER 1.29.7
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VER}/bin/linux/amd64/kubectl && \
chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl

# Copy the script files
# Copy the script files from build-tools stage
COPY --from=build-tools /root/*.sh /root/

# Final setup
# Set the default working directory
WORKDIR /go

# Set the default command
CMD ["/bin/bash"]

0 comments on commit 704f118

Please sign in to comment.