Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing docker runtime environment variables in docker image

Here's my docker image. I want to override the default environment variables being set below from whatever is passed in the docker run command mentioned in the end

FROM ubuntu:16.04

ADD http://www.nic.funet.fi/pub/mirrors/apache.org/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz /usr/local/
RUN cd /usr/local && tar -zxvf apache-tomcat-8.0.48.tar.gz && rm apache-tomcat-8.0.48.tar.gz
RUN mv /usr/local/apache-tomcat-8.0.48 /usr/local/tomcat
RUN rm -rf /usr/local/tomcat/webapps/*

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
ENV CATALINA_HOME /usr/local/tomcat
ENV CATALINA_BASE /usr/local/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin

ENV dummy_url defaulturl
ENV database databasedefault

COPY my.war /usr/local/tomcat/webapps/

RUN echo >> /usr/local/tomcat/conf/test.properties
RUN echo dummy_url =$dummy_url >> /usr/local/tomcat/conf/test.properties
RUN echo database =$database >> /usr/local/tomcat/conf/test.properties

ENTRYPOINT ["catalina.sh", "run"]

To run in local :

docker run -p 8080:8080 -e dummy_url=http:google.com -e database=jdbc://mysql allimages/myimage:latest

dummy_url and database do not seem to be getting overridden in the file that I am adding them in - test.properties. Any ideas would be greatly appreciated.

like image 513
Righto Avatar asked Jan 11 '18 07:01

Righto


People also ask

Do docker images have environment variables?

When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

Is it possible to pass environment variables using Dockerfiles?

Passing Environment Variables Into a Dockerfile Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs. Let's see how we can use it to pass value to our greetings script. There are two different ways to do it.


2 Answers

I want to override the default environment variables being set below from whatever is passed in the docker run command mentioned in the end

That means overriding an image file (/usr/local/tomcat/conf/test.properties) when running the image as a container (docker run), not building the image (docker build and its --build-args option and its ARG Dockerfile entry).

That means you create locally a script file which:

  • modifies /usr/local/tomcat/conf/test.properties
  • calls catalina.sh run $@ (see also "Store Bash script arguments $@ in a variable" from "Accessing bash command line args $@ vs $*")

That is:

myscript.sh

#!/bin/sh
echo dummy_url=$dummy_url >> /usr/local/tomcat/conf/test.properties
echo database=$database >> /usr/local/tomcat/conf/test.properties
args=("$@")
catalina.sh run "${args[@]}"

You would modify your Dockerfile to COPY that script and call it:

COPY myscript.sh /usr/local/
...
ENTRYPOINT ["/usr/local/myscript.sh"]

Then, and only then, the -e options of docker run would work.

like image 151
VonC Avatar answered Oct 07 '22 15:10

VonC


You are confusing what gets executed when building the image and what gets executed when starting the container. The RUN command inside the dockerfile is executed when building the image, when running docker build ...

RUN echo dummy_url =$dummy_url >> /usr/local/tomcat/conf/test.properties
RUN echo database =$database >> /usr/local/tomcat/conf/test.properties

Thus when the above execute the file test.properties will contain the default values specified in the Dockerfile.

When you execute docker run -p 8080:8080 -e dummy_url=http:google.com -e database=jdbc://mysql allimages/myimage:latest the ENTRYPOINT ["catalina.sh", "run"] will get executed with env values dummy_url=http:google.com and database=jdbc://mysql.

You can allow values in test.properties to be ovveriden using:

  1. Move $dummy_url >> /usr/local/tomcat/conf/test.properties and $database >> /usr/local/tomcat/conf/test.properties to the start of catalina.sh script.

  1. Override the values when building the image as such:

ARG dummy_url_arg
ARG database_arg

ENV dummy_url $dummy_url_arg
ENV database $database_arg

COPY my.war /usr/local/tomcat/webapps/

RUN echo >> /usr/local/tomcat/conf/test.properties
RUN echo dummy_url =$dummy_url >> /usr/local/tomcat/conf/test.properties
RUN echo database =$database >> /usr/local/tomcat/conf/test.properties

ENTRYPOINT ["catalina.sh", "run"]

And when building the image override the values using docker build --build-arg dummy_url_arg=http:google.com --build-arg database_arg=jdbc://mysql allimages/myimage:latest ...

like image 21
yamenk Avatar answered Oct 07 '22 15:10

yamenk