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 where your Dockerfile is located.
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 as long as the build 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
Since you convert monoliths into microservices, you will edit the source code (eg mythicalMysfitsService.py) and build this image a few 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
TABLE_NAME=$(aws dynamodb list-tables | jq -r .TableNames[0])
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 names in the workshop-1/cfn-output.json file which is derived from the CloudFormation Stack output.
curl http://localhost:8000/mysfits
Note: Processes running inside a Docker container can authenticate with DynamoDB because they can access the EC2 Metadata API endpoint running at 169.254. attached to our Cloud9 environment in the initial setup script. Processes in the container cannot access the ~/.aws/credentials file in the host filesystem (unless it’s explicitly attached 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: Container runs in the foreground with stdout/stderr printing to the console. In a production environment, when running the container in the background and having to configure the destination of the logs. We can run the container in the background using -d.
TABLE_NAME=$(aws dynamodb list-tables | jq -r .TableNames[0])
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: 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