How to break a long environment variable text into multiple lines in dotenv file (for docker-compose) - docker-compose

I have a regular docker-compose.yml
version: "3.9"
services:
test_service:
image: some_img:0.1.0
...
...
env_file:
- .env
...
...
here, I am loading environment variables using a .env file. In the file I have an environment variable with a long string value.
# .env
SPACE=test
SPECIFICS=packageA,packageB,packageC,packageD,...
I want SPECIFICS to parsed as above(SPECIFICS=packageA,packageB,packageC,packageD,... ) but would like to break it down into multiple lines for easy reading, may be like below (with or without indentation)
SPECIFICS=packageA,
packageB,
packageC,
packageD,
...
I do not want to use environment: section in docker-compose.yml because I am loading quite a few environment variables which will bloat the corresponding section.
Can anyone suggest a solution for this
CHeers,
DD.

Related

how to set an environment variable when run - az container app compose create

It seems when my docker compose yml is run via "az containerapp compose create", environment variables are not picked up. Is there a way I can set an env variable so the command picks it up?
I'm seeing this error:
ERROR: The following field(s) are either invalid or missing. Invalid value: "${DOCKER_REGISTRY-}/sample-blazorapp": could not parse reference: ${DOCKER_REGISTRY-}/sample-blazorapp: template.containers.blazorapp.image.
I have set the variable with: export DOCKER_REGISTRY="myregistry"
And when I echo $DOCKER_REGISTRY, the value is returned. So in the bash session it is set (I tried powershell first, I thought that was the issue because $(envvar-) is bash syntax, however the error is the same.)
This is what I have in my compose file (alignment is correct in the file):
blazorapp:
container_name: "blazorapp"
image: ${DOCKER_REGISTRY-}sample-blazorapp
build:
context: .
dockerfile: BlazorApp/BlazorApp/Dockerfile
depends_on:
- redis
ports:
- "55000:443"
If I explicitly set the image name, i.e. not use an env var, then it works. i.e. this change to the image line works:
image: myregistry/sample-blazorapp
I also tried adding the forward slash, this makes no difference (as expected, it works fine without the slash when running docker compose up).
I can set it explicitly but that would be annoying. I feel like I'm missing something. Any help or guidance is greatly appreciated :)
If the image is defined like this into you docker compose file:
image: ${DOCKER_REGISTRY-}sample-blazorapp
then you must export using a slash at the end of the value:
export DOCKER_REGISTRY="myregistry/"
I discovered the issue, I was missing a colon.
Does not work (produces the error described in the question):
image: ${DOCKER_REGISTRY-}sample-blazorapp
Also does not work:
image: ${DOCKER_REGISTRY-mydefault}sample-blazorapp
Add the magic : in and it works:
image: ${DOCKER_REGISTRY:-}sample-blazorapp
Also works:
image: ${DOCKER_REGISTRY:-mydefault}sample-blazorapp

Unable to set file secrets passwords on docker-compose

I want to access a secret file with docker-compose. I follow many tutorials and reproduce the structure. The container is launched but when I need to log in, the right string to enter is "/run/secrets/sec_pgadmin_default_email". I want the login to be the one inside my secret file: admin. In this secret file pgadmin_default_email.txt, there is only the word admin. I don't know if the structure of this file is good. Should it be a dictionary or something else ?
version: '3.8' services: pgadmin4:
build:
context: ./dockerfiles/
dockerfile: dockerfile_pgadmin4
image: img_pgadmin
container_name: cont_pgadmin4
ports:
- "80:80"
secrets:
- sec_pgadmin_default_email
- sec_pgadmin_default_password
environment:
- PGADMIN_LISTEN_PORT=80
- PGADMIN_DEFAULT_EMAIL=/run/secrets/sec_pgadmin_default_email
- PGADMIN_DEFAULT_PASSWORD=/run/secrets/sec_pgadmin_default_password
secrets:
sec_pgadmin_default_email:
file: ./.secrets/pgadmin_default_email.txt
sec_pgadmin_default_password:
file: ./.secrets/pgadmin_default_password.txt
PS I just use the command docker-compose up -d, without swarn things or else. I don't define external secrets and I prefer not. Is it possible ?
the environment variable will be set to the path of the file containing the actual value. Not the actual value itself.
For example, in Go you could do something like:
var pgAdminDefaultEmailFile = os.Getenv("PGADMIN_DEFAULT_EMAIL") // get the file
pgAdminDefaultEmailValue, err := ioutil.ReadFile(pgAdminDefaultEmailFile) // get the value
if err != nil {
log.Fatal(err)
}
pgAdminDefaultEmail = string(pgAdminDefaultEmailValue) // parse out the value

How to set a variable from another yaml file in azure-pipeline.yml

I have an environment.yml shown as follow, I would like to read out the content of the name variable (core-force) and set it as a value of the global variable in my azure-pipeline.yamal file how can I do it?
name: core-force
channels:
- conda-forge
dependencies:
- click
- Sphinx
- sphinx_rtd_theme
- numpy
- pylint
- azure-cosmos
- python=3
- flask
- pytest
- shapely
in my azure-pipeline.yml file I would like to have something like
variables:
tag: get the value of the name from the environment.yml aka 'core-force'
Please check this example:
File: vars.yml
variables:
favoriteVeggie: 'brussels sprouts'
File: azure-pipelines.yml
variables:
- template: vars.yml # Template reference
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
Please note, that variables are simple string and if you want to use list you may need do some workaraund in powershell in place where you want to use value from that list.
If you don't want to use template functionality as it is shown above you need to do these:
create a separate job/stage
define step there to read environment.yml file and set variables using REST API or Azure CLI
create another job/stage and move you current build defitnion into there
I found this topic on developer community where you can read:
Yaml variables have always been string: string mappings. The doc appears to be currently correct, though we may have had a bug when last you visited.
We are preparing to release a feature in the near future to allow you to pass more complex structures. Stay tuned!
But I don't have more info bout this.
Global variables should be stored in a separate template file. This file ideally would be in a separate repo where other repos can refer to this.
Here is another answer for this

Can I include another source in the default .env file?

Is it possible with docker-compose to mimic following?
the .env file:
...
if [ -f "another.env" ]; then
. ./another.env
fi
....
I am getting no errors, just nothing happens.
Just try with env_file section:
version '3.6'
your-service:
env_file: ./another.env
For more information, have a look to docker-compose environment conf doc

How dynamic map service name to ENV var

Example:
my-server:
image: my-server:latest
ports:
- 1234:1234
proxy:
image: lb:latest
environment:
- BACKEND=${VAR}??? # must be resolve as 'my-server'
The server name can be changed to any name, but the proxy has a entry-point script where the variable will be substituted in the BACKEND to config.
You can use a .env file to define your variable. This file will be placed in the same directory as your docker-compose.yml file.
When you run docker-compose, it will read this value and use it. Using your example, your .env file would look something like this:
VAR=my-server
and, the line:
- BACKEND=${VAR}??? # must be resolve as 'my-server'
would become just:
- BACKEND=${VAR}
or
BACKEND: ${VAR}