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!
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!
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.
/bin/sh -c
arguments: they're unnecessary here, cause any options to the command to be lost, and introduce a serious security issue if they're present. If you can run anydocker
command at all, you can all but trivially root the host system, so think carefully about whether you actually need to do this in the program, and if you do, be very careful with anything security-related. – David Maze Commented Jan 21 at 16:32docker compose
, notdocker-compose
(or in this case"docker", "compose"
) – BMitch Commented Jan 21 at 19:04