Running docker-compose Commands from Go: Handling Dynamic .env Files Correctly
Автор: vlogommentary
Загружено: 2026-01-05
Просмотров: 0
Описание:
Learn how to execute docker-compose commands from a Go application properly and ensure it uses an updated .env file, avoiding stale environment variables.
---
This video is based on the question https://stackoverflow.com/q/79375050/ asked by the user 'Михаил Васильев' ( https://stackoverflow.com/u/18979015/ ) and on the answer https://stackoverflow.com/a/79375424/ provided by the user 'Михаил Васильев' ( https://stackoverflow.com/u/18979015/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Golang exec command docker-compose
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction
When running docker-compose commands programmatically from a Go service, you may encounter issues where the .env file changes are not reflected. This often happens because the Go process environment overlays or caches environment variables, leading docker-compose to use stale configuration.
The Problem
You want to execute the equivalent of docker-compose up -d --build from your Go backend repeatedly, for example in response to frontend requests. You modify the .env file before each run. However:
Docker Compose appears to use an old version of .env.
Using exec.Command("docker-compose", "up", "--build", "-d") works only once; subsequent runs don't behave as expected.
The --env-file .env flag for docker-compose doesn't fix the issue.
Why This Happens
exec.Command inherits the environment variables of the Go process. If environment variables referenced by docker-compose were loaded into Go's environment when the program started, docker-compose may use those instead of reading the updated .env file on disk.
Hence, even if you change .env on disk, docker-compose might pick variables from the environment of your Go program, making it seem like changes had no effect.
How to Fix It
1. Clear or Set the Environment Explicitly
Before running docker-compose, override the environment for the command so it does not inherit stale variables:
[[See Video to Reveal this Text or Code Snippet]]
By clearing cmd.Env, docker-compose will load .env fresh from the file.
2. Provide .env File Path Correctly
If your .env file is not in the current working directory, also consider setting the Dir field:
[[See Video to Reveal this Text or Code Snippet]]
So docker-compose executes in the directory where it expects the .env file.
3. Use CombinedOutput Instead of Output
This captures both stdout and stderr, essential for debugging.
4. Avoid Using /bin/sh -c
Passing docker-compose and its arguments directly to exec.Command is more reliable and secure than invoking a shell.
Summary
The key is to control environment variables when executing docker-compose commands from Go.
Clear or explicitly set cmd.Env so docker-compose reads the updated .env file.
Set the working directory if needed to ensure docker-compose uses the correct context.
Sample Final Code
[[See Video to Reveal this Text or Code Snippet]]
Using this method, you can safely modify the .env file and repeatedly run docker-compose commands through your Go backend, with updated environment settings applied every time.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: