Skip to content

Commit

Permalink
ecr image example
Browse files Browse the repository at this point in the history
  • Loading branch information
omenking committed Apr 14, 2024
1 parent 9789010 commit 3e616fc
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
78 changes: 78 additions & 0 deletions ecr/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## Install requirements

```sh
cd app
pip install -r requirements.txt
```

## Run without a container

```sh
python app.py
```

This should start the app on port `5000`

## Test the endpoint for local app

```sh
curl localhost:5000/api/hello
curl localhost:5000/api/hello?name=Andrew+Brown
```

## Build Image

```sh
docker build -t app ./app
```

## Run the Container

```sh
docker run --rm -p 4567:4567 -it app
```

## Test the endpoint for local container app

```sh
curl localhost:4567/api/hello
curl localhost:4567/api/hello?name=Andrew+Brown
```

## Create ECR Repos


aws ecr create-repository \
--repository-name python \
--image-tag-mutability IMMUTABLE

aws ecr create-repository \
--repository-name app \
--image-tag-mutability MUTABLE

## Login to ECR

```sh
aws ecr get-login-password --region ca-central-1 | docker login \
--username AWS \
--password-stdin 982383527471.dkr.ecr.ca-central-1.amazonaws.com
```

### Pull, Tag and Push Python Image


```sh
docker pull python:3.13.0a6-slim-bookworm
docker tag python:3.13.0a6-slim-bookworm 982383527471.dkr.ecr.ca-central-1.amazonaws.com/python:3.13.0a6-slim-bookworm
docker push 982383527471.dkr.ecr.ca-central-1.amazonaws.com/python:3.13.0a6-slim-bookworm
```

### Build, tag and push app container

> Update the Dockerfile to reference our python image in ECR
```sh
docker build -t app .
docker tag app:latest 982383527471.dkr.ecr.ca-central-1.amazonaws.com/app:latest
docker push 982383527471.dkr.ecr.ca-central-1.amazonaws.com/app:latest
```
28 changes: 28 additions & 0 deletions ecr/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# FROM python:3.13.0a6-slim-bookworm
FROM 982383527471.dkr.ecr.ca-central-1.amazonaws.com/python:3.13.0a6-slim-bookworm

# Inside Container
# make a new folder inside container
WORKDIR /app

# Outside Container -> Inside Container
# this contains the libraries want to install to run the app
COPY requirements.txt requirements.txt

# Inside Container
# Install the python libraries used for the app
RUN pip3 install -r requirements.txt

# Outside Container -> Inside Container
# . means everything in the current directory
# first period . - /app (outside container)
# second period . /app (inside container)
COPY . .

EXPOSE ${PORT}

ENV PYTHONUNBUFFERED=1

# CMD (Command)
# python3 -m flask run --host=0.0.0.0 --port=4567
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0", "--port=4567", "--debug"]
22 changes: 22 additions & 0 deletions ecr/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import Flask
from flask import request
import os

app = Flask(__name__)

@app.route("/api/hello", methods=['GET'])
def hello():
model = {'errors': None}
name = request.args.get('name')
if not name:
model['errors'] = [{"msg": "Name not provided"}]
else:
model['data'] = {"msg": f"Hello {name}!"}

if 'errors' in model and model['errors'] is not None:
return model['errors'], 422
else:
return model['data'], 200

if __name__ == "__main__":
app.run(debug=True)
1 change: 1 addition & 0 deletions ecr/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask

0 comments on commit 3e616fc

Please sign in to comment.