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

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

Related

How to break a long environment variable text into multiple lines in dotenv file (for 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.

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

Docker composer parsing error for docker-compose.yml, at line 1, and column 1

I'm trying to setup selenoid and I'm having trouble with dockercomposer, it throws exception as below:
yaml.parser.ParserError: while parsing a flow mapping in "./docker-compose.yml", line 1, column 1 expected ',' or '}', but got '{' in "./docker-compose.yml", line 2, column 1
when I try to run docker command "$ sudo docker-compose up -d"
I'm in the terminal with same folder where docker-compose.yml present and content is as below,
version: '3'
services:
selenoid:
network_mode: bridge
image: aerokube/selenoid
volumes:
- "/docker:/etc/selenoid"
- "/var/run/docker.sock:/var/run/docker.sock"
- "/docker/video:/opt/selenoid/video"
environment:
- OVERRIDE_VIDEO_OUTPUT_DIR=/opt/selenium/video
- TZ=Europe/Amsterdam
command: ["-conf", "/etc/selenoid/browsers.json", "-video-output-dir", "/opt/selenoid/video"]
ports:
- "4444:4444"
I've also tried many online yml parsers and there is nothing wrong with yml file. Any help would be much appreciated.
Thanks
Sorry being late to answer my own post, there was nothing wrong with YML file, I should have created/edited it using programming editors such as IntelliJ, but I was editing and saving it using EditText. All worked fine after editing them and saving using IntelliJ in mac.

Environment precedence and ports

I have the following in my Dockerfile:
. . .
ENV SSL_PORT=443
. . .
EXPOSE ${SSL_PORT}
. . .
And the following in a docker-compose.override.yml file calling that image:
environment:
SSL_PORT: $SSL_PORT
ports:
- "${SSL_PORT}:${SSL_PORT}"
If I do that
WARNING: The SSL_PORT variable is not set. Defaulting to a blank string.
ERROR: The Compose file '.\docker-compose.override.yml' is invalid because:
services.ssl.ports contains an invalid type, it should be a number, or an object
If I set it in the .env file, the container is built.
Is there any way I can set the value of SSL_PORT in docker-compose.override.yml AND use that same value?
There are only two ways to set value of SSL_PORT variable into docker-compose.override.yml:
1. Declaring default environment variables in an environment file named .env placed in the folder where the docker-compose command is executed.
2. Add SSL_PORT variable into environment variables when you execute docker-compose command. It would be either:
SSL_PORT=443 docker-compose -f docker-compose.override.yml up
or
export SSL_PORT=443
docker-compose -f docker-compose.override.yml up

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}