I am trying to execute certain command inside of docker swarm containers from another go service. But when executing it, I am getting the exit code 126 while inspecting. Here is the code used for reference
func inspectDockerContainer(cli *client.Client, containerId string) error{
        execConfig := types.ExecConfig{
            Cmd: []string{"nginx -t && nginx -s reload"},
        }
        execResponse, err := cli.ContainerExecCreate(context.Background(), containerID, execConfig)
        if err != nil {
            fmt.Println("Error creating container exec", err)
            return err
        }
        err = cli.ContainerExecStart(context.Background(), execResponse.ID, types.ExecStartCheck{})
        if err != nil {
            fmt.Println("Error creating container exec start", err)
            return err
        }
        hijackedResp, err := cli.ContainerExecAttach(context.Background(), execResponse.ID, types.ExecStartCheck{})
        if err != nil {
            fmt.Errorf("Error attaching container %w", err)
            hijackedResp.Close()
            return err
        }
        var hasErr bool
        for {
            execInfo, err := cli.ContainerExecInspect(context.Background(), execResponse.ID)
                        ...
                        if execInfo.ExitCode != 0 {
                                fmt.Println("Finished with exitCode: ", execInfo.ExitCode)
                break
                        }
        }
               return nil
}
func inspectService(cli *client.Client, serviceName string) error{
    // Get the list of tasks (containers) for the specified service
    tasks, err := cli.TaskList(context.Background(), types.TaskListOptions{Filters: filters.NewArgs(filters.Arg("service", serviceName))})
    if err != nil {
        return fmt.Errorf("error while listing docker tasks %w", err)
    }
    // Execute a command in each container of the service
    for _, task := range tasks {
        fmt.Println("Task Status: ", task.Status.State)
        if task.Status.State != "running" {
            continue
        }
        containerID := task.Status.ContainerStatus.ContainerID
                inspectDockerContainer(containerID)
    }
    return nil
}
It works fine when running these services on docker compose and passing command to nginx container. But fails with exit code of 126 when running the same command in docker swarm containers.
Is there some restriction that i am not aware of that blocks the access of swarm container through docker engine api. Both nginx service and golang service are running as root users, golang service has access to docker daemon and all the services are sharing the same overlay networks.
Cmd:slice probably needs ansh -cwrapper to be able to understand the&&operator, or it needs to be run in multiple commands. (I'm surprised this apparently works locally.) It might be cleaner to delete and recreate the container, though; I wouldn't expect you to need exec-type commands in normal operation.