# syntax=docker/dockerfile:1

# This Dockerfile is used to build Teleport on ARM (32-bit) only.
# The binaries produced need to be able to run on all the linux
# distributions we support. See https://goteleport.com/docs/installation/
# The binaries also run in the OCI containers we produce,
# both "heavy" and distroless.
#
FROM docker.io/library/debian:11

COPY locale.gen /etc/locale.gen
COPY profile /etc/profile

ENV LANGUAGE="en_US.UTF-8" \
    LANG="en_US.UTF-8" \
    LC_ALL="en_US.UTF-8" \
    LC_CTYPE="en_US.UTF-8" \
    DEBIAN_FRONTEND="noninteractive"

# BUILDARCH is automatically set by DOCKER when building the image with Build Kit (MacOS by default).
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
ARG BUILDARCH

RUN apt-get -y update && \
    apt-get install -q -y --no-install-recommends \
        build-essential \
        ca-certificates \
        curl \
        git \
        gzip \
        libc6-dev \
        libpam-dev \
        locales \
        pkg-config \
        sudo \
        unzip \
        zip \
        # ARM dependencies
        gcc-arm-linux-gnueabihf \
        libc6-dev-armhf-cross \
        # ARM64 dependencies
        gcc-aarch64-linux-gnu \
        libc6-dev-arm64-cross \
        && \
    dpkg-reconfigure locales && \
    apt-get -y clean && \
    rm -rf /var/lib/apt/lists/*

# Install Node.js.
ARG NODE_VERSION
ENV NODE_PATH="/usr/local/lib/nodejs-linux"
ENV PATH="$PATH:${NODE_PATH}/bin"
RUN NODE_ARCH="$(if [ "$BUILDARCH" = 'amd64' ]; then echo 'x64'; else echo 'arm64'; fi)" && \
    NODE_URL="https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${NODE_ARCH}.tar.xz" && \
    NODE_FILE="$(mktemp node-XXXXXX.tar.xz)" && \
    mkdir -p "$NODE_PATH" && \
    curl -o "$NODE_FILE" -fsSL "$NODE_URL" && \
    tar -xJf "$NODE_FILE" -C /usr/local/lib/nodejs-linux --strip-components=1 && \
    rm -f "$NODE_FILE"
RUN corepack enable yarn pnpm

# Install Go.
ARG GOLANG_VERSION
RUN mkdir -p /opt && \
    cd /opt && \
    curl -fsSL "https://go.dev/dl/$GOLANG_VERSION.linux-$BUILDARCH.tar.gz" | tar xz && \
    mkdir -p /go/src/github.com/gravitational/teleport && \
    chmod a+w /go && \
    chmod a+w /var/lib && \
    chmod a-w /
ENV GOPATH="/go" \
    GOROOT="/opt/go" \
    PATH="$PATH:/opt/go/bin:/go/bin:/go/src/github.com/gravitational/teleport/build"

# Add the CI user.
# This images is not used in CI, but because we used to use it in CI, we keep the same UID/GID and name.
ARG UID
ARG GID
RUN groupadd ci --gid="$GID" -o && \
    useradd ci --uid="$UID" --gid="$GID" --create-home --shell=/bin/sh && \
    mkdir -p -m0700 /var/lib/teleport && \
    chown -R ci /var/lib/teleport

# Install Rust.
ARG RUST_VERSION
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:$PATH \
    RUST_VERSION=$RUST_VERSION
RUN mkdir -p $RUSTUP_HOME && chmod a+w $RUSTUP_HOME && \
    mkdir -p $CARGO_HOME/registry && chmod -R a+w $CARGO_HOME
# Install Rust using the ci user, as that is the user that
# will run builds using the Rust toolchains we install here.
USER ci
RUN curl --proto '=https' --tlsv1.2 -fsSL https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain $RUST_VERSION && \
    rustup target add arm-unknown-linux-gnueabihf && \
    rustup --version && \
    cargo --version && \
    rustc --version

VOLUME ["/go/src/github.com/gravitational/teleport"]
