title: Refused connection to RabbitMQ when using docker link
body: <p>I have a microservices application which has two services and a rabbit mq used as a message queue for communication between them. Now, I want to deploy them on docker. I have the following code in the <code>docker-compose.yml</code> file:</p>
  
  <p>version: "3"
  services:</p>
  
  <pre><code>  rabbitmq:
      build: ./Rabbit
      hostname: "rabbitmq"
      container_name: "rabbitmq"
      environment:
        RABBITMQ_ERLANG_COOKIE: "cookie"
        RABBITMQ_DEFAULT_USER: "user"
        RABBITMQ_DEFAULT_PASS: "pass"
        RABBITMQ_DEFAULT_VHOST: "/"
      ports:
        - "15672:15672"
        - "5672:5672"
      # labels:
      #   NAME: "rabbit1"
      volumes:
      - "/opt/rabbitmq:/var/lib/rabbitmq"
  
    service1:
      build: ./service1
      deploy:
        replicas: 5
        restart_policy:
          condition: on-failure
        resources:
          limits:
            cpus: "0.1"
            memory: 50M
      ports:
        - "8181:80"
      depends_on:
        - rabbitmq
      links:
       - rabbitmq
      networks:
        - webnet
  </code></pre>
  
  <p>So, here I build the RabbitMQ image in a container and then link this container to the container of <code>service1</code>. Since <code>service1</code> one is an ASP.NET Core Web API, I use the following setup to connect to the message queue:</p>
  
  <pre><code>//Establish the connection
              var factory = new ConnectionFactory
              {
                  HostName = "rabbitmq",
                  Port = 5672,
                  UserName = "user",
                  Password = "pass",
                  VirtualHost = "/",
                  AutomaticRecoveryEnabled = true,
                  NetworkRecoveryInterval = TimeSpan.FromSeconds(15)
              };
  </code></pre>
  
  <p>But when I try to run <code>docker-compose up</code>, I receive the following error message:</p>
  
  <blockquote>
    <p>Unhandled Exception:
    RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the
    specified endpoints were reachable --->
    RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
    ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException:
    No such device or address</p>
  </blockquote>
  
  <p>Maybe I have a mistake in the <code>HostName</code> but I am not sure how to correct it.</p>