Bootstrap FreeKB - Docker - Restart service using the docker service scale command
Docker - Restart service using the docker service scale command

Updated:   |  Docker articles

Before scaling a service, the docker service ls command to list the services. Let's say you want to restart the foo-service.

~]# docker service ls
ID            NAME         MODE        REPLICAS  IMAGE                                  PORTS
jwwpwkpzkqe0  foo-service  repliacted  1/1       server1.example.com/foo:latest         *:8101->8080/tcp
rof5uwjm749q  bar-service  repliacted  1/1       server1.example.com/bar:latest         *:8155->8104/tcp
v771iegeoxrm  service003   repliacted  1/1       server1.example.com/helloworld:latest  *:8123->8000/tcp

 

The docker service inspect command can be used to determine how many replicas the service has.

docker service inspect foo-service

 

Something like this should be returned. Notice there might be two blocks that contain Replicas, Spec and PreviousSpec. Spec is short for Specification. In this example, the service has 1 replica.

[
    {
        "Spec": {
            "Name": "foo-service",
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            }
        },
        "PreviousSpec": {
            "Name": "foo-service",
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            }
        }
    }
]

 

The docker service scale command can be used to scale the service down to 0 replicas.

~]# docker service scale foo-service=0
foo-service scaled to 0
overall progress: 0 out of 0 tasks 
verify: Service converged

 

You can then scale the service back up. Since the service originally had 1 replica, we'll scale it back up to 1 replica, but it could be scale to 2 or 3, or however many you want. Replicas is the number of containers the service will be running on.

~]# docker service scale foo-service=1
foo-service scaled to 1
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged

 

Once scaled up, you can reiusse the inspect command.

docker service inspect foo-service

 

And something like this should be included in the ouptu. Spec should have now every many replicas you scaled the service up to. Don't be surprised if PreviousSpec has 0 replicas.

[
    {
        "Spec": {
            "Name": "foo-service",
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            }
        },
        "PreviousSpec": {
            "Name": "foo-service",
            "Mode": {
                "Replicated": {
                    "Replicas": 0
                }
            }
        }
    }
]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 215cae in the box below so that we can be sure you are a human.