Environment precedence and ports - docker-compose

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

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

Kubernetes command arguments being ignored

when running specific command from linux terminal command is the following:
/opt/front/arena/sbin/ads_start ads -db_server vmazfassql01 -db_name Test1
In regular docker compose yaml file we define it like this:
ENTRYPOINT ["/opt/front/arena/sbin/ads_start", "ads" ]
command: ["-db_server vwmazfassql01","-db_name Test1"]
Then I tried to convert it to Kubernetes
command: ["/opt/front/arena/sbin/ads_start","ads"]
args: ["-db_server vwmazfassql01","-db_name Test1"]
or without quotes for args
command: ["/opt/front/arena/sbin/ads_start","ads"]
args: [-db_server vwmazfassql01,-db_name Test1]
but I got errors for both cases:
Unknown parameter value '-db_server vwmazfassql01'
Unknown parameter value '-db_name Test1'
I then tried to remove dashes from args but then it seems those values are being ignored and not set up. During the Initialization values process, during the container start, those properties seem to have they default values e.g. db_name: "ads". At least that is how it is printed out in the log during the Initialization.
I tried few more possibilities:
To define all of them in command:
command:
- /opt/front/arena/sbin/ads_start
- ads
- db_server vwmazfassql01
- db_name Test1
To define them in little bit different way:
command: ["/opt/front/arena/sbin/ads_start","ads"]
args:
- db_server vwmazfassql01
- db_name Test1
command: ["/opt/front/arena/sbin/ads_start","ads"]
args: [db_server vwmazfassql01,db_name Test1]
again they are being ignored, and not being set up.
Am I doing something wrong? How I can make some workaround for this? Thanks
I would try separating the args, following the documentation example (https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#run-a-command-in-a-shell)
Something like:
command: ["/opt/front/arena/sbin/ads_start", "ads"]
args: ["-db_server", "vwmazfassql01", "-db_name", "Test1"]
Or maybe, it would work even like this and it looks more clean:
command: ["/opt/front/arena/sbin/ads_start"]
args: ["ads", "-db_server", "vwmazfassql01", "-db_name", "Test1"]
This follows the general approach of running an external command from code (a random example is python subprocess module) where you specify each single piece of the command that means something on its own.

MIx environment variables from kubernetes deployment and docker image

I am trying to pass an environment variable in my deployment that should define a prefix based on a version number:
env:
- name: INDEX_PREFIX
value: myapp-$(VERSION)
$(VERSION) is not defined in my deployment but is set in the docker image used by the pod.
I tried to use both $() and ${} but VERSION is not interpolated in the environment of my pod. In my pod shell doing export TEST=myapp-${VERSION} does work though.
Is there any way to achieve what I am looking for? ie setting an environment variable in my deployment that reference an environment variable set in the docker image?
VERSION is an environment variable of the docker image. So you can assign it a value either inside the container or by passing
env:
- name : VERSION
value : YOUR-VALUE
In your case, VERSION is either set by a script inside the docker container or in the Dockerfile.
You can do :
In the Dockerfile, adding ENV INDEX_PREFIX myapp-${VERSION}
Adding a script to your entrypoint as
export INDEX_PREFIX=myapp-${VERSION}
In case you can't modify Dockerfile, you can try to :
Get the image entrypoint file from the docker image (ie: /IMAGE-entrypoint.sh) and the image args(ie: IMAGE-ARGS). you can use docker inspect IMAGE.
Override the container command and args in the pod spec using a script.
command:
- '/bin/sh'
args:
- '-c'
- |
set -e
set -x
export INDEX_PREFIX=myapp-${VERSION}
IMAGE-entrypoint.sh IMAGE-ARGS
k8s documentation : https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/
Hope it could help you.

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}