Back

Docker compose and aws fargate

AWS Fargate is a new service that has caught a lot of attention from the development community. The ability to host any docker container in a serverless manner has huge applications, and I wanted to look into how I could possibly use it at work.

Scope

My end goal would be to use Fargate for staging environments for our customers, and developers. The ability to spin up a stack quickly would be advantageous for code/product review. Fargate seems more suited to the 'temporary workload' type of work where our server don't really need to be runnign 24/7.

Docker Compose

Docker Compose allows you to orchestrate and script multiple docker images together. This has been around for a while however I was curious about how it could work with Fargate.

I firstly needed to create a docker_compose.yml file so I could start creating a script

Here is an example of my local docker-compose file

version: '3.6'

services:
  web:
    build: .
    command: ["supervisord", "-c", "/etc/supervisord-staging.conf"]
    ports:
      - 3000:3000
    depends_on:
      - db
    environment:
      DATABASE_URL: postgres://postgres@db
      RAILS_ENV: staging
      RAILS_SERVE_STATIC_FILES: 'true'
  db:
    image: postgres:10.3-alpine

When you call the docker-compose up command, docker will take this file and build + run each component. The result being a fully scriptable environment which is pretty handy for development and QA.

Getting ready for AWS

I came across an article explaining that ECS supports Docker Compose Files which is really powerful. It means that we can pass the docker compose file straight into ECS and it will take care of all the provisioning for me.

I did have to make some changes to the compose file though, to make it compatible with AWS.

version: '3'

services:
  web:
    image: "123456789.dkr.ecr.ap-southeast-2.amazonaws.com/my-app:4f6a07433a35c1369851eacf7773e2cccdc5a651"
    command: ["supervisord", "-c", "/etc/supervisord-staging.conf"]
    ports:
      - 3000:3000
    environment:
      DATABASE_URL: postgres://postgres@localhost
      RAILS_ENV: staging
      RAILS_SERVE_STATIC_FILES: 'true'
    logging:
      driver: awslogs
      options: 
        awslogs-group: /ecs/tutorial
        awslogs-region: ap-southeast-2
        awslogs-stream-prefix: ecs
  db:
    image: postgres:10.3-alpine
    logging:
      driver: awslogs
      options: 
        awslogs-group: /ecs/tutorial
        awslogs-region: ap-southeast-2
        awslogs-stream-prefix: db

Provisioning Resources

So most of preperation work is completed and it's time to connect with AWS

I used this AWS Tutorial to create my resources

Quick Overview of what this tutorial goes through

Gotchas