-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Feature request: docker stack deploy pass environment variables via cli options #939
Comments
From @AkihiroSuda on March 11, 2018 14:16 CLI is out of the scope of this repo. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@dhanvi Thanks for the issues. That said, I have a small question about the feature request
I can see two different features requested here :
@marcel-steinbach-mpf @jeanadrien @mallchin it would be helpful for us to know which one is it 👼 🙏 |
@vdemeester I am just looking for the option I am assuming that the variable in here |
@vdemeester's question was because there are two "levels" of environment variables:
For example, taking this version: "3.6"
services:
one:
image: "nginx:alpine"
environment:
foo: "bar"
SOME_VAR:
baz: "${OTHER_VAR}"
labels:
some-label: "$SOME_VAR"
two:
image: "nginx:alpine"
environment:
hello: "world"
world: "${SOME_VAR}"
labels:
some-label: "$OTHER_VAR" env SOME_VAR="I am some var" OTHER_VAR="I am other var" docker stack deploy -c docker-compose.yml envie Inspecting environment variables on service docker service inspect --format='{{ json .Spec.TaskTemplate.ContainerSpec.Env}}' envie_one Shows: [
"SOME_VAR=I am some var",
"baz=I am other var",
"foo=bar"
] And its labels: docker service inspect --format='{{ json .Spec.TaskTemplate.ContainerSpec.Labels}}' envie_one Shows: {
"com.docker.stack.namespace": "envie",
"some-label": "I am some var"
} Inspecting environment variables on service docker service inspect --format='{{ json .Spec.TaskTemplate.ContainerSpec.Env}}' envie_two [
"hello=world",
"world=I am some var"
] And its labels: docker service inspect --format='{{ json .Spec.TaskTemplate.ContainerSpec.Labels}}' envie_two Shows: {
"com.docker.stack.namespace": "envie",
"some-label": "I am other var"
} My take on thisEnvironment variables that are substituted while processing the docker compose file are already possible;
What's not supported (yet?) is the use of an What I don't think should be done is add an
What if I want an environment variable to be set on service(s), but don't store it's value in the docker-compose file?Use variable substitusion, and set the environment-variable in your shell (having support for an version: "3.6"
services:
one:
image: "nginx:alpine"
environment:
SOME_VAR: Now, deploy the stack using either: export SOME_VAR=some-value
docker stack deploy -c docker-compose.yml foobar Or
What if I want additional environment variables to be set (in some situations)For example: during development, I want to set some additional environment variables, but in production, I don't want those set (or vice-versa). Use an "override" file, by deploying multiple docker compose files:
version: "3.6"
services:
one:
image: "nginx:alpine"
environment:
somevar: "somevalue"
two:
image: "nginx:alpine"
version: "3.6"
services:
one:
environment:
debug: "1"
two:
environment:
development: "yes" Specify both files when deploying the stack:
And see that the additional environment variables are set; docker service inspect --format='{{ json .Spec.TaskTemplate.ContainerSpec.Env}}' mystack_one
["debug=1","somevar=somevalue"]
docker service inspect --format='{{ json .Spec.TaskTemplate.ContainerSpec.Env}}' mystack_two
["development=yes"] |
Having said the above; I'm interested to hear what the exact use-case is that people here are looking for. Perhaps you can explain what you want to use this for, and show examples how it will be used. |
My use case is the first one you describe:
Specifically, I have two files
Or,
Within those
As long as the user remembers to source the file, it works great. It would be a little nicer to have support for multiple |
Thanks for the additional information! Okay, so looks like the desired feature would be to (for a start) make From moby/moby#29133 (comment), I see that @dnephin was open to discuss that option, but looks like he had some reservations;
Some questions, as I know there are various limitations to the current
In addition to the above, I think it would be useful to print an informational message that docker read variables from that file (so that it doesn't catch a user by surprise). |
I'm busy with GDPR right now but will revisit this next week. I had a specific use case and will see how it fits in with the above. I expect setting environment variables on the command line might work for me but will confirm shortly. |
I have a slightly different scenario:- I am using a standard docker-compose.yml file to define my stack. I wish to build the containers via "docker-compose build" and then deploy into my swarm via "docker stack deploy".
The result of the build is a suitably tagged image. What I would like to happen is for the same docker-compose.yml file to support environment variable substitution via the call to "docker stack deploy" when deploying my stack such that the correct images are deployed. My workaround for now is to set those environment variables via the shell, but it would be good to see a way to set these on the command line call. Happy to take advice if there is a better or more Docker compliant way of achieving this. |
I'm deploying from CircleCI and have the same need as @gbjbaaha. Unfortunately it not as easy as exporting the variable to the environment because in CircleCI the docker environment is remote, so I'd have to ssh in and export them there. This requires a lot of setup because again the machine where the docker commands are executed is remote, see setup_remote_docker |
This comment has been minimized.
This comment has been minimized.
I'm also using a tag like @gbjbaaha , in the @gbjbaaha if I understood you correctly, replacing a
So, this: Hmm maybe the script could update the version number directly in the |
Yes, variable substitution via environment variables set in the shell as suggested by @kajmagnus works correctly. |
Ok thanks @gbjbaaha for the info :- ) |
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
There is this work around |
Another use case: How to force a container's (task) environment variables to receive its values from the environment variables of the host in which it is running? (This is for each task in a "docker stack deploy" running in several swarm nodes)
This service is to be deployed unto two nodes of a docker swarm using docker stack deploy
But the expressions How could I pass to the containers in node A and B a different local dns configuration (/etc/hosts) depending on the node itself? Is there another approach to solve this problem? Which approach would you use? |
Good workaround, but will not work in cases docker-compose is not installed, only docker is |
Hey man, did you manage to find an approach for your use case? |
My entire workflow is failing at this point with external networks defined on the
I really don't know how to transition from docker-compose to Swarm, where you set a development and production scenarios. |
I just read about secrets but I want to define not secret configs from |
My workaround:
#!/bin/sh
export $(cat .env) > /dev/null 2>&1;
docker stack deploy -c docker-compose.yml ${1:-STACK_NAME} . deploy.sh <stack_name> (stack_name optional if previously defined in .env) |
Nope :( |
How can I pass an environment variable for the image registry from In my following example, the value
(even setting the
When I start this: All I get is: This seems to be due to the So why is does variable replacement works in |
Using this compose file: version: '3.7'
services:
docker-test:
image: $REGISTRY/nginx:alpine
environment:
- app.secret=$APP_SECRET And a stack deploy with the REGISTRY=docker.io APP_SECRET=hello docker stack deploy -c docker-compose.yaml mystack
docker service inspect --format='{{json .Spec.Labels}}' mystack_docker-test
{"com.docker.stack.image":"docker.io/nginx:alpine","com.docker.stack.namespace":"mystack"}
docker service inspect --format='{{json .Spec.TaskTemplate.ContainerSpec.Env}}' mystack_docker-test
["app.secret=hello"] |
But why then does |
The (docker compose unfortunately also has a top-level |
From @dhanvi on March 10, 2018 18:54
Few people might have a use case to pass environment variables with a flag like -e.
docker stack deploy -c file.yml -e name=value stack_name
Labels: area/cli kind/feature
Copied from original issue: moby/moby#36554
The text was updated successfully, but these errors were encountered: