Docker Config Reference (docker stop / docker swarm / docker service)

We go over numerous examples of using the docker stop / docker swarm / docker service examples.

Docker Config Reference (docker stop / docker swarm / docker service)

Examples of the Docker Stop Command

The docker stop command gracefully terminates one or more running containers by sending a SIGTERM signal, allowing processes to shut down cleanly before a potential forcible kill (SIGKILL) after a timeout period. This operation preserves the container's state for later restarts, making it suitable for controlled halts during maintenance or scaling activities.

docker stop mycontainer

This command stops the running container identified as "mycontainer" by issuing a SIGTERM signal to its main process. It provides a grace period (default 10 seconds) for orderly shutdown, after which a SIGKILL is sent if necessary, ensuring minimal data corruption while transitioning the container to a stopped state.

docker stop --time 30 dbcontainer

Utilizing the --time flag set to 30 seconds, this command stops "dbcontainer" with an extended grace period. It allows database processes additional time to commit transactions or flush caches, reducing the risk of data loss in persistent storage scenarios.

docker stop webapp

This command stops the "webapp" container, halting its web server processes gracefully. It is particularly useful in web applications to avoid abrupt disconnections, preserving session states where configured.

docker stop container1 container2

This command stops multiple running containers, "container1" and "container2", in parallel. It enables batch management, facilitating coordinated shutdowns in multi-container environments without individual invocations.

docker stop --signal SIGINT interactivecontainer

By specifying --signal SIGINT, this command stops "interactivecontainer" using an interrupt signal instead of SIGTERM. It mimics keyboard interrupts, suitable for interactive shells or processes expecting specific termination handling.

docker stop worker

This command stops the "worker" container, which might be processing background tasks. It ensures tasks are paused safely, allowing for resumption via restart without full recreation.

docker stop --time 5 cachecontainer

This command stops "cachecontainer" with a shortened 5-second timeout. It accelerates shutdown for in-memory caches like Redis, where rapid termination is acceptable due to persistence mechanisms.

docker stop api-service

This command stops the "api-service" container, gracefully handling API requests during shutdown. It supports zero-downtime strategies when combined with load balancers or orchestration tools.

docker stop --signal SIGQUIT debugcontainer

Using --signal SIGQUIT, this command stops "debugcontainer" with a quit signal, potentially triggering core dumps for debugging. It aids in forensic analysis of problematic processes.

docker stop --help

This command displays the help documentation for docker stop, detailing available options, flags, and usage syntax. It serves as a reference for understanding command behaviors and parameters.

Examples of the Docker Swarm Command

The docker swarm command manages Docker Swarm mode, a native clustering and orchestration tool for Docker. It includes subcommands for initialization, joining, leaving, updating, and inspecting clusters, enabling distributed container management with features like service replication and load balancing.

docker swarm init

This command initializes a new Swarm cluster on the current node, designating it as the manager. It generates a join token for workers and sets up the Raft consensus for leadership, forming the basis for multi-node orchestration with automatic service scheduling.

docker swarm join --token token worker-node-ip:2377

This command joins the current node as a worker to an existing Swarm cluster using the provided token and manager IP on port 2377. It integrates the node into the cluster for task execution, enhancing scalability without management responsibilities.

docker swarm leave

This command removes the current node from the Swarm cluster, provided it is not the last manager. It gracefully drains tasks and disconnects, allowing for node maintenance or decommissioning while preserving cluster integrity.

docker swarm update --autolock=true

This command updates the Swarm configuration to enable manager autolocking, requiring an unlock key for restarts. It enhances security by protecting against unauthorized access to the Raft log, which contains sensitive cluster state.

docker swarm ca --rotate

This command rotates the Swarm's root CA certificate, issuing a new one while revoking the old. It strengthens security by periodically refreshing certificates, mitigating risks from long-term key exposure in production clusters.

docker swarm unlock-key --rotate

This command rotates the unlock key for a locked Swarm manager, generating a new key while invalidating the previous one. It supports key management practices, ensuring secure restarts in autolocked environments.

docker swarm join-token worker

This command retrieves or rotates the join token for worker nodes in the Swarm. It facilitates secure addition of workers by providing a time-limited token, maintaining cluster access control.

docker swarm init --advertise-addr 192.168.1.100

This command initializes a Swarm with the --advertise-addr flag specifying the IP for join advertisements. It ensures correct network discovery in multi-interface setups, optimizing cluster formation.

docker swarm leave --force

With the --force flag, this command forcibly leaves the Swarm, even if the node is a manager or has tasks. It overrides safety mechanisms for urgent removals, though it may disrupt cluster quorum.

docker swarm --help

This command displays help documentation for the docker swarm command, including subcommands and options. It provides a comprehensive reference for cluster management functionalities.

Examples of the Docker Service Command

The docker service command manages services in Docker Swarm mode, abstracting replicated containers for scalability and resilience. It includes subcommands for creation, updating, scaling, inspection, and removal, enabling declarative management of distributed applications.

docker service create --name web nginx:latest

This command creates a service named "web" using the "nginx:latest" image with one replica by default. It schedules the task on available nodes, providing built-in load balancing and service discovery in the Swarm.

docker service ls

This command lists all services in the Swarm, displaying ID, name, mode, replicas, image, and ports. It offers a high-level view of deployed applications for status monitoring and resource oversight.

docker service rm web

This command removes the "web" service, terminating its replicas and associated resources. It ensures clean decommissioning, freeing nodes for other tasks without residual overhead.

docker service scale web=3

This command scales the "web" service to three replicas, distributing additional tasks across the cluster. It adjusts capacity dynamically, supporting load handling in variable demand scenarios.

docker service update --image nginx:1.25 web

This command updates the "web" service to use "nginx:1.25", performing a rolling update to minimize downtime. It replaces tasks incrementally, ensuring continuous availability.

docker service inspect web

This command inspects the "web" service, outputting detailed JSON including configuration, endpoints, and update status. It supports in-depth analysis for compliance or troubleshooting.

docker service ps web

This command lists tasks for the "web" service, showing node, ID, name, image, and status. It enables examination of task placement and health in the distributed environment.

docker service create --publish 8080:80 --name api customapi:latest

This command creates the "api" service from "customapi:latest", publishing port 8080 to container port 80. It exposes the service via Swarm's ingress routing for external access.

docker service update --replicas 5 --env-add DEBUG=true web

This command updates "web" to five replicas and adds the "DEBUG=true" environment variable. It modifies runtime parameters without full redeployment, enhancing flexibility.

docker service --help

This command displays help for the docker service command, detailing subcommands and options. It serves as a reference for advanced service management features.