1

I'm trying to export environment variables from a Docker container using a shell script. This script will run a command and save the output as an environment variable. These variables will be used by the Java application.

The shell script variables are not being populated when I run the Docker container.

What is the best way to export environment variables from a Docker container using a shell script?

FROM maven:3.8-amazoncorretto-11 as TEMP_BUILD_IMAGE
ENV APP_HOME=/src
WORKDIR $APP_HOME/
COPY pom.xml $APP_HOME/
RUN mvn clean
RUN mvn dependency:resolve
COPY src $APP_HOME/src/
RUN mvn package -Dmaven.test.skip
FROM amazoncorretto:11
ARG ENVIRONMENT
RUN yum install -y shadow-utils
ENV ARTIFACT_NAME=application-0.0.1-SNAPSHOT.jar
WORKDIR /home/nonroot/application/
COPY environment_config.sh environment_config.sh
RUN yum install -y jq 
ENV JAVA_OPTS=""
CMD ["sh", "-c", "source ./environment_config.sh && java $JAVA_OPTS -jar $ARTIFACT_NAME --spring.config.location=file:./config/application.properties"]

#!/bin/bash
echo "environment ${ENVIRONMENT}"
export API_URL=$(eval COMMAND)
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
Dinesh Kumar
  • 483
  • 8
  • 24

1 Answers1

1

Using script to compute environment variables in dockerfile.

Suggesting to look into this answer.

The question is ambiguous about using a shell script origin. And the target environment context.


1. Use a shell script in the container's host.

Use ssh command to connect with container to dig for the required environment inside the container.

ssh -i container-ssh.key root@container.ip 'echo $(your-environment-var)'

2. Use a shell script inside the container, to affect the environment variable of the container's host.

No can do: this request contradicts the fundamental container context isolation from host context.

The best suggestion is to write a shell script in the container that writes value to a file CNT_ENV. And file CNT_ENV is located in a volume accessible to container's host.

In container's host, write a script or service to update host environment variable from value in file CNT_ENV.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
  • Docker entrypoint command `source ./environment_config.sh` is not working. I can able to see the echo message but environment variable alone not working. Am I missing something? – Dinesh Kumar May 22 '22 at 08:43
  • please see this answer: https://stackoverflow.com/questions/34911622/dockerfile-set-env-to-result-of-command#37860420. The official strategy is to add `./environment_config.sh` to `.profile`. Like this `run echo ". ./environment" >> ~/.profile` – Dudi Boy May 22 '22 at 08:56
  • This method will not work for me. Because that script will only work after the container is started. It won't work while building the image. – Dinesh Kumar May 22 '22 at 10:51
  • Suggesting to test. Since each line in dockerfile is a container. Each line must run the login script (.profile or .bash_profile). This method should work if root shell is `sh`. If root shell is `bash` than append the `. ./environment` to `~/.bash_profile`. Or preapre a login script and copy it to the container. – Dudi Boy May 22 '22 at 11:26