How do you initialize a project in Docker Swarm? How to cancel? How do you update?

Hello everybody,
I want to make a premise. I’ve read that Docker Swarm is used to create a Docker project where the containers don’t physically reside on the same server. I don’t have this requirement, I only have 1 server. I would like to use Docker Swarm because I found some code that does exactly what I need. Basically I’m forced to use Docker Swarm because I’m forced to use this specific script. Unfortunately I have no alternatives. I wrote some code without Docker Compose but it doesn’t work and Traefik’s team can’t fix it. Traefik’s team provides me with a solution project in Docker Swarm that I don’t know how to use.
I would need the following code:

INSTALLATION
1 - Code to create the following docker swarm project containers:

#docker-compose.yml
version: '3.9'

services:
  traefik:
    image: traefik:v2.9
    ports:
      - published: 80
        target: 80
        protocol: tcp
        mode: host
      - published: 443
        target: 443
        protocol: tcp
        mode: host
    networks:
      - proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /root/traefik-certificates:/traefik-certificates
    command:
      --...
    labels:
      - traefik.enable=true
      - ...

  whoami:
    image: traefik/whoami:v1.8
    networks:
      - proxy
    labels:
      - ...

networks:
  proxy:
    name: proxy
    driver: overlay
    attachable: true

UNINSTALL
2 - Code to remove everything from the server (swarm, compose, docker, etc… everything)

UPDATE
3 - Code to update the project in docker swarm (for example add a service like mysql, or add a container for a new subdomain, etc…). I need the code because with Letsencrypt I can only issue a finite number of certificates every week.

When I used Docker Compose I did it like this:

delete-server (to be used in extreme cases or to allocate the server for a new project, it provides the security of starting a new project on clean, something similar to ‘format c:’ on windows)

cd $URL_PROGETTO
docker compose down --volumes
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker container prune -f
docker volume prune -f
docker network prune -f
docker image prune -f
docker system prune -f
docker system prune -a -f
sudo rm -rf $URL_PROGETTO
docker images
docker container ls
docker volume ls
docker network ls

reset (delete all containers of a project and create a new project that uses the same images as the previous project, useful locally because my internet connection sucks)

cd $URL_PROGETTO
docker compose down --volumes
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker container prune -f
docker volume prune -f
docker network prune -f
docker images
docker container ls
docker volume ls
docker network ls

restart (make a small change to a project without exceeding the speed limits of the certificate)

cd $URL_PROGETTO
docker compose up -d --force-recreate
docker images
docker container ls
docker volume ls
docker network ls

setup (install a project by removing any other projects previously present on the server)

cd $URL_PROGETTO
docker compose down --volumes
sudo rm -rf $URL_PROGETTO/volumes/data-letsencrypt/*
sudo mkdir -p $URL_PROGETTO/volumes/data-letsencrypt/
sudo echo '{}' > $URL_PROGETTO/volumes/data-letsencrypt/acme.json
sudo chmod 600 $URL_PROGETTO/volumes/data-letsencrypt/acme.json
sudo rm -rf $URL_PROGETTO/volumes/data-log/*
sudo mkdir -p $URL_PROGETTO/volumes/data-log/
sudo echo '{}' > $URL_PROGETTO/volumes/data-log/traefik.log
sudo chmod 600 $URL_PROGETTO/volumes/data-log/traefik.log
sudo rm -rf $URL_PROGETTO/volumes/data-php-wordpress/*
sudo mkdir -p $URL_PROGETTO/volumes/data-php-wordpress/
docker compose up -d --build
docker images
docker container ls
docker volume ls
docker network ls

uninstall-all (uninstall everything in case of problem without biting the folder with project files)

cd $URL_PROGETTO
export UTENTE_CORRENTE=$(id -u):$(id -g)
docker compose down --volumes
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker container prune -f
docker volume prune -f
docker network prune -f
docker image prune -f
docker system prune -f
docker system prune -a -f
docker images
docker container ls
docker volume ls
docker network ls

If I have to use Docker Swarm what should I add in my code?
I tried some code but I get this error:

Error response from daemon: Could not attach to network proxy: rpc error: code = PermissionDenied desc = network proxy not manually attachable

If I need to add the Docker Swarm code I used I can.
If I’m in the wrong section of the site, I’m sorry, tell me where to write.
thank you

It seems like you want to learn everything about Docker Swarm from one answer. I think everything you need in the beginning is in the Docker swarm tutorial

Just follow the links in the “What’s Next” sections or click the links in the navigation bar.

One thing I can tell you now that you need the docker stack command instead of docker compose

For using docker stack and comparing with docker compose this link can be useful as well

1 Like