Docker swarm
Docker Swarm is a container orchestration engine. At a high level, it takes multiple Docker Engines running on different hosts and lets you use them together. The usage is simple: declare your applications as stacks of services, and let Docker handle the rest. Services can be anything from application instances to databases.
Create a new swarm cluster with the initialization command below. Replace the manager_private_ip with the private IP of the host you are creating the cluster on
#docker swarm init --advertise-addr=
When the swarm starts successfully, we can see a docker swarm join command in the output like the example below. Use the command on our other nodes to join them to the swarm.
#docker swarm join --token SWMTKN_token manager_private_ip
When all nodes are connected, use the following command on the manager to check that the nodes are ready to accept tasks
# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
5bdtzjfa0qw799e6npb4dcp18jf * node1 Ready Active Leader 20.11.3
create a service
After creating the manager and worker node in swarm mode, we can create a service and run it inside the container.
*Run the following command on the manager node to create an Nginx service with 2 replicas.
# docker service create --replicas 2-p 80:80 --name web_server nginx
* check the status of the service, by giving the following command
#docker service ls
*We can also see the status of the service and how it is being distributed to different nodes by using the following command
#docker service ps service_name
Access the service
we can access the service by hitting any of the manager or worker nodes. curl to any of the Docker Machine IPs manager1 or worker1 or hit the URL (HTTP://) in the browser. we should be able to get the standard NGINX Homepage.
Scale-up and down
We can increase or decrease the number of containers for a particular service at any time by using the following command
*scale Up
#docker service scale web_server=5
*scale down
#docker service scale web_server=2
*List all the services running in swarm mode
#docker service ls
*List all the running containers
#docker ps
*Inspect node and remove a particular service
#docker node inspect — pretty worker_node
#docker service rm service_name
*Rolling update
#docker service update --image : web
*Inspect service
#docker service inspect service name
*See all containers and images (running, stopped, or dead once)
#docker ps -a
#docker image ls
*Remove unused Images, containers, etc
#docker system prune
#docker system prune -a
Image related commands
*List images
#docker images -a
*Remove all image
#docker rmi $(docker images -a -q)
*Remove single image
#docker rmi Image Image
*List dangling image
#docker images -f dangling=true
*Remove dangling image
#docker images purge
Container related commands
*List all containers
#docker ps -a
*Remove container
#docker rm ID_or_Name ID_or_Name
*Remove a container upon exit
#docker run --rm image_name
*List all exited containers
#docker ps -a -f status=exited
*Remove all exited containers
#docker rm $(docker ps -a -f status=exited -q)
*List containers using more than one filter
#docker ps -a -f status=exited -f status=created
*Remove containers using more than one filter
#docker ps -a -f status=exited -f status=created
*Stop and remove all containers
#docker stop $(docker ps -a -q)
#docker rm $(docker ps -a -q)
Volume related command
*List dangling volumes
#docker volume ls -f dangling=true
*Remove dangling volumes
#docker volume prune
*Remove a container and its volume
#docker rm -v container_name
*List all volumes
#docker volume ls
Network related commands
*List all network
#docker network ls
*Get particular network
#docker network ls | grep "bridge"
*Remove network
#docker network rm $(docker network ls | grep "bridge" | awk '/ / {print $1 }')
*Inspect network
#docker network inspect -v bridge
*To see what network(s) your container is on, assuming our container is called cont1
#docker inspect cont1 -f "{{json .NetworkSettings.Networks }}"
*To disconnect your container from the first network (assuming your first network is called net1)
#docker network disconnect net1 cont1
*Then to reconnect it to another network assuming it's called net2
#docker network connect test2 cont1
*To check if two containers (or more) are on a network together
#docker network inspect net1 -f "{{json .Containers }}"
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post