Skip to content

Instantly share code, notes, and snippets.

@mobmad
Last active August 29, 2015 14:17
Show Gist options
  • Save mobmad/7ad4adac06e26a0b854a to your computer and use it in GitHub Desktop.
Save mobmad/7ad4adac06e26a0b854a to your computer and use it in GitHub Desktop.
Forward/enable environment variables from docker (-e) to be visible even after changing user from root with "su -"

Dockerfile

FROM centos:6
RUN useradd appuser
CMD env|grep JAVA_OPTIONS|awk -F= '{print "export "$1"=\""$2"\""}'>/etc/profile.d/00_java-env.sh;bash

Usage

  1. docker build -t javaenv .
  2. docker run -it -e JAVA_OPTIONS="-DjvmProperty1=1 -DjvmProperty2=2" javaenv

Observe:

  • printenv on root includes JAVA_OPTIONS (as it would by default)
  • su - appuser wipes the environment variables set from docker run ... -e (as su - is intended to), but since we wrote the JAVA_OPTIONS to /etc/profile.d/ it is shared globally between all users (at least on CentOS) and printenv for appuser still includes the JAVA_OPTIONS passed to docker initially :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment