Reputation: 2304
My app uses some secrets, like password to database. I don't want to publish it inside any Docker file or any other app's file.
Is it possible to set environment variable on host machine, then run on this host Docker container with my app and use the host's environment variable inside of Docker image?
I would like to be able to get in my Docker container something like:
$ echo $DB_PASSWORD
But the DB_PASSWORD would be set on host machine.
Are the environment variables shared between host machine and its containers?
Upvotes: 1
Views: 6382
Reputation: 683
You can pass all the host envs with a little shell trickery
docker run $(printenv | cut -f1 -d= | sed 's/^/--env /g' | tr '\n' ' ') fancy-image
Upvotes: 0
Reputation: 1235
You can share the host enviorenment variable using the below command line statement
docker run --name mycontainer -e your_host_variable image-name
or you can use a file to get all the environement variable from in there
Please refer to this for more details, Its a nice blog.
Upvotes: 0
Reputation: 450
The environment variables are not automatically shared between docker containers and the host os.
You can set environment variables with the run command's --env
flag, though:
$ docker run --env DB_PASSWORD="ohsosecret" someuser/someimage
To use your host os environment variables do not provide a value for the container variable, the hosts value for that variable will be used automatically.
$ docker run --env DB_PASSWORD someuser/someimage
Upvotes: 4
Reputation: 2304
Here I post my solution how I work with secrets.
# docker-compose-development.yml version: '2' services: app: image: app_development build: context: ./app dockerfile: Dockerfile.development links: - db environment: DB_USER: ${DB_DEVELOP_USER} DB_PASS: ${DB_DEVELOP_PASS} db: image: mysql:latest ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: ${DB_DEVELOP_USER} MYSQL_USER: root MYSQL_PASSWORD: ${DB_DEVELOP_PASS} MYSQL_DATABASE: app_development
# docker-compose-test.yml version: '2' services: app: image: app_development build: context: ./app dockerfile: Dockerfile.test links: - db environment: DB_USER: ${DB_TEST_USER} DB_PASS: ${DB_TEST_PASS} db: image: mysql:latest ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: ${DB_TEST_USER} MYSQL_USER: root MYSQL_PASSWORD: ${DB_TEST_PASS} MYSQL_DATABASE: app_test
default: &default adapter: mysql2 encoding: utf8 pool: 5 socket: /var/run/mysqld/mysqld.sock development: adapter: mysql2 encoding: utf8 pool: 5 socket: /var/run/mysqld/mysqld.sock database: app_development host: 'db' port: 3306 username: password: test: adapter: mysql2 encoding: utf8 pool: 5 socket: /var/run/mysqld/mysqld.sock database: app_test host: 'db' port: 3306 username: password:
Upvotes: 0