From 2586fa35d42a94becd6660232b0cc8cd81721ffc Mon Sep 17 00:00:00 2001 From: Chad Metcalf Date: Tue, 7 Jul 2020 22:20:27 -0700 Subject: [PATCH] Adding the demo from AWS C3. Signed-off-by: Nicolas De Loof --- ecs/demo/Makefile | 25 ++++++ ecs/demo/README.md | 107 ++++++++++++++++++++++++ ecs/demo/app/Dockerfile | 7 ++ ecs/demo/app/app.py | 19 +++++ ecs/demo/app/requirements.txt | 2 + ecs/demo/app/scripts/entrypoint.sh | 4 + ecs/demo/app/templates/index.html | 125 +++++++++++++++++++++++++++++ ecs/demo/docker-compose.yml | 12 +++ 8 files changed, 301 insertions(+) create mode 100644 ecs/demo/Makefile create mode 100644 ecs/demo/README.md create mode 100644 ecs/demo/app/Dockerfile create mode 100644 ecs/demo/app/app.py create mode 100644 ecs/demo/app/requirements.txt create mode 100755 ecs/demo/app/scripts/entrypoint.sh create mode 100644 ecs/demo/app/templates/index.html create mode 100644 ecs/demo/docker-compose.yml diff --git a/ecs/demo/Makefile b/ecs/demo/Makefile new file mode 100644 index 000000000..deba2b83f --- /dev/null +++ b/ecs/demo/Makefile @@ -0,0 +1,25 @@ +REPO_NAMESPACE ?= ${USER} +FRONTEND_IMG = ${REPO_NAMESPACE}/timestamper +REGISTRY_ID ?= PUT_ECR_REGISTRY_ID_HERE +DOCKER_PUSH_REPOSITORY=dkr.ecr.us-west-2.amazonaws.com + +all: build-image + +create-ecr: + aws ecr create-repository --repository-name ${FRONTEND_IMG} + +build-image: + docker build -t $(REGISTRY_ID).$(DOCKER_PUSH_REPOSITORY)/$(FRONTEND_IMG) ./app + docker build -t $(FRONTEND_IMG) ./app + +push-image-ecr: + aws ecr get-login-password --region us-west-2 | docker login -u AWS --password-stdin $(REGISTRY_ID).$(DOCKER_PUSH_REPOSITORY) + docker push $(REGISTRY_ID).$(DOCKER_PUSH_REPOSITORY)/$(FRONTEND_IMG) + +push-image-hub: + docker push $(FRONTEND_IMG) + +clean: + @docker context use default + @docker context rm aws || true + @docker-compose rm -f || true diff --git a/ecs/demo/README.md b/ecs/demo/README.md new file mode 100644 index 000000000..803235204 --- /dev/null +++ b/ecs/demo/README.md @@ -0,0 +1,107 @@ +## Compose sample application + +### Python/Flask application + +``` ++--------------------+ +------------------+ +| | | | +| Python Flask | timestamps | Redis | +| Application |------------->| | +| | | | ++--------------------+ +------------------+ +``` + +### Things you'll need to do. + +There are a number of places you'll need to fill in information. You can find them with: + +``` +grep -r '<<<' ./* +./docker-compose.yml: x-aws-pull_credentials: <<>> +./docker-compose.yml: image: <<>>/timestamper +## Walk through +``` + +### Setup pull credentials for private Docker Hub repositories + +You should use a Personal Access Token (PAT) vs your default password. If you have 2FA enabled on your Hub account you will have to create a PAT. You can read more about managing access tokens here: https://docs.docker.com/docker-hub/access-tokens/ + +``` + docker ecs secret create -d MyKey -u myhubusername -p myhubpat +``` + +### Create an AWS Docker context and list available contexts + +``` +docker ecs setup +Enter context name: aws +✔ sandbox.devtools.developer +Enter cluster name: +Enter region: us-west-2 +✗ Enter credentials: + +docker context ls +NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR +aws +default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm +``` + +### Test locally + +``` +docker context use default +docker-compose up +open http://localhost:5000 +``` + +### Push images to hub for ecs (ecs cannot see your local image cache) + +``` +docker-compose push +``` + +### Switch to ECS context and launch the app + +``` +docker context use aws +docker ecs compose up +``` + +### Check out the CLI + +``` +docker ecs compose ps +docker ecs compose logs +``` + +### Check out the aws console + +- cloud formation +- cloud watch +- security groups +- Load balancers (ELB for this example / ALB if your app only uses 80/443) + +### Checkout cloudformation + +``` +docker ecs compose convert +``` + +### Stop the meters + +``` +docker ecs compose down + +``` + +## Using Amazon ECR instead of Docker Hub + +[Makefile](Makefile) has an example setup for creating an ECR repository and pushing to it. You'll need to have the AWS CLI installed and your AWS credentials available. + +``` +make create-ecr +REGISTRY_ID= make build-image +REGISTRY_ID= make push-image-ecr +``` + +If you want to use this often, you'll likely want to replace `PUT_ECR_REGISTRY_ID_HERE` with the value from above. diff --git a/ecs/demo/app/Dockerfile b/ecs/demo/app/Dockerfile new file mode 100644 index 000000000..64469d281 --- /dev/null +++ b/ecs/demo/app/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.7-alpine +WORKDIR /app +COPY requirements.txt /app +RUN pip3 install -r requirements.txt +COPY . /app +ENTRYPOINT ["/app/scripts/entrypoint.sh"] +CMD ["python3", "app.py"] diff --git a/ecs/demo/app/app.py b/ecs/demo/app/app.py new file mode 100644 index 000000000..e54b19a28 --- /dev/null +++ b/ecs/demo/app/app.py @@ -0,0 +1,19 @@ + +from flask import Flask +from flask import render_template +from redis import StrictRedis +from datetime import datetime + +app = Flask(__name__) +redis = StrictRedis(host='backend', port=6379) + + +@app.route('/') +def home(): + redis.lpush('times', datetime.now().strftime('%Y-%m-%dT%H:%M:%S%z')) + return render_template('index.html', title='Home', + times=redis.lrange('times', 0, -1)) + + +if __name__ == '__main__': + app.run(host='0.0.0.0', debug=True) diff --git a/ecs/demo/app/requirements.txt b/ecs/demo/app/requirements.txt new file mode 100644 index 000000000..1a5dc97b1 --- /dev/null +++ b/ecs/demo/app/requirements.txt @@ -0,0 +1,2 @@ +flask +redis diff --git a/ecs/demo/app/scripts/entrypoint.sh b/ecs/demo/app/scripts/entrypoint.sh new file mode 100755 index 000000000..198660244 --- /dev/null +++ b/ecs/demo/app/scripts/entrypoint.sh @@ -0,0 +1,4 @@ +#! /bin/sh + +if [ "${LOCALDOMAIN}" != "" ]; then echo "search ${LOCALDOMAIN}" >> /etc/resolv.conf; fi +exec "$@" diff --git a/ecs/demo/app/templates/index.html b/ecs/demo/app/templates/index.html new file mode 100644 index 000000000..91efdaefb --- /dev/null +++ b/ecs/demo/app/templates/index.html @@ -0,0 +1,125 @@ + + + + + + + + + + + Hello, Docker! + + +
+ + +
+

Hello, Docker Folks!

+
+ +
+ + +
+ +
+ + + + + + + + {% for t in times %} + + + + {% endfor %} + +
Timestamp
{{ t.decode('utf-8') }}
+
+ +
+ + + + + + + + diff --git a/ecs/demo/docker-compose.yml b/ecs/demo/docker-compose.yml new file mode 100644 index 000000000..31152b2a7 --- /dev/null +++ b/ecs/demo/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.8" +services: + frontend: + build: app + x-aws-pull_credentials: <<>> + image: <<>>/timestamper + ports: + - "5000:5000" + depends_on: + - backend + backend: + image: redis:alpine