How to tokenize and use env variables used by docker-compose
.env file
PROTOCOL=http
# PROTOCOL=https
HOST=www.goo.com
PORT=5424
ROOT_URL=$PROTOCOL://$HOST:$PORT
The above doesn't work, what would be the best way to achieve this
Related
Take my example of two services:
services:
nginx:
ports:
- 443:443
volumes:
- "CONFIG_DIRECTORY/nginx/nginx.conf:/etc/nginx/nginx.conf"
- "CONFIG_DIRECTORY/certs:/etc/ssl/certs"
web:
command: ["node", "index.js"]
volumes:
- "CONFIG_DIRECTORY/certs:/var/client/config/ssl/certs"
- "CONFIG_DIRECTORY/process:/var/client/process"
I'd really like to be able to substitute a string such as /home/garnettm/development/config directly into the indicated CONFIG_DIRECTORY locations in the above strings.
Is there any way to do this other than the many currently available environment variable substitution process options?
A .env file for example would allow you to do this using an already defined variable and the $VARIABLE syntax.
The only form of string interpolation Compose supports at all is environment variable substitution. This uses shell-style $VARIABLE syntax, with very limited options for if the variable isn't set, ${VARIABLE:-default value} or ${VARIABLE:?error message}.
There is no way to declare these variables in the Compose file itself. Compose does support putting variable values in a .env file, so they don't necessarily need to be set as actual environment variables. You do not need to mention this file in a Compose env_file: directive, Compose reads it on its own to set environment variables to do these substitutions.
# .env
CONFIG_DIRECTORY=/home/garnettm/development/config
# docker-compose.yml
volumes:
- "$CONFIG_DIRECTORY/nginx/nginx.conf:/etc/nginx/nginx.conf"
For this specific setting, on the left-hand side of volumes: and other places that refer to host directories, relative paths are interpreted relative to the location of the Compose file (the first one, if you have multiple docker-compose -f options). A very common setup is to put all of the state and source code in the same directory tree, and to put the docker-compose.yml file at the root of that tree. In that case the relative directory . will almost always be right
volumes:
- "./nginx/nginx.conf:/etc/nginx/nginx.conf"
YAML anchors are occasionally used to provide common blocks of settings, but you can't do string interpolation using anchors, only supply an entire YAML scalar node. They won't be helpful here.
This is my connection string inside my docker env file:
DB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}#XX.XXXX.XXX.XXXX:27017/david?authSource=admin
And inside my env file I have this:
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example
I do I reference these values in the connection string above in the same file, or I need to hard code them. Currently I am using the format ${.....} but not sure it's the right way.
I have this in the env file:
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example
...
DB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}#XX.XXXX.XXX.XXXX:27017/david?authSource=admin
Using secrets from docker-compose on my dev machine works. But the remote server via ssh just says open /home/johndoe/~/my-secrets/jenkinsuser.txt: no such file or directory.
secret definition in stack.yml:
secrets:
jenkinsuser:
file: ~/my-secrets/jenkinsuser.txt
Run with:
docker stack deploy -c stack.yml mystack
The documentation does not mention any gotchas about ~ path. I am not going to put the secret files inside . as all examples do, because that directory is version controlled.
Am I missing some basics about variable expansion, or differences between docker-compose and docker swarm?
Your ~ character in your path is considered as literal. Use $HOME wich is treated as a variable in your string path.
Tilde character work only if it is unquoted. In your remote environment, the SWARM yaml parser consider your path as a string, where the prefix-tilde is read as a normal character (see prefix-tilde).
I am planning to self-host Bitwarden using Ansible. During the execution of the ansible playbook, a file hierarchy and a docker-compose file is generated on the remote based on config file parameters and other. At the end, this docker file is executed to spawn the containers.
My personal server infrastructure already servers an nginx-proxy combined with letsencrypt. In order to use this service, I need to add several specific environmental parameters to the "main container" in this docker-compose file. I collected them in an external file and now I need to add a this external file to the docker file. I want to do it dynamically. Until now, I am using the ansible task:
- name: append nginx.env as env file to automatically generated docker file
shell: 'sed -i "\$a\ \ \ \ \ \ - ../env/nginx.env" /srv/bitwarden/bwdata/docker/docker-compose.yml'
to transform:
...
*OTHER CONTAINERS*
...
nginx:
...
*UNIMPORTANT PARAMETERS*
...
env_file:
- ../env/uid.env(EOF)
into
...
*OTHER CONTAINERS*
...
nginx:
...
*UNIMPORTANT PARAMETERS*
...
env_file:
- ../env/uid.env
- ../env/nginx.env(EOF)
But I am unhappy with this sed solution because Bitwarden could modify this auto-generated file (appending to the end would eventually fail.. and I also can't really use any regEx because they could change the structure of single components of the file etc...).
Is there any built-in safe way by ansible to achieve this exact behavior?
EDIT: I am looking for something like:match <container_name> append to env_file <../env/nginx.env>
I am trying to run grafana but i need to specify the port using an environment variable. Currently, in the custom.ini i have
# The http port to use
http_port = 9082
how do i assign an environment variable to http_port ? It would be best if i could do it from the command line but the only command line options available are
Usage of bin\grafana-server.exe:
-config string
path to config file
-homepath string
path to grafana install/home path, defaults to working directory
-pidfile string
path to pid file
-v prints current version and exits
is there any way to assign an environment variable to the http_port?
I'm not a Windows user but I guess you can write a script (bat?) to first generate the custom.ini file filling http_port with an environment variable, then run grafana-server.exe pointing to the generated config file.