I followed this guide to install Portainer.

Install Portainer as a local container

# the name of the data volume
export PORTAINER_DATA="portainer_data"
# the name of the container
export PORTAINER_NAME="portainer"
# create the data volume
docker volume create "$PORTAINER_DATA"
# start the container
docker run -d -p 9000:9000 --name $PORTAINER_NAME --restart always -v /var/run/docker.sock:/var/run/docker.sock -v $PORTAINER_DATA:/data portainer/portainer

Install Portainer as a swarm service

# the name of the service
export PORTAINER_NAME="portainer"
# create the service
docker service create \
    --name $PORTAINER_NAME \
    --publish 9000:9000 \
    --constraint 'node.role == manager' \
    --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \
    portainer/portainer \
    -H unix:///var/run/docker.sock