admin管理员组

文章数量:1200790

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!

Share Improve this question edited Jan 21 at 17:42 Михаил Васильев asked Jan 21 at 15:48 Михаил ВасильевМихаил Васильев 436 bronze badges 4
  • Remove the /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 any docker 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:32
  • Thanks for answering, yes, I have already removed sh. There is no question of security. – Михаил Васильев Commented Jan 21 at 16:40
  • In addition to @david-maze feedback, consider capturing stderr: go.dev/play/p/cr5UQwUbqbh – DazWilkin Commented Jan 21 at 16:48
  • To add to the list of suggestions, use docker compose, not docker-compose (or in this case "docker", "compose") – BMitch Commented Jan 21 at 19:04
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: goGolang exec command dockercomposeStack Overflow