N
N
Ninja Mate2018-02-11 19:18:02
Amazon Web Services
Ninja Mate, 2018-02-11 19:18:02

How to properly offload a Docker project with many containers in AWS(ECS)?

I have a project with 4 images running as 5 services via docker-compose

version: '3.1'
services:
  nginx-api:
    image: nginx
    depends_on:
      - api
    ports:
      - "3001:443"
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
    volumes:
      - ./api/api.nginx.site:/etc/nginx/conf.d/default.conf
      - ./api/certs/.:/etc/nginx/certs
  api:
    build: api/.
    command: yarn start
    depends_on:
      - db
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      NODE_ENV: development
      COOKIE_SECRET: ChangeMe1234
    env_file:
      - api/.env
    ports:
      - '8080:3000'
    volumes:
      - ./api/.:/opt/app
      - /opt/app/node_modules
  nginx-frontend:
    image: nginx
    depends_on:
      - frontend
    ports:
      - "3000:443"
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
    volumes:
      - ./frontend/frontend.nginx.site:/etc/nginx/conf.d/default.conf
      - ./frontend/certs/.:/etc/nginx/certs
  frontend:
    build: frontend/.
    command: yarn start
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      NODE_ENV: development
      COOKIE_SECRET: ChangeMe1234
      API_URL: "https://nginx-api"
    ports:
      - "0.0.0.0:3030:3000"
    volumes:
      - ./frontend/.:/opt/frontend
      - /opt/frontend/node_modules
  db:
    build: db/.
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      POSTGRES_PASSWORD: localdev
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ./db/postgres-data:/var/lib/postgresql/data/pgdata
    ports:
      - '8001:5432'

What I'm doing now:
Created an ECS cluster ( Registered container instances 5),
Separately assembled image api, frontend, db, nginx and uploaded them to the aws repository
Registered them all as Task.
I started db and frontend on the cluster, api did not start.
How to start nginx now?
Is there a way to do this more easily than by hand?
What is left to do to run all containers on the server?
Here is a question on github to the boilerplate (only the database is slightly different)
https://github.com/chadfurman/rpg-boilerplate/issues/19
PS All this is locally run via the docker/run.sh script
#!/bin/bash

set -e

COMPOSE_PROJECT_NAME=rpgboilerplate

# Linux fix
CONFIG_FILE=docker-compose.yml

CACHE_INIT=false

SERVICES=(api frontend)
SERVICES_DIR=(api frontend)

BUILD_CMD="docker-compose -f $CONFIG_FILE build"
UP_CMD="docker-compose -f $CONFIG_FILE up"

function rmContainers() {
  echo "Removing containers"
  docker-compose -f $CONFIG_FILE rm -fv $SERVICE_NAME || true
}

function initCache() {
  # Init empty cache file
  touch $SERVICE_DIR/yarn.lock
  if [ ! -f $SERVICE_DIR/.yarn-cache.tgz ]; then
    echo "Init empty $SERVICE_DIR/.yarn-cache.tgz"
    tar cvzf $SERVICE_DIR/.yarn-cache.tgz --files-from /dev/null
    CACHE_INIT=true
  fi
}

function retrieveYarnCache() {
  echo "Checking if I need to retrieve Yarn cache and lock file for $SERVICE_NAME"
  RETRIEVE=false

  # Case #1: we initialized an empty cache (usually, first run after cloning)
  if [ "$CACHE_INIT" = true ]; then
    RETRIEVE=true
  fi

  # Case #2: yarn packages changed during build
  docker run --rm --entrypoint cat ${COMPOSE_PROJECT_NAME}_$SERVICE_NAME:latest /cache/yarn.lock > /tmp/${SERVICE_NAME}_yarn.lock
  if ! diff -q $SERVICE_DIR/yarn.lock /tmp/${SERVICE_NAME}_yarn.lock > /dev/null  2>&1; then
    RETRIEVE=true
  fi

  if [ "$RETRIEVE" = true ]; then
    echo "Retrieving"
    docker run --rm --entrypoint cat ${COMPOSE_PROJECT_NAME}_$SERVICE_NAME:latest /.yarn-cache.tgz > $SERVICE_DIR/.yarn-cache.tgz
    cp /tmp/${SERVICE_NAME}_yarn.lock $SERVICE_DIR/yarn.lock
  else
    echo "Not retrieving"
  fi
  echo "Done"
}

for i in "${!SERVICES[@]}"
do
  SERVICE_NAME=${SERVICES[$i]} SERVICE_DIR=${SERVICES_DIR[$i]} rmContainers
  SERVICE_NAME=${SERVICES[$i]} SERVICE_DIR=${SERVICES_DIR[$i]} initCache
done

COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME $BUILD_CMD

for i in "${!SERVICES[@]}"
do
  SERVICE_NAME=${SERVICES[$i]} SERVICE_DIR=${SERVICES_DIR[$i]} retrieveYarnCache
done

COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME $UP_CMD

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question