Docker Community Forums

Share and learn in the Docker community.

Running docker compose from Golang

#1

Hello,

I’m experiencing a weird behaviour by docker-compose.
I have a folder ~/test that contains a docker compose YAML file along with a .env that stores some variables used by the former.
Now, if launch the command docker-compose --project-directory ~/test -f ~/test/compose.yml up -d from terminal, it works pretty well. Hurray! Finally, I have a static directory where to find the .env.
My problem is that the above command does not work if I wrap it into a Golang script like:

cmd := exec.Command("docker-compose", "-f", composePath, "--project-directory", envPath, "up", "-d")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if cmd.Run(); err != nil {
	return err
}

Compose returns an error saying that variables are not set:

WARNING: The TEST_PORT variable is not set. Defaulting to a blank string.
ERROR: The Compose file '~/test/compose.yml' is invalid because:
services.testservice.ports is invalid: Port ranges don't match in length

It seems like the --project-directory flag is not passed or not read for some reason, or it is overridden.
I really don’t know what to think about this. Any help is appreciated.

UPDATE:
The same error is returned also from a python script. Just to make sure this is something related with docker and not with a programming language.