New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using shell command in docker-compose.yml #4081
Comments
Hi @zkanda , Sorry, this is not something we support. Usually, this is done by setting environment variables and using variable substitution inside the Compose file instead. |
@shin- thanks, I can workaround with environment variables. Also |
@zkanda did you ever get this to work, I tried in my DOCKER_BIN=`which docker` and then in the docker-compose.yml jenkinsmaster:
build: jenkins-master
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ${DOCKER_BIN}:/usr/bin/docker
ports:
- "50000:50000" But I keep getting
So it looks like commands are interpreted as strings in the |
Commands are not expanded by Compose, in |
This would be very useful to have .... |
+1 |
Here's a use case. When setting up Kafka, it needs the IP of the host machine. I have the following script to get that for me: DOCKER_HOST_IP=$(ifconfig | grep 'inet .*br' | sed -E 's/.*inet (.*) netmask.*/\1/') I reference that IP in the env. I can't just run |
+1 |
Would be useful to get the current user, as in: services:
foo:
image: bar:latest
user: ${CURRENT_USER-$(id -u):$(id -g)} There must be other use cases too... By the way, I don't get the fact that |
Yes @davi5e ... What I was trying for:
|
@davi5e another use case is when you want to create a docker volume and link it to your git repository directory. This absolute bath will wary for each developer on the team. |
+1 |
+1 |
This would be very useful to have for setting UID/GID variables as this is pretty much a requirement for using Docker in a development environment (without needed to use scripts to set this into an .env file). |
+1 Could use this to label/tag the images generated by docker compose with the git tag and commit hash: In
In
|
For those doing this in development, if your shell sets the |
+1 |
@con-f-use Doing that will just send the actual command and not the result from my testing, the only hack I've been successful so far is writing a script wrapping docker compose which sets up the environment variable. |
+1 |
TL;DR if you want to export those variables from # set the path of .env file here
ENV_FILE="${ENV_FILE:-local.env}"
while IFS= read -r line; do
export "$line"
done < <( grep --color=never -E -v -e '^#' -e '^[[:space:]]*$' "${ENV_FILE}" ) How to use:
OR
Caveats:
Explanation: here |
Have a look at direnv to automate all that https://direnv.net/
…On Fri, Jun 28, 2019, 12:57 PM Sudarshan Wadkar ***@***.***> wrote:
TL;DR if you want to export those variables from .env file:
# set the path of .env file here
ENV_FILE="${ENV_FILE:-local.env}"while IFS= read -r line; do
export "$line"done < <( grep --color=never -E -v -e '^#' -e '^[[:space:]]*$' "${ENV_FILE}" )
How to use:
- Type it on your command line
- Save it as bash script and source it
Caveats:
- Saving this as a script and running it won't magically export those
variables in your current shell environment. You'll have to source it or do
some fancy eval.
- (Maybe a good thing?) It will overwrite existing variables with same
name in $ENV_FILE
Explanation: here
<https://explainshell.com/explain?cmd=while+IFS%3D+read+-r+line%3B+do+export+%22%24line%22%3B+done+%3C+%3C%28grep+--color%3Dno+-v+-e+%27%5E%23%27+-e+%27%5E%5B%5B%3Aspace%3A%5D%5D*%24%27+%24%7BENV_FILE%7D%29>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4081?email_source=notifications&email_token=AACHRDBQXQZTC5FXTOFL6UTP4XOBJA5CNFSM4CUISSSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYZUTRQ#issuecomment-506677702>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACHRDGJ6FAQBEDLBSRVS6TP4XOBJANCNFSM4CUISSSA>
.
|
Yeah, |
There not really environment variables
<https://en.wikipedia.org/wiki/Environment_variable> if they are part of
the code base and not part of the environment...
…On Fri, Jun 28, 2019 at 2:04 PM Sudarshan Wadkar ***@***.***> wrote:
Yeah, direnv is nice but I am not sure if it will be useful with Docker's
.env file which has simple lines with plain VAR=VAL syntax (as per this
<https://docs.docker.com/compose/env-file/#syntax-rules> link.) The idea
being .env file is committed to the repository and the build script can
run it on my local or docker-compose can run it on the staging/deploy.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4081?email_source=notifications&email_token=AACHRDCEKOMK2VI3A43QBC3P4XV5BA5CNFSM4CUISSSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYZY53A#issuecomment-506695404>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACHRDF3Q3YOZPHUIUZYEPTP4XV5BANCNFSM4CUISSSA>
.
|
Oh, okay. I like to think that the My use case was to use the same I still don't see how |
Another use case is to put a command that generates a password assigning it to env var in the container. I think this is going to be a huge benefit for many security related use cases. |
+1 |
1 similar comment
+1 |
+1, would be hugely useful for setting
|
Yup, that's what I'm having to do now as well, wrapping it with a shell script. |
I have a workaround with direnv https://direnv.net/
Then
Then Inspired from the comment of @mbrevda |
direnv looks very cool @arulrajnet . Not sure how I'm only learning about it's existence now though? |
I use a similar workaround to the one described by @arulrajnet with some additions. I have environment variables that depend on the current state of a git repo, e.g. something like Up to this point, export APP_VERSION=$(git describe --tags) The problem is that the variable gets stale whenever you switch between git branches or commit new changes. If you don't want to remember to run export APP_VERSION=$(git describe --tags)
watch_file .git/HEAD
watch_file .git/refs/heads/$(git branch --show-current) This will reload the environment when you switch branches or commit to the currently checked out branch. You can also apply different behavior for different branches. For example, to add if on_git_branch -r 'feature.*'; then
export APP_VERSION=$(git describe --tags)-feature
else
export APP_VERSION=$(git describe --tags)
fi
watch_file .git/refs/heads/$(git branch --show-current) Note: |
Would be nice to open and tag as a feature request instead of question |
This likely won't get far enough, unfortunately. The problem is that different shells may use different inline commands syntax. For example, bash uses
Then I start my containers as |
Why do you think this is a good compromise? Docker-Compose files are supposed to hide away leaking deployment details from the calling scope; that's the whole purpose of this technology. If I need to inline-specify a variable right before "docker-compose up", I'm adding a lot of unnecessary complexity. Why can't Docker-Compose inline-evaluate environment variables before it passes them to a container? What is so difficult about this? I mean just give it a command as string and run it. The user has to think about whether his console supports the command. Skilled cloud devs capable of figuring out how to do proper IaC should be able to constrain their deployment target to a set of supported host systems and consoles without any problem. That's really not an argument. Btw, you're constraining the deployment as well by using a *.sh file such that it won't run on Windows / Powershell, so basically nothing is gained by your shell script approach; it's just tedious and overly complex 😂 I can see that evaulating a command on the host system can introduce crazy command injection security holes. But for the reasons you've given, it doesn't make any sense to hold the feature back. |
This is something I'd love as well. We have some monorepo-wide settings I'd like to keep in a single shell script, and just The alternative of |
Coming here looking for the same feature :( |
FWIW I use a combination of Taskfile and multiple docker-compose.yml files. My use case sets different environment variables depending on the current arch: version: '3'
tasks:
docker:compose:
desc: Run a docker compose command
summary: |
Sets the base command that is needed to run a docker compose command.
Note that this uses both 'docker-compose.yml' and 'docker-compose-{{ARCH}}.yml' which is determined by the host ARCH.
The order of these inclusion matter and the latter can override the earlier.
https://docs.docker.com/compose/reference/#use--f-to-specify-name-and-path-of-one-or-more-compose-files
internal: true
cmd: >
docker compose
-f docker-compose.yml
-f docker-compose-{{ARCH}}.yml
{{.COMPOSE_COMMAND}}
preconditions:
- sh: docker compose version
msg: Could not connect to docker server! Then I have |
Yes please |
This is something that is really needed to make the |
@bubbleguuum how would you manage shell script portability? Both cross-OS and regarding installed commands on user's workstation? |
I suppose if it has not been done it is because of such complications. I would say that if there is a need for some environment variables whose value is dynamically defined with shell commands, it is not portable anyway if not running on the system it is meant to run on. So you might as well run these commands as part of docker compose to make it easier and self-contained. |
It could be made portable if such a command can be container-ized, as sort of a pre-up hook / init-container / to-be-defined. Would this be useful for you own usage ? |
My usage is quite basic and similar to other posts. It would be for being able to write:
rather than to have to use env variables for these, which works just fine, but an extra step. |
so IIUC the original issue is that you need access to a GPU device, and get into permission issue doing so? |
The |
ok, have you considered using |
I have not looked at the |
The |
ok. In addition to portability consideration, one challenge to address with this approach is that you assume here |
I don't know if this is resolved, but I would find it really convenient if I could just set env variable as: Edit: I tried to follow instruction from: #7843, but it throws error for: |
can't docker compose evaluate the commands on the machine where it runs before sending the configuration to the engine, remote or no? |
Yeah, there's not changeset there and was closed without a merge by the author. I don't think that ever made it in the codebase. |
Hello, is there a way to use shell commands in
docker-compose.yml
file?Here is my use case:
Currently it's giving me this error:
The text was updated successfully, but these errors were encountered: