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)
CodePudding user response:
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 [email protected] '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
.