What I am doing Wrong?

Hi,

I have a go micro service running in a container that starts and stops some stateful service containers based on some business logic. I am using docker swarm api to orchestrate service placement in the swarm. Everything works as expected, but when a service is removed intentionally using RemoveService api (explained below), the service is stopped but not removed from service list, if i open portainer dashboard i see the service is still listed in service list with scale 0.

What i am doing wrong?

I have following two API's that wraps go API of docker swarm services.

  1. StartService API
  2. RemoveService

// region Service API func (docker *Docker) StartService(name string, labels map[string]string, image string, command []string, env []string, placement []string, networks []string, autoRestart bool) (string, error) { // create a new docker client dockerClient, err := client.NewEnvClient() // check if everything is ok if err != nil { return "", err } // create service annotations serviceAnnotations := swarm.Annotations{ Name: name, Labels: labels, } // create a container specification for the container containerSpec := swarm.ContainerSpec{ Image: image, Command: command, Env: env, } // create a restart policy for the service maxAttempts := uint64(1) restartPolicy := &swarm.RestartPolicy{ MaxAttempts: &maxAttempts, } if autoRestart { restartPolicy.Condition = swarm.RestartPolicyConditionAny } else { restartPolicy.Condition = swarm.RestartPolicyConditionNone } // create a placement policy for the service placementPolicy := &swarm.Placement{} if len(placement) > 0 { placementPolicy.Constraints = placement } // build the service task specification taskSpec := swarm.TaskSpec{ ContainerSpec: containerSpec, RestartPolicy: restartPolicy, Placement: placementPolicy, } // build service specification swarmServiceSpec := swarm.ServiceSpec{ Annotations: serviceAnnotations, TaskTemplate: taskSpec, } // update service network specification for _, network := range networks { networkConfig := swarm.NetworkAttachmentConfig{ Target: network, } swarmServiceSpec.Networks = append(swarmServiceSpec.Networks, networkConfig) } // build service options swarmServiceOpt := types.ServiceCreateOptions{} if config.GetConfig().IsSet("registry_user") && config.GetConfig().IsSet("registry_server") && config.GetConfig().IsSet("registry_token") { swarmServiceOpt.EncodedRegistryAuth = BuildRegistryAuth() } // create a background context and start the service dockerContext := context.Background() service, err := dockerClient.ServiceCreate(dockerContext, swarmServiceSpec, swarmServiceOpt) // get the service id serviceID := "" if err == nil { serviceID = service.ID } // return the error if any return serviceID, err } func (docker *Docker) RemoveService(id string) error { // create a new docker client dockerClient, err := client.NewEnvClient() // check if everything is ok if err != nil { return err } // create a background context and remove the service dockerContext := context.Background() return dockerClient.ServiceRemove(dockerContext, id) } // endregion 

submitted by /u/kunaldawn
[link] [comments]
Source: Reddit