docker / libcompose

*Unmaintained/Deprecated* An experimental go library providing Compose-like functionality
https://godoc.org/github.com/docker/libcompose
Apache License 2.0
585 stars 191 forks source link

Error when scaling a service #480

Closed deviantony closed 6 years ago

deviantony commented 7 years ago

It seems that there is a bug related to the ability to scale a service using libcompose.

I built libcompose using the following steps:

Now, when using the following docker-compose.yml file:

version: "2"
services:
  myservice:
    image: nginx

I can execute the up action against the stack without any issue (note: I've enabled debugging in libcompose):

$ ./libcompose -f docker-compose.yml -p myproject up -d

WARN[0000] Note: This is an experimental alternate implementation of the Compose CLI (https://github.com/docker/compose) 
Creating network "myproject_default" with driver ""
DEBU[0015] Launching action for myservice               
DEBU[0015] Project [myproject]: Starting project        
INFO[0015] [0/1] [myservice]: Starting                  
DEBU[0015] Found 0 existing containers for service myservice 
DEBU[0015] Creating container myproject_myservice_1 &service.ConfigWrapper{Config:(*container.Config)(0xc420244140), HostConfig:(*container.HostConfig)(0xc4202d8000), NetworkingConfig:(*network.NetworkingConfig)(nil)} 
DEBU[0015] [0/1] [myservice]: Created container name=myproject_myservice_1 
DEBU[0015] Checking existing image name vs id: sha256:c246cd3dd41d35f9deda43609cdeaa9aaf04d3658f9c5e38aad25c4ea5efee10 == sha256:c246cd3dd41d35f9deda43609cdeaa9aaf04d3658f9c5e38aad25c4ea5efee10 
DEBU[0015] Going to decide if recreate is needed         ForceRecreate=false NoRecreate=false outOfSync=false
DEBU[0015] Starting container                            container.ID=321c1f94ad6f026f1473ec1bf570e06facf8af0563a8d31540c5a597d6324e6c container.Name="/myproject_myservice_1"
DEBU[0015] [0/1] [myservice]: Started container name=/myproject_myservice_1 
INFO[0015] [1/1] [myservice]: Started      

And somehow, an error is raised sometimes (not all the times, I'd say 80% of time) when I try to scale a service:

First try, scale the service to 2, OK:

$ ./libcompose -f docker-compose.yml -p myproject scale myservice=2

WARN[0000] Note: This is an experimental alternate implementation of the Compose CLI (https://github.com/docker/compose) 
INFO[0000] Setting scale myservice=2...                 
DEBU[0000] Creating container myproject_myservice_2 &service.ConfigWrapper{Config:(*container.Config)(0xc4200963c0), HostConfig:(*container.HostConfig)(0xc4202d6000), NetworkingConfig:(*network.NetworkingConfig)(nil)} 
DEBU[0000] Created container 96db9ec6599633656f0b6903489e8c7f027f8b232e9fed74d38e35fd733ac127: /myproject_myservice_2 
DEBU[0000] [0/1] [myservice]: Created container name=myproject_myservice_2 
DEBU[0000] Found 2 existing containers for service myservice 
DEBU[0000] Starting container                            container.ID=96db9ec6599633656f0b6903489e8c7f027f8b232e9fed74d38e35fd733ac127 container.Name="/myproject_myservice_2"

Second try, downscale the service to 1, KO:

$ ./libcompose -f docker-compose.yml -p myproject scale myservice=1

WARN[0000] Note: This is an experimental alternate implementation of the Compose CLI (https://github.com/docker/compose) 
INFO[0000] Setting scale myservice=1...                 
DEBU[0000] Found 1 existing containers for service myservice 
Failed to set the scale myservice=1: Error response from daemon: endpoint with name myproject_myservice_2 already exists in network myproject_default

Third try, scale the service to 2, OK:

$ ./libcompose -f docker-compose.yml -p myproject scale myservice=2

WARN[0000] Note: This is an experimental alternate implementation of the Compose CLI (https://github.com/docker/compose) 
INFO[0000] Setting scale myservice=2...                 
DEBU[0000] Creating container myproject_myservice_3 &service.ConfigWrapper{Config:(*container.Config)(0xc420232140), HostConfig:(*container.HostConfig)(0xc420039400), NetworkingConfig:(*network.NetworkingConfig)(nil)} 
DEBU[0000] Created container 6bf5e2ec9b82af295f486a13ceb9c75764b9225de45d6151f70919d87300171f: /myproject_myservice_3 
DEBU[0000] [0/1] [myservice]: Created container name=myproject_myservice_3 
DEBU[0000] Found 2 existing containers for service myservice 
DEBU[0000] Starting container                            container.ID=6bf5e2ec9b82af295f486a13ceb9c75764b9225de45d6151f70919d87300171f container.Name="/myproject_myservice_3"

Fourth try, scale the service to 3, KO:

$ ./libcompose -f docker-compose.yml -p myproject scale myservice=3

WARN[0000] Note: This is an experimental alternate implementation of the Compose CLI (https://github.com/docker/compose) 
INFO[0000] Setting scale myservice=3...                 
DEBU[0000] Creating container myproject_myservice_4 &service.ConfigWrapper{Config:(*container.Config)(0xc420234500), HostConfig:(*container.HostConfig)(0xc4204b6c00), NetworkingConfig:(*network.NetworkingConfig)(nil)} 
DEBU[0000] Created container 66a1263d563c67338ece4ee41cb4b51fdcc7c2c27d424232fd121c6981df6969: /myproject_myservice_4 
DEBU[0000] [0/1] [myservice]: Created container name=myproject_myservice_4 
DEBU[0000] Found 3 existing containers for service myservice 
DEBU[0000] Starting container                            container.ID=66a1263d563c67338ece4ee41cb4b51fdcc7c2c27d424232fd121c6981df6969 container.Name="/myproject_myservice_4"
Failed to set the scale myservice=3: Error response from daemon: endpoint with name myproject_myservice_2 already exists in network myproject_default

One thing that disturbs me is that starting from the third try, it seems that libcompose lost the count of replicas inside the service. On the third try, I'm trying to scale the service to 2 replicas and it is creating a container name myproject_service_3 (should be myproject_service_2). Same for the fourth try, it should be creating a container named myproject_service_3 and instead it is creating a container named myproject_service_4.

After diving a bit in the code, I'm pretty sure that the following error:

Failed to set the scale myservice=3: Error response from daemon: endpoint with name myproject_myservice_2 already exists in network myproject_default

Is related to https://github.com/docker/libcompose/blob/master/docker/service/service.go#L337 and specifically to https://github.com/docker/libcompose/blob/master/docker/service/service.go#L377

deviantony commented 7 years ago

This issue seems to be related to the API_VERSION. I did not found a way to set the API_VERSION used internally by the client with the libcompose cli tool (default API_VERSION is hardcoded to "1.20", my engine is using "1.30").

When using the scale feature programmatically with a client with a correct API_VERSION, this works well.

deviantony commented 7 years ago

Actually, I just reproduced this even we specifying the APIVersion. Will investigate a bit more.