1

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.

2
  • 1
    That Cmd: slice probably needs an sh -c wrapper 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. Commented Apr 30, 2024 at 13:03
  • Thanks a lot it worked. Had been stuck on this for days now. Though it was also throwing the same error without "&&" too but I may have missed its image build on stag server, not sure. But prefixing with sh -c helps it execute successfully. Commented Apr 30, 2024 at 13:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.