Skip to content

Instantly share code, notes, and snippets.

@PavelVanecek
Created September 28, 2015 17:51
Show Gist options
  • Save PavelVanecek/ada56650d116b73586b8 to your computer and use it in GitHub Desktop.
Save PavelVanecek/ada56650d116b73586b8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# There are several options how to provide config to a Docker container during runtime.
# The idea is to allow flexibility in environment settings without knowing the application structure and without changing it.
# This gist shows how to configure database hostname.
# a) Pass as environment variables.
# Your script can read it too.
# in Node.js as `process.env.DB_HOST`
# in Python os.environ.get('DB_HOST')
# etc.
docker run --env DB_HOST=127.0.0.1 --rm -it ubuntu bash -c "echo \$DB_HOST"
# b) Add host to container
# The name you choose will be available as hostname, ready to use directly.
docker run --add-host=database:127.0.0.1 --rm -it ubuntu bash -c "ping -a -c 1 database"
# On your machine, you can simulate this behaviour by adding the very same hostname to /etc/hosts (Linux/OSX; Windows filename differs)
# b) Link another running container
# Note that this is still labeled as b), because your application cannot even tell the difference!
docker run --link mongo:mongo --rm -it ubuntu bash -c "ping -a -c 1 mongo"
# c) Let the application to read configuration from a file
# This is what most developers are familiar with, and frankly my least favourite. I shall therefore skip the example
# Hope this helps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment