3

Question: Is it possible to set docker container env from docker daemon configuration file?

When run a container, we can use -e flag to set specific environment variables inside container, such as docker run -e foo=bar.

But is it possible to configure docker daemon file such as /etc/default/docker or /etc/sysconfig/docker, then every docker container can has same env foo=bar instead of use -e flag.

[root@i-7guzl6d7 ~]# cat /etc/sysconfig/docker
# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export TMPDIR="/mnt/bigdrive/docker-tmp"

# Set container environment here?
# export FOO=BAR
Allen Heavey
  • 530
  • 2
  • 5
  • 16

1 Answers1

3

No, there's currently no such option, however, there are some options to achieve this.

Using a Bash function

You can create a simple bash function that automatically sets these environment variables.

For example; this Bash function will automatically set foo=bar and bar=baz environment variables on docker run. Other docker commands are passed through as-is.

function docker() { case $* in run* ) shift 1; command docker run -e foo=bar -e bar=baz "$@" ;; * ) command docker "$@" ;; esac }

You can run the above line in your shell to load the function, or add it to your ~/.bash_profile, ~/.bash_login or ~/.profile to have it loaded automatically when you open a new shell.

Using Docker Compose

There has been a proposal to implement a configuration file for default options (see https://github.com/docker/docker/issues/7232), but this was closed in favor of using Docker Compose.

Have a look at docker compose to see if that fits your use-case. Docker Compose allows you to create a YAML file that defines your "project" -- all the containers that should be started to run your project, and how they should be configured. Compose supports variable substitution in the docker-compose.yml file, which allows you to set environment variables in your shell that are used inside the docker-compose.yml; see the documentation for that feature here; https://docs.docker.com/compose/compose-file/#variable-substitution

Community
  • 1
  • 1
thaJeztah
  • 27,738
  • 9
  • 73
  • 92