---
title: Swarm Introduction
tags: Swarm, docker-compose
description: swarm introduction
---
# Swarm Introduction
When deploy applications with docker, there always have lots of service to manage. Needing some software infrastructure tools to manage services like Kubernetes, swarm, so on…
if services need GPU resources, it need to build on kubernetes infrastructure. On the contrary, If services don’t need GPU resource. THe fastest way to deploy service is to use swarm. It support building docker overlay network between multi-server. The following will introduce swarm.
## Swarm Usage
swarm use docker-compose to deploy service, use the following commnad to deploy service in node.
### Swarm construction
```
$ docker run -it --rm \
--name swarmpit-installer \
--volume /var/run/docker.sock:/var/run/docker.sock \
swarmpit/install:1.9
```
After installation is done, swarm has web portocol to manage the containers. The default port is 888. Use the url to enter swarmpit [http://localhost:888](http://localhost:888)
#### Swarm manager
Initial swarm manger service, add server ip behind — advertise-addr
```
$ docker swarm init --advertise-addr 10.8.1.101
```
After initial swarm will output a token like below:
```
Swarm initialized: current node (lb3r7xwa32wryl4de6n3kn9xt) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0pf0h3jy3wtemhqyxj012ud34b2colqp06miwlbjxsqruvfdsfsdkfpdsofkdsopfksdopfdskw 10.8.1.101:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
```
#### Swarm join worker node
```
$ docker swarm join --token SWMTKN-1-4y22cau9ejhrppdlf2g5y7dn6ibltq67ccvoiw2xf3yoj5i5ii-69yg43tece4d5sbwl1a1ftmu9 10.11.97.71:2377
```
#### Swarm leave node
```
$ docker swarm leave
```
#### Swarm deploy service
```
$ docker stack deploy --compose-file docker-compose.yaml chatting_room
```
##### docker-compose.yaml
```
version: '3.9'
networks:
net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.80.0.0/16
services:
visualbackend:
image: qctuser/visual-backend
container_name: visualbackend
ports:
- "8080:80"
restart: always
command: python3.6 manage.py runserver
deploy:
placement:
constraints:
- node.role == manager
```
#### Swarm remove service
```
$ docker service rm chatting_room
```
## 參考
* http://www.sankedan.com/detail/9/6051.html
* https://www.cnblogs.com/skyflask/p/11212452.html
## Thank you! :dash:
You can find me on
- GitHub: https://github.com/shaung08
- Email: a2369875@gmail.com