YES, YOU CAN DO IT.
Use exec
and env
command to implement this scene.
Test Fixture in Docker
docker run -it --rm alpine:3.10
Run command in container:
exec env spring.application_name=happy-variable-name ${SHELL:-/bin/sh}
Verify environment variables:
HOSTNAME=bd0bccfdc53b
SHLVL=2
HOME=/root
spring.application_name=happy-variable-name
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
Use ps aux
to verify PID not changed
PID USER TIME COMMAND
1 root 0:00 /bin/sh
12 root 0:00 ps aux
Use python
to verify environemnt variable
apk add python
python -c 'import os; print(os.environ["spring.application_name"])'
OUTPUT is happy-variable-name
.
What happen?
- Shell call builtin exec
- Shell builtin exec call syscall.exec create process 'env' to replace current shell
- env process call syscall.execvp create process '/bin/sh' to replace env process
Another way
If you are using docker, you can set variable in Dockerfile
FROM busybox
ENV xx.f%^&*()$#ff=1234
If you are using kubernetes, you can set variable by ConfigMap
test.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: foo-config
data:
"xx.ff-bar": "1234"
---
apiVersion: v1
kind: Pod
metadata:
name: foobar
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: foo-config
restartPolicy: Never
Deploy pod kubectl apply -f test.yaml
Verify kubectl logs foobar
output:
xx.ff-bar=1234
ConfigMap allow '-', '_' or '.'
-D
command line option), so it works now. Obviously the program looks in both variable sets without telling me. But still I am curious about which environment variable names are allowed. – Sinhalesecom_example_fancyproperty
andCOM_EXAMPLE_FANCYPROPERTY
. – Recuperate