docker-compose.yml syntax leads to error in runtime - postgresql

Is there any difference between these two configs:
Config 1:
...
environment:
- POSTGRES_NAME='postgres'
- POSTGRES_USER='postgres'
- POSTGRES_PASSWORD='postgres'
- POSTGRES_PORT='5432'
Config 2:
...
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_PORT=5432
Because, when I try to docker-compose up with Config 1 it throws an error (django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"), and it works fine with Config 2. What is wrong with docker-compose.yml?

In a YAML scalar, the entire string can be quoted, but quotes inside a string are interpreted literally as quotes. (This is a different behavior than, say, the Bourne shell.)
So in this fragment:
environment:
- POSTGRES_NAME='postgres'
The value of environment: is a YAML list. Each list item contains a YAML string. The string is the literal string POSTGRES_NAME='postgres', including the single quotes. Compose then splits this on the equals sign and sets the variable POSTGRES_NAME to the value 'postgres', including the single quotes.
There's two ways to work around this. One is to not quote anything; even if there are "special characters" after the equals sign, it will still be interpreted as part of the value.
environment:
- CONTAINING_SPACES=any string
- CONTAINING_EQUALS=1+1=2
- CONTAINING_QUOTES="double quotes outside, 'single quotes' inside"
A second way is to use the alternate syntax for ENVIRONMENT that's a YAML mapping instead of a list of strings. You'd then use YAML (not shell) quoting for the value part (and the key part if you'd like).
environment:
POSTGRES_NAME: 'postgres' # YAML single quoting
CONTAINING_SPACES: any string # YAML string rules don't require quotes
START_WITH_STAR: '*star' # not a YAML anchor
'QUOTED_NAME': if you want # syntactically valid

...
environment:
POSTGRES_NAME='postgres'
POSTGRES_USER='postgres'
POSTGRES_PASSWORD='postgres'
POSTGRES_PORT='5432'

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

Ansible Strange Type Conversion When Using Inventory Files vs. Setting Vars on command line [duplicate]

I have an ansible playbook, which first initializes a fact using set_fact, and then a task that consumes the fact and generates a YAML file from it.
The playbook looks like this
- name: Test yaml output
hosts: localhost
become: true
tasks:
- name: set config
set_fact:
config:
A12345: '00000000000000000000000087895423'
A12352: '00000000000000000000000087565857'
A12353: '00000000000000000000000031200527'
- name : gen yaml file
copy:
dest: "a.yaml"
content: "{{ config | to_nice_yaml }}"
Actual Output
When I run the playbook, the output in a.yaml is
A12345: 00000000000000000000000087895423
A12352: 00000000000000000000000087565857
A12353: '00000000000000000000000031200527'
Notice only the last line has the value in quotes
Expected Output
The expected output is
A12345: '00000000000000000000000087895423'
A12352: '00000000000000000000000087565857'
A12353: '00000000000000000000000031200527'
All values should be quoted.
I cannot, for the life of me, figure out why only the last line has the value printed in single-quotes.
I've tried this with Ansible version 2.7.7, and version 2.11.12, both running against Python 3.7.3. The behavior is the same.
It's because 031200527 is an octal number, whereas 087895423 is not, thus, the octal scalar needs quoting but the other values do not because the leading zeros are interpreted in yaml exactly the same way 00hello would be -- just the ascii 0 followed by other ascii characters
If it really bothers you that much, and having quoted scalars is obligatory for some reason, to_nice_yaml accepts the same kwargs as does pyyaml.dump:
- debug:
msg: '{{ thing | to_nice_yaml(default_style=quote) }}'
vars:
quote: "'"
thing:
A1234: '008123'
A2345: '003123'
which in this case will also quote the keys, but unconditionally quotes the scalars

Parameter name containing special characters on Helm chart

In my Helm chart, I need to set the following Java Spring parameter name:
company.sms.security.password#id(name):
secret:
name: mypasswd
key: mysecretkey
But when applying the template, I encounter a syntax issue.
oc apply -f template.yml
The Deployment "template" is invalid: spec.template.spec.containers[0].env[79].name: Invalid value: "company.sms.security.password#id(name)": a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
What I would usually do is defining this variable at runtime like this:
JAVA_TOOL_OPTIONS:
-Dcompany.sms.security.password#id(name)=mypass
But since it's storing sensitive data, obviously I cannot log in clear the password.
So far I could only think about defining an Initcontainer as a workaround, changing the parameter name is not an option.
Edit: So the goal is to not log the password neither in the manifest nor in the application logs.
Edited:
Assign the value from your secret to one environment variable, and use it in the JAVA_TOOL_OPTIONS environment variable value. the way to expand the value of a previously defined variable VAR_NAME, is $(VAR_NAME).
For example:
- name: MY_PASSWORD
valueFrom:
secretKeyRef:
name: mypasswd
key: mysecretkey
- name: JAVA_TOOL_OPTIONS
value: "-Dcompany.sms.security.password#id(name)=$(MY_PASSWORD)"
Constrains
There are some conditions for kuberenetes in order to parse the $(VAR_NAME) correctly, otherwise $(VAR_NAME) will be parsed as a regular string:
The variable VAR_NAME should be defined before the one that uses it
The value of VAR_NAME must not be another variable, and must be defined.
If the value of VAR_NAME consists of other variables or is undefined, $(VAR_NAME) will be parsed as a string.
In the example above, if the secret mypasswd in the pod's namespace doesn't have a value for the key mysecretkey, $(MY_PASSWORD) will appear literally as a string and will not be parsed.
References:
Dependent environment variables
Use secret data in environment variables

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.

Is there a way to use square bracket on environment variable in kubernetes?

I have an environment variable like as follows that works with docker-compose.yaml in relation to a springboot container:
- name: pool.config[0].Number
value: "2"
This works completely fine in docker-compose.yaml but not in yaml - it keeps giving error:
a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or'.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
In docker-compose it's fine with the square brackets in the key name, but in kubernetes deployment spec it's not permitting. How can I work through this?
see here and here
You should be able to use following binding for your case: POOL_CONFIG_0__Number