title: AWS ECS container networking, communication between services and within containers
body: <p>I am having trouble understanding the point of AWS implementation of service discovery in ECS when using bridge mode, and in general a path forward to (relatively basic) container networking, despite the numerous AWS blog posts on the subject.  </p>
  
  <p>Service discovery seems to me about solving dynamically generated containers accessibility (in tasks), so that, similar to docker user defined networks, I can access different tasks on a cluster with a predefined canonical host name.name-space, in a VPC.<br>
  I've made sure in the VPC that:</p>
  
  <pre><code>DNS hostnames: Enabled
  DNS resolution: Enabled
  </code></pre>
  
  <p>When service discovery is defined when using bridge mode:  </p>
  
  <ol>
  <li>it still tacks on a dynamic portion to the name i did not specify. </li>
  </ol>
  
  <pre><code>      {
             "Name": "my-service.my-namespace.",
              "Type": "SRV",
              "SetIdentifier": "4b46cb82ba434dasdb163c1f06ca5c083",
              "MultiValueAnswer": true,
              "TTL": 60,
              "ResourceRecords": [
                  {
                      "Value": "1 1 27017 4b46cb82ba434dasdb163c1f06ca5c083.my-service.my-namespace."
                  }
              ],
              "HealthCheckId": "862bd287-2b41-43ac-8442-a3d27042482b"
          },
  </code></pre>
  
  <p>So i need to manually look up the record each time a service is created, or updated. I cannot <code>dig
  my-service.my-namespace</code> for example, that record does not exist.   </p>
  
  <p>And:<br>
   2. every time the service is updated, the record is regenerated...</p>
  
  <p>To get here i need to do:   </p>
  
  <pre><code>$ aws servicediscovery list-namespaces
  </code></pre>
  
  <pre><code>$ aws route53 list-resource-record-sets --hosted-zone-id $ZONE_ID --region us-east-1
  </code></pre>
  
  <p>My application currently accesses task hosts via injected environment variables, but if the record refreshes on every service update, this is a non starter. All documentation/forums I've come across seem to say, create some kind of dynamic SRV lookup workaround (seems hackish?) or just switch to awsvpc mode, but then why is this service available at all under bridge/host?<br>
  Clearly I'm missing something fundamental.  </p>
  
  <p>In addition  I'm using dynamic port mapping. If I don't, things like rolling updates fail with <em>port already in use</em> errors. Similarly attempting to run a new instance of a task via scheduling creates the same error.   </p>
  
  <p>I can connect within a given docker container in a task with the internal private DNS of the instance, i.e. ip-172-31-52-141.ec2.internal, but here I'm outside of the VPC (?) i.e. now I need to be specifying the dynamically mapped port. So this is a non starter as well. </p>
  
  <p>All of this sits behind a public ALB (for dynamic port resolution etc), and this has been working fine, requests from outside AWS resolve correctly to the target groups / targeted services.  </p>
  
  <p>If I switch to awsvpc mode, and enable service discovery,  I can have multiple tasks/services communicate privately.<br>
  However what if I want to have multiple services communicate, but additionally a single service/task might house multiple docker containers, (e.g. a localized redis cache). I cannot specify the 'link' for these containers without the network mode being 'bridge' again.   </p>
  
  <p><strong>here's the TLDR question:</strong>   </p>
  
  <p>I have 2 tasks and a service associated with each task.  There may be multiple instances of each task, therefore ports need to be dynamic. In each task i have 2 containers.<br>
  What is the general approach here for allowing different services to communicate via a predefined host.namespace dns resolution, and have the containers inside each task communicate with each other?  </p>
  
  <p>Apologies for the long post, but as a novice to ECS/AWS, I'm really struggling here ;)<br>
  Any feedback or advice is really appreciated.</p>