Docker stack deploy do not use networks

HI,

I’m having problem with network. In my swarm env I created a network

docker network create -d overlay dls_network

here is my compose

version: "3.8"
services:
  mysql:
    image: mysql:8.3.0
    ports:
      - "3306:3306"
    environment:
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    networks:
      - dls_network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 30s
      timeout: 10s
      retries: 5

  keycloak:
    image: quay.io/keycloak/keycloak:24.0.3
    environment:
      - KEYCLOAK_ADMIN=${KEYCLOAK_ADMIN}
      - KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD}
      - KC_DB=${KC_DB}
      - KC_DB_URL_HOST=${KC_DB_URL_HOST}
      - KC_DB_URL_DATABASE=${KC_DB_URL_DATABASE}
      - KC_DB_USERNAME=${KC_DB_USERNAME}
      - KC_DB_PASSWORD=${KC_DB_PASSWORD}
      - KC_HEALTH_ENABLED=${KC_HEALTH_ENABLED}
    ports:
      - "8080:8080"
    command: start-dev
    networks:
      - dls_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health/ready"]
      interval: 30s
      timeout: 10s
      retries: 5

networks:
  dls_network:
    external: true

when I run

docker stack deploy -c docker-compose.yml dls-sevices

if I inspect the network I see Containers: Null and keycloak doesn’t find mysql service.

Have you tried to create the Docker network inside compose, maybe compare to simple Traefik Swarm example.

Note that you are playing a dangerous game. You use Swarm, so I assume multiple nodes. You don’t persist your data in MySQL, so it will be gone when you re-create the container. But even volumes and bind-mounts are not shared across nodes. So if the container is re-scheduled to a different node, all data is gone.