
FROM ubuntu:20.04
RUN apt-get update -y
RUN apt-get install -y python3-pip python-dev build-essential
RUN pip3 install --upgrade pip
#[TODO]: Copy python source files and requirements file into container image
#[TODO]: Install dependencies listed in the requirements.txt file using pip3
#[TODO]: Specify a listening port for the container
#[TODO]: Run mythicalMysfitsService.py as the final step. We want this container to run as an executable. Looking at ENTRYPOINT and CMD for this?

FROM ubuntu:20.04
RUN apt-get update -y
RUN apt-get install -y python3-pip python-dev build-essential
RUN pip3 install --upgrade pip
#[TODO]: Copy python source files and requirements file into container image
COPY ./service /MythicalMysfitsService
WORKDIR /MythicalMysfitsService
#[TODO]: Install dependencies listed in the requirements.txt file using pip3
RUN pip3 install -r ./requirements.txt
#[TODO]: Specify a listening port for the container
EXPOSE 80
#[TODO]: Run the mythicalMysfitsService.py as the final step
ENTRYPOINT ["python3"]
CMD ["mythicalMysfitsService.py"]

cd ~/environment/amazon-ecs-mythicalmysfits-workshop/workshop-1/app/monolith-service/
mv Dockerfile.draft Dockerfile

This command needs to be run in the same directory that contains your Dockerfile.
docker build -t monolith-service .

If something goes wrong during the build, the build will fail and stop (red text and warnings along the way are also fine as long as the build process doesn’t fail).
Removing intermediate container a71540b615b4
---> 5ab93ce927c8
Step 8/10 : EXPOSE 80
---> Running in 27074f1d4c3a
Removing intermediate container 27074f1d4c3a
---> f528fe7756d5
Step 9/10 : ENTRYPOINT ["python3"]
---> Running in 8ef1757aadb0
Removing intermediate container 8ef1757aadb0
---> a1d1ed159fb2
Step 10/10 : CMD ["mythicalMysfitsService.py"]
---> Running in da0c544e601b
Removing intermediate container da0c544e601b
---> b283e0821fc9
Successfully built b283e0821fc9
Successfully tagged monolith-service:latest

Because you are migrating the monolith into microservices, you will edit the source code (e.g., mythicalMysfitsService.py) and build this image several times.

FROM ubuntu:20.04
RUN apt-get update -y
RUN apt-get install -y python3-pip python-dev build-essential
RUN pip3 install --upgrade pip
COPY ./service/requirements.txt .
RUN pip3 install -r ./requirements.txt
COPY ./service /MythicalMysfitsService
WORKDIR /MythicalMysfitsService
EXPOSE 80
ENTRYPOINT ["python3"]
CMD ["mythicalMysfitsService.py"]

docker build -t monolith-service .

# This is a comment to force a Docker-rebuild

docker build -t monolith-service .

docker images

export TABLE_NAME="$(jq < ~/environment/amazon-ecs-mythicalmysfits-workshop/workshop-1/cfn-output.json -r '.DynamoTable')"
docker run -p 8000:80 -e AWS_DEFAULT_REGION=$AWS_REGION -e DDB_TABLE_NAME=$TABLE_NAME monolith-service
Note: You can find your DynamoDB table name in the file workshop-1/cfn-output.json which originates from the CloudFormation Stack outputs

curl http://localhost:8000/mysfits
Note: The processes running inside the Docker container can authenticate with DynamoDB because they can access the EC2 metadata API endpoint running at 169.254.169.254 to retrieve credentials for the instance profile that was attached to our VSCode environment in the initial setup script. Processes inside the container cannot access the ~/.aws/credentials file in the host file system (unless it is explicitly mounted to the container).

* Serving Flask app "mythicalMysfitsService" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
172.17.0.1 - - [26/May/2022 16:49:43] "GET /mysfits HTTP/1.1" 200 -
INFO:werkzeug:172.17.0.1 - - [26/May/2022 16:49:43] "GET /mysfits HTTP/1.1" 200 -

Note: The container runs in the foreground with stdout / stderr printing to the console. In a production environment, when running a container in the background, you must configure the destination of the logs. We can run the container in the background (detached mode) by using -d.
export TABLE_NAME="$(jq < ~/environment/amazon-ecs-mythicalmysfits-workshop/workshop-1/cfn-output.json -r '.DynamoTable')"
docker run -d -p 8000:80 -e AWS_DEFAULT_REGION=$AWS_REGION -e DDB_TABLE_NAME=$TABLE_NAME monolith-service

docker ps

docker logs <CONTAINER_ID>

Note: the repository URI is unique

MONO_ECR_REPOSITORY_URI=$(aws ecr describe-repositories | jq -r .repositories[].repositoryUri | grep mono)
docker tag monolith-service:latest $MONO_ECR_REPOSITORY_URI:latest
docker push $MONO_ECR_REPOSITORY_URI:latest

