How to name/alias a network in Docker swarm Stack

7.3k views Asked by At

I would like to give an alias to my network in docker-swarm stack file.

Currently, the stack file looks like

version: '3'

networks:
  mybridge:

services:
  web:
    restart: always
    build: ./web
    image: shivanand3939/web
    expose:
      - "8000"
    volumes:
      - ./output:/usr/src/app/static
    command: /usr/local/bin/gunicorn -w 2 -b :8000 --access-logfile - classifierv2RestEndPoint_ridge_NB:create_app()   
    networks:
      mybridge:
        aliases:
          - web
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  nginx:
    restart: always
    build: ./nginx/
    image: shivanand3939/nginx
    ports:
      - "80:80"
    volumes:
      - /www/static
    networks:
      - mybridge
    deploy:
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  viz:
    image: dockersamples/visualizer
    ports:
      - 8080:8080/tcp
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    deploy:
      placement:
        constraints: [node.role == manager]

when I run this file

docker stack deploy -c docker-stack.yml classifierbot

I get the following output

Creating network classifierbot_default
Creating network classifierbot_mybridge
Creating service classifierbot_viz
Creating service classifierbot_web
Creating service classifierbot_nginx

The network name is changed to classifierbot_mybridge from mybridge. Thus the 2 services nginx and web are not able to communicate among themselves.

So, my question is how can I give an alias to my network can also be referred as mybridge

Edit:

One way is add

networks:
  mybridge:
    external:
      name: mybridge

and create mybridge outside the stack file but it defeats the purpose right? as I am unable to do everything in a single stack file

1

There are 1 answers

0
Jonathan Komar On

I am assuming by "alias" a network, you mean "give a service an alias" within a network. Because the alias of your network is given successfully. In your case, "mybridge".

Giving a service a network alias (what we usually think of as domain name) You are normally supposed to be able to do this in your services: block

 nginx:
    networks:
      mybridge:
        aliases: # https://github.com/moby/moby/issues/38066
          - nginx

This is due to a bug that has not yet been resolved. There are tickets open for this:

For now, I use docker service to do this until it gets supported by the multi-service variant, docker stack up|deploy.

--hostname $SERVICE_NAME
--name $SERVICE_NAME