Docker_Swarm_ECS

Creat an example app with docker swarm

Here is an app based on latest wordpress and mysql 5.7 docker image.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ cat <<EOF > stack.yaml
version: '3.1'

services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
EOF
$ docker swarm init
$ docker stack deploy -c stack.yaml mywordpress

Creating network mywordpress_default
Creating service mywordpress_wordpress
Creating service mywordpress_db

$ docker stack rm mywordpress 

To test it, browse http://127.0.0.1:8000 to see the workpress new user page.

Creat the same app on ECS

Install ECS CLI

1
2
3
4
5
6
7
8
9
sudo curl -Lo /usr/local/bin/ecs-cli https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-linux-amd64-latest
vi ecs_cli_gpg.txt # copy/paste Amazon ECS PGP public key
gpg --import ecs_cli_gpg.txt
curl -Lo ecs-cli.asc https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-linux-amd64-latest.asc
gpg --verify ecs-cli.asc /usr/local/bin/ecs-cli
sudo chmod +x /usr/local/bin/ecs-cli
$ ecs-cli -v
ecs-cli version 1.21.0 (bb0b8f0)

Configuring ECS CLI

Configuration information is stored in the ~/.ecs directory on macOS and Linux systems

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ ecs-cli configure profile \
    --access-key $AWS_ACCESS_KEY_ID \
    --secret-key $AWS_SECRET_ACCESS_KEY
    # --profile-name dec
$ cat ~/.ecs/credentials # created by above configure profile --profile-name dec command
version: v1
default: default
ecs_profiles:
  default:
    aws_access_key_id: <AWS_ACCESS_KEY_ID>
    aws_secret_access_key: <AWS_SECRET_ACCESS_KEY>
$ ecs-cli configure \
    --cluster test-fargate-App \
    --region us-east-1 \
    --default-launch-type FARGATE
    # --config-name dec
$ cat ~/.ecs/config # this file is created by above ecs-cli configure command
version: v1
default: default
clusters:
  default:
    cluster: test-fargate-App
    region: us-east-1
    default_launch_type: FARGATE

Creat ECS Cluster

This command “surprisingly” created a Cloudformation stack “amazon-ecs-cli-setup-test-fargate-App”.

All this CF template deployed is a VPC, however the ECS cluster is created outside of this CF template. I believe if the default_launch_type is not FARGATE, the EC2 instances will be created since I do see them in the CF template with conditions.

1
2
3
$ ecs-cli up
    # --cluster-config dec \
    # --ecs-profile dec

List and pull Images from ECR

1
2
3
4
5
6
7
$ aws ecr --profile <profile> --region eu-west-1 describe-repositories 
$ ecs-cli pull --aws-profile <profile> <AWS_ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/my-ecr-repo:mytag
INFO[0000] Getting AWS account ID...                    
INFO[0000] Pulling image                                 repository=<AWS_ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/my-ecr-repo tag=mytag
INFO[0006] Image pulled           
$  docker images | tail -1
<AWS_ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/my-ecr-repo   mytag      6c1f3809ea08   2 years ago    50.9MB                      

Create task definiation

Following ecs-cli official repo to create 2 files.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ cat <<EOF > docker-compose.yml
version: '3'
services:
    mysql:
        image: mysql:5.7
        environment:
            MYSQL_ROOT_PASSWORD:
    wordpress:
        image: wordpress
        ports:
            - "80:80"
EOF
$ cat <<EOF > ecs-params.yml
version: 1
task_definition:
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 2GB
    cpu_limit: 512
run_params:
  network_configuration:
    awsvpc_configuration:
      subnets:
        - "subnet-0f8e36255ab1ac868"
        - "subnet-0a38fe5a081e8eead"
      assign_public_ip: ENABLED         
EOF
$ 

Todo: using the following commands I should be able to create tasks and service.

1
2
$ ecs-cli compose --project-name wordpress-test service create
$ ecs-cli compose --project-name wordpress-test service ps