linuxdockerkubernetesgithub-actions

Set ENV Variable Dynamically in a Dockerfile


I am creating a multi-arch docker image in GA using the Build and Push Action as:

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: my-dir
          push: true
          tags: myrepo.io/my-registry/my-app:${{ github.event_name == 'repository_dispatch' && github.event.client_payload.latest_ver || github.event.inputs.certified_version }}-multiarch
          file: Dockerfile_multiarch
          platforms: linux/amd64,linux/arm64
          provenance: false

In the dockerfile, I want to set the value of LD_PRELOAD as ENV LD_PRELOAD=/usr/lib/${ARCH}-linux-gnu/libjemalloc.so.2 where ARCH can be with x86_64 or aarch64. Setting this env variable inside a RUN block does not work because that variable is lost on that block finishes. How can I set this env var dynamically?

Here's my Dockerfile:

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND noninteractive

# Install required packages and dependencies for RPM
RUN apt-get update && \
    apt-get install -y wget curl vim less git python3 linux-tools-common sysstat procps libjemalloc-dev gnupg && \
    rm -rf /var/lib/apt/lists/*

ENV JAVA_HOME="/usr/lib/jvm/java-21-amazon-corretto"
ENV PATH=$JAVA_HOME/bin:$PATH

//todo: How can I do this?
ENV LD_PRELOAD=/usr/lib/${ARCH}-linux-gnu/libjemalloc.so.2

Solution

  • I would just make the path constant.

    RUN <code to get arch> && ln -s /usr/lib/${ARCH}-linux-gnu/libjemalloc.so.2 /
    ENV LD_PRELOAD=/libjemalloc.so.2