title: AWS Using ECS-Optimized AMI with Docker Containers in ECS
body: <p>I have used an extensive amount of time building the kind of deployment setup I'm going to describe next. I've gone through most of the documentation on ECS and documented an exhaustive list of learning resources.</p>
  
  <p><em>However, it seems I'm still missing some vital piece of this puzzle with Docker on AWS ECS...</em></p>
  
  <p>In development, we are using Docker Compose for defining a set of containers as a local full-stack cluster. It is a really convenient tool for working with containers locally.</p>
  
  <p>Our aim is to use Docker Compose approach for deploying this cluster into Amazon AWS ECS.</p>
  
  <p>ECS supports Compose file format up to version 3.0. Most notably there is no support for using local files as the build context, so an image must be referenced.</p>
  
  <p>What I'm currently confused with:</p>
  
  <p>The <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html" rel="nofollow noreferrer">official doc states</a> that instances running in ECS should have <em>Amazon ECS container agent</em>, <em>Docker daemon</em>, and <em>ecs-init</em> installed and configured. As such, for the base image they are recommending:</p>
  
  <blockquote>
    <p>The Amazon ECS-optimized AMIs are preconfigured with these
    requirements and recommendations. We recommend that you use the Amazon
    ECS-optimized Amazon Linux 2 AMI for your container instances unless
    your application requires a specific operating system or a Docker
    version that is not yet available in that AMI.</p>
  </blockquote>
  
  <p><strong>Confusion point 1:</strong> However, I've found no examples in the official docs on how to actually build and run Docker containers based on these optimized AMIs... There are just about two Dockerfile examples, <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html" rel="nofollow noreferrer">here</a> and <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cmd-ecs-cli-compose-start.html" rel="nofollow noreferrer">here</a>, which are both building from public non-AMI images?</p>
  
  <p>I've found some resources, like <a href="https://app.pluralsight.com/library/courses/docker-production-using-amazon-web-services" rel="nofollow noreferrer">here</a> (<a href="https://github.com/mixja/Docker-on-Amazon-Web-Services/tree/master/Chapter06/packer-ecs" rel="nofollow noreferrer">source</a>) and <a href="https://gist.github.com/henrybravo/8e77edf41a1aecc1763f55d1df5fdf95" rel="nofollow noreferrer">here</a>, which show how to use Packer with its <a href="https://www.packer.io/docs/builders/amazon-ebs.html" rel="nofollow noreferrer">EC2 AMI Builder (EBS backed)</a> for building a custom AMI based on these ECS optimized AMIs.</p>
  
  <p>I've set up this process myself with some minor modifications, and it is working. I have my own custom images showing up under AWS EC2 AMIs.</p>
  
  <p><strong>Confusion point 2:</strong> However, I seem unable to reference these AMI images in my dockerfiles or docker compose files, as they are not stored in ECR... Am I correct (or having misconfig)?</p>
  
  <p>Packer does have a <a href="https://www.packer.io/docs/post-processors/docker-push.html" rel="nofollow noreferrer">post-processor for docker push</a> which has support for ECR. But it does not work with EC2 AMI Builder:</p>
  
  <pre><code>Post-processor failed: Unknown artifact type: mitchellh.amazonebs
  Can only import from docker-import and docker-tag artifacts.
  </code></pre>
  
  <p>Sure, I would not actually need to have the base AMI stored in ECR... But I would need to be able to use it as the image to build my custom service Docker images from, and to store those release images in ECR. Then reference those images in the docker compose file.</p>
  
  <p>I'm aware that turning to additional automation tools like Ansible, Terraform, or AWS Code* services could get me past this point. But I've been trying to follow the official docs and gather information from various sources while trying to keep the setup minimal, and do not yet fully understand the issue I'm facing.</p>
  
  <p><strong>Confusion point 3:</strong> What is the recommended way for building and running Docker containers in ECS with Amazon ECS-optimized Amazon Linux 2 AMIs?</p>
  
  <p>And what am I possibly missing here...? </p>