Search code examples
dockergodocker-composeexec

Golang exec command docker-compose


I have a Linux operating system (Ubuntu).

I want to run docker-compose on command from the frontend. For this I wrote a service that has access to the docker-compose.yml file. .env file is located in the same directory.

My function:

func runCommand() error {
    output, err := exec.Command("/bin/sh", "-c", "docker-compose", "up", "--build", "-d").Output()
    if err != nil {
        return err
    }

    c.logger.Info().Msgf("run docker-compose - %s", string(output))

    return nil
}

I change the number of replicas in the .env file. And run the function. But it does not work as I want. I tried different command formats, for example:

exec.Command("docker-compose", "up", "--build", "-d")

This command works for me, but only once, the process seems to be held.

I want to execute the command(docker-compose up -d --build) and reuse the function to handle requests from the frontend.

P.S. After long tests I found that docker-compouse uses an old version of the .env file. Although the .env file changes as a result of the program's operation.

Solution: it turned out that the command exec.Command() uses environment variables from the program for docker-compouse. Forcing the flag --env-file .env for docker-compouse did not give the desired result.

Thanks for any help!


Solution

  • P.S. After long tests I found that docker-compouse uses an old version of the .env file. Although the .env file changes as a result of the program's operation.

    Solution: it turned out that the command exec.Command() uses environment variables from the program for docker-compouse. Forcing the flag --env-file .env for docker-compouse did not give the desired result.