disable env variable substitution in docker compose env files - docker

I updated docker and notice a significant change of behavior when using my env files in docker-compose
I have the following:
env_file:
- ./file.env
and, in file.env
property=${something}
Before updating, doing echo $property in my running container would produce the string ${something}. Which was what I wanted.
Now (docker 19.03.12), $property is empty because docker is trying to find $something from my system env variables.
What caused this change of behavior? Is there a way to disable it or work around it?

Related

vscode dev container environment variables not exposing to docker-compose

My docker-compose.yml looks like this
version: "3.8"
services:
vscode:
volumes:
- ..:/workspace:cached
- $SSH_AUTH_SOCK:/ssh-agent
- /var/run/docker.sock:/var/run/docker.sock
environment:
- SSH_AUTH_SOCK=/ssh-agent
Problem is that vscode does not want to give any kind of possibility to do an equivalent of docker-compose run --env ... thus I'm left with
WARNING: The SSH_AUTH_SOCK variable is not set. Defaulting to a blank string.
Is there any way for me to expose my variables from my host to the dev container without using an .env file or anything like that?
I have opened an issue on github yesterday. The outcome is that if you use WSL, then you need to export this variable through your ~/.profile or ~/.bash_profile.
We are using a non-interactive login shell to probe the environment variables in WSL and use these as the "local" variables.
https://github.com/microsoft/vscode-remote-release/issues/5806
I just had a similar issue so this may help.
I would run the following:
export VARIABLE=VALUE
sudo docker-compose up
The problem was that I was exporting the environment variable as my user and then running docker-compose as sudo so it wouldn't have the same environment variables.
This can be solved by either adding yourself to the docker group so you can run without sudo
or exporting the variable as root(not recommended)

How to create dynamic env variable in Dockerfile?

I am using Docker composé ver 1.5 or 6 and nginx image. I want to parameterize the nginx.config. To do that I want to create a var from $(basename some path). But the problem is Docker does not accept dynamic vars like that in env part of the Dockerfile. Another problem is that I also cannot map those variables on build run as Docker compose does not accept dynamic, scripted vars. How to overcome that issue?
From nginx
ENV myvar=$(basename /)
This is impossible to build the image.
The other way was
ARG myvar
ENV myvar2=myvar
But my version of Docker compose allows only to set
Environment: myvar=$(basename mypathinthevolume/)
That also does not seem to work

How to set environment variables in a docker container after it starts

I need to set some environment variables in a docker container after it starts. When the docker starts env X gets value, then I want to set env Y with a value which is the first part of the value X with this command:
Y=$(echo $X | cut -d'#' -f 1)
Is there any way to do this?
I tried ENTRYPOINT and CMD in the Dockerfile, but it doesn't work.
The docker will be deployed on a Kubernetes cluster, and I also tried to set them in the config.yaml file but it doesn't work either.
You are on the right track that you would have to handle this by either CMD or ENTRYPOINT, because you want it to be dynamic and derived from existing data. The specifics would depend on your container and use case though.
You can use the ENV command in your dockerfile like below:
ENV PORT 8080
Source and more info - https://vsupalov.com/docker-build-time-env-values/

Variable substitution in docker-compose.yml file when running docker-compose with sudo

I'm currently trying to use variable substitution in a docker-compse.yml file. This file contains the following:
jenkins:
image: "jenkins:${JENKINS_VERSION}"
external_links:
- mongodb:mongo
ports:
- 8000:8080
The image below shows what happens when I try to start everything up.
As you can see, docker-compose shows a warning saying that the variable is not set. I suspect this is caused due to the use of sudo to start docker-compose. My setup (a Jenkins docker container which has access to docker and docker-compose via volume mounts) currently requires the use of sudo. Would it be better to stop docker requiring sudo, or is there another way to fix this without changing the current setup?
sudo -E preserve the user environment when running the command. It should do what you want.

Pass local environment variable to docker

This might be a general docker problem but my use-case is docker-compose:
I need to pass my machine's hostname as an environment variable to a container which would be created through docker-compose. That particular container uses a Dockerfile and uses "cmd" flag in it. The command in "cmd" uses this environment variable. Can someone please help?
If you can rewrite the Dockerfile in order to include the hostname in an ENV instruction, as mentioned in issue 1136, that will work:
FROM centos:latest
ENV BLABLA hello
RUN echo $BLABLA
# outputs: "hello"
If this is purely a runtime environment variable value, it should be included in docker 1.9 with issue 14634: Builder - Build-time argument passing (e.g., HTTP_PROXY):
In the meantime, the OP 208rishabh mentions issue 2091 for docker compose, answered by dnephin:
environment or env_file should both work:
You can leave the value blank, and it will take the value from the current environment:
environment:
HOST_HOSTNAME:
That allows:
HOST_HOSTNAME=$(hostname) docker-compose up

Resources