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
docker stack deploy
in 1.13 doesn't load .env
file as docker-compose up
does
#29133
Comments
docker stack deploy
in 1.13 doen't load .env
file as docker-compose up
doesdocker stack deploy
in 1.13 doesn't load .env
file as docker-compose up
does
This is by design. The We can discuss adding this for a future release, but I don't know if it's really the best option. |
The It's not possible to use Otherwise, we have to prefix |
I've just noticed this. In my particular case, I'm was expecting to use the version: '3'
volumes:
data:
driver: local
networks:
backend:
driver: overlay
services:
rabbitmq:
image: rabbitmq:${EXCHANGE_RABBITMQ_TAG}
volumes: [ "data:/var/lib/rabbitmq" ]
logging: { driver: gelf, options: { gelf-address: "udp://0.0.0.0:12201" } }
networks: [ "backend" ]
ports: [ "15672:15672", "5672:5672" ]
environment:
RABBITMQ_DEFAULT_USER: ${EXCHANGE_RABBITMQ_USER}
RABBITMQ_DEFAULT_PASS: ${EXCHANGE_RABBITMQ_PASS}
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints:
- node.labels.queue-host == true While this Compose file will be checked in on Git, the I've followed the whole stuff/history of the *.dab vs compose files, and I feel you guys are trying to avoid something - or proposing a better solution - but I lost track of the whole discussion... |
Hello All, Docker version: v1.13.0-rc4 I am getting error : Ignoring unsupported options: build, Does it mean it will not create build from Dockerfile ? And also getting same error for network_mode: Ignoring unsupported options: network_mode. Below is command: Many thanks in advance. |
@akhildangore please don't comment on issues with questions that are not directly related. The |
Are there any cases/discussion in favor of/against supporting this behavior on |
@vovimayhem no decision was made yet on |
I've just found that discussion. Excellent! |
I am using a bash function as a workaround. You may adapt it to your needs until the dsd() {
stack=${1:-${PWD##*/}} # by default, the name of the cointaining folder
compose_file=${2:-docker-compose.yml}
if [ ! -f $compose_file ]; then
echo "Misses compose file: $compose_file" >&2
return 1
fi
# execute as a subcommand in order to avoid the variables remain set
(
# export variables excluding comments
[ -f .env ] && export $(sed '/^#/d' .env)
# Use dsd your_stack your_compose_file to override the defaults
docker stack deploy --compose-file $compose_file $stack
)
} |
For those subscribing to this issue; secrets support for docker-compose files will be included in the 1.13.1 release, which should be not too far in the future |
@whoan I'm using this as a workaround: env $(cat .env | grep ^[A-Z] | xargs) docker stack deploy --compose-file docker-compose.yml [STACK_NAME] That way, variables don't get stuck on the terminal window |
As per many I hit this issue and found this. A solution that has worked for me is as follows:
Create startmystack.sh script containing the stack command required:
Variable picked up using source from .env, exported and then are available for substitution in the compose file for Swarm. |
@andyfinch Alternatively:
or:
|
This is my aproach until I found this thread:
#!/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) |
There is a workaround if you don't want to remember custom commands to do variable substitution in loadenvs() {
local envfile="${1:-.env}"
set -a && . $envfile && set +a
} The script does the following:
Then the usage is very simple:
Please note that if you are using |
thank you so much, thats works perfect ! |
It's worth noting, BTW, that the new, non-hyphenated So, even though I've switched to |
On a side note, |
In the docs, it's still listed as a 'technical preview' but some of the talks at DockerCon gave the impression it was now more mainstream than that. They were (confusingly) calling it Compose v2, but that's nothing to do with the version numbers in the docker-compose.yml file. The basic idea was to rewrite docker-compose in Go and include it in the main CLI, partly for speed but partly so it always had access to the latest facilities in the engine/CLI. There are still some differences, but I've been using it all day for the last couple of days on my Mac with no issues other than those I mentioned above. More info in this talk, if it's accessible: Quentin |
And with that the env-file works? Unfortunately, the page does not really explain how to use it though. I still get
Indicating that it is either not working or not installed correctly. Replacing the variables all over the compose file that were before niceley and uniquely handled by the .env file seems a bit inconvenient and it creates incompatibilities. |
The translation hack is useless and error prone. |
In docker compose V2 this approach won't work causing It seems to me that the creators did it on purpose to even take away these hacks we had 🥇 😸 |
For people who would like to supply a custom .env file like .env.prod you can use the following: (Combining @kinghuang and @vovimayhem answers)
To do this with a local registry, don't forget to initialize a local registry with a given port (f.e.5000) and tag your services in the docker-compose.yml file with: image: 127.0.0.1:5000/<parent_folder>_<service_name> Then build with docker-compose and push them to the registry as explained here: https://docs.docker.com/engine/swarm/stack-deploy/ |
I wrote a compose-spec (compose v2) compliant docker-cli plugin replacement of |
If anyone is looking for a powershell script to replace the environment variables in a docker-compose.yaml file, here your go: # Define the path to env file as parameter as well as the path to docker-compose.yaml
param(
[string]$envFilePath = ".env",
[string]$dockerComposeFilePath = "docker-compose.yaml"
)
# Check if the .env file exists
if (Test-Path $envFilePath) {
# Read the contents of the .env file
$envVariables = Get-Content $envFilePath
# Loop through each line in the .env file
foreach ($line in $envVariables) {
# Split the line into variable name and value
$parts = $line -split "="
[System.Environment]::SetEnvironmentVariable($parts[0], ($parts[1..($parts.Length - 1)] -join "="), [System.EnvironmentVariableTarget]::Process)
}
} else {
Write-Host "The .env file does not exist."
}
# Input string containing references to environment variables
$inputString = Get-Content $dockerComposeFilePath -Raw
# Regular expression pattern to match environment variable references
$pattern = '\${([a-zA-Z_][a-zA-Z0-9_]*)}'
# Replace environment variable references with their values
$outputString = [regex]::Replace($inputString, $pattern, {
param($match)
$envValue = [System.Environment]::GetEnvironmentVariable($match.Groups[1].Value)
if ($envValue -ne $null) {
return $envValue
} else {
return $match.Value
}
})
# Output the modified string
Write-Output $outputString You can even pass the name of the .env and docker-compose.yaml file (with -envFilePath and -dockerComposeFilePath) |
This is my approach |
@msk010 Doesn't work for me:
|
Here is what we do, it works very well on Docker Swarm cluster.
Hope it helps. |
Here is another workaround: docker compose -f stack-deploy.yml config | grep -v '^name' | docker stack deploy -c - |
To test
docker stack deploy --compose-file
function, I load one of my sampledocker-compose.yml
:In the
image
section of servicenginx
andphp
, I used${DOCKER_USER}
to get the docker id from environment variables. And if I usedocker-compose up
, it will load.env
file as default envvar files, which content is:However, if I use
docker stack
to deploy thisdocker-compose.yml
, I will got following errors:$ docker stack deploy --compose-file docker-compose.yml lnmp Ignoring unsupported options: build Creating network lnmp_frontend Creating network lnmp_backend Creating network lnmp_default Creating service lnmp_php Error response from daemon: rpc error: code = 3 desc = ContainerSpec: "/lnmp-php:v1.2" is not a valid repository/tag
As you can see, as
docker stack deploy
command didn't load.env
file, the${DOCKER_USER}
was replaced by empty string, which cause image name become invalid.If
.env
file was loaded, the final image name should betwang2218/lnmp-php:v1.2
.The environment substitution is actually working, if I run the command this way:
And we can verify it's working by
docker service inspect
command:The image name is
twang2218/lnmp-php:v1.2
, which is correct.I tested this feature on Digtial Ocean droplet, which installed docker 1.13.0-rc2 via
docker-machine
.Here is the version:
$ docker version Client: Version: 1.13.0-rc2 API version: 1.25 Go version: go1.7.3 Git commit: 1f9b3ef Built: Wed Nov 23 06:32:39 2016 OS/Arch: linux/amd64 Server: Version: 1.13.0-rc2 API version: 1.25 Minimum API version: 1.12 Go version: go1.7.3 Git commit: 1f9b3ef Built: Wed Nov 23 06:32:39 2016 OS/Arch: linux/amd64 Experimental: false
Here is the
docker info
:The text was updated successfully, but these errors were encountered: