“docker service” Command Examples

The “docker service” command is a Docker CLI command used to manage services on a Docker daemon. Services are a fundamental concept in Docker Swarm, which is Docker’s native orchestration and clustering solution. Services allow you to define and manage long-running containers as a single entity, enabling you to scale, distribute, and manage applications across a cluster of Docker nodes.

Here are some key aspects of the “docker service” command:

1. Creating a service: You can create a service using the “docker service create” command. When creating a service, you specify parameters such as the image to use, the number of replicas, networking settings, resource limits, and more. For example, you can create a service with three replicas using the following command:

# docker service create --replicas 3 --name my-service my-image:tag

2. Listing services: To see a list of all services running on the Docker Swarm, you can use the “docker service ls” command. It displays information such as the service ID, name, number of replicas, image used, and other relevant details.

3. Inspecting a service: You can retrieve detailed information about a specific service using the “docker service inspect” command. This command provides information such as the service’s configuration, current state, replicas, tasks, and more.

4. Scaling a service: With the “docker service scale” command, you can adjust the number of replicas for a service. Scaling a service allows you to increase or decrease the number of running containers, thereby adjusting the capacity or performance of your application. For example, to scale a service to five replicas, you can use the following command:

# docker service scale my-service=5

5. Updating a service: When you need to update the configuration or image of a service, you can use the “docker service update” command. This command allows you to modify parameters such as the number of replicas, image version, networking, resource limits, and more. Docker Swarm performs rolling updates, ensuring that the service remains available during the update process.

6. Removing a service: To remove a service from the Docker Swarm, you can use the “docker service rm” command, followed by the service’s name or ID. This command stops and removes all containers associated with the service.

The “docker service” command is essential for managing services in a Docker Swarm cluster. It provides a convenient way to create, scale, update, and remove services, enabling you to efficiently manage and deploy applications across a distributed environment.

docker service Command Examples

1. List the services on a docker daemon:

# docker service ls

2. Create a new service:

# docker service create --name service_name image:tag

3. Display detailed information of a space-separated list of services:

# docker service inspect [service_name|ID]

4. List the tasks of a space-separated list of services:

# docker service ps [service_name|ID]

5. Scale to a specific number of replicas for a space-separated list of services:

# docker service scale service_name=count_of_replicas

6. Remove a space-separated list of services:

# docker service rm [service_name|ID]
Related Post