Containerize the Mythical Mysfits monolith

Containerize the Mythical Mysfits monolith

Containerize monolith

Meaning of some INSTRUCTIONs in Dockerfile.

  • FROM: Used to indicate which base image the image is built from. Depending on the application being packaged, we will use different base images.
  • RUN: Used to run a specific command when building an image.
  • WORKDIR: Used to set the working directory. Any subsequent RUN, CMD, ENTRYPOINT, COPY and ADD instructions will be executed inside this WORKDIR directory.
  • COPY: COPY the source directory from the host machine to the image’s filesystem.
  • CMD: Used to provide the default command that will be run when the Docker Container starts from the built Image, there can only be exactly 1 CMD instruction.
  1. Check Dockerfile.draft
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?

Containerize the Mythical Mysfits monolith

  1. Complete the Dockerfile
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"]

Containerize the Mythical Mysfits monolith

  1. If the Dockerfile is complete, rename your file from “Dockerfile.draft” to “Dockerfile” and proceed to the next step.
cd ~/environment/amazon-ecs-mythicalmysfits-workshop/workshop-1/app/monolith-service/
mv Dockerfile.draft Dockerfile

Containerize the Mythical Mysfits monolith

  1. Execute building the image using the command docker build [OPTIONS] PATH | URL | - .

This command needs to be run in the same directory that contains your Dockerfile.

  • Note the period at the end for the build command to look in the current directory for the Dockerfile.
docker build -t monolith-service .

Containerize the Mythical Mysfits monolith

  1. You will see a series of outputs as Docker builds all the layers of the image.

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

Containerize the Mythical Mysfits monolith

  1. Your Dockerfile has been created successfully, but the Dockerfile has not yet been optimized for microservices.

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

  • Check the Dockerfile

Containerize the Mythical Mysfits monolith

  1. Perform Dockerfile optimization
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"]

Containerize the Mythical Mysfits monolith

  1. To see the benefits of optimizing the Dockerfile, you first need to rebuild the monolith image using the new Dockerfile.
docker build -t monolith-service .

Containerize the Mythical Mysfits monolith

  1. After that, we perform a change in mythicalMysfitsService.py by adding a comment line at the end of the file.
# This is a comment to force a Docker-rebuild

Containerize the Mythical Mysfits monolith

  1. Docker has cached the requests in the first rebuild after reordering.
docker build -t monolith-service .

Containerize the Mythical Mysfits monolith

  1. Use docker images [OPTIONS] [REPOSITORY[:TAG]] to list images
docker images

Containerize the Mythical Mysfits monolith

  1. Run the container and test
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

Containerize the Mythical Mysfits monolith

  1. To check the basic functionality of the monolith service, query the service using a utility like cURL that comes with the linux distribution.
curl http://localhost:8000/mysfits
  • You will see a JSON array with data about Mythical 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).

Containerize the Mythical Mysfits monolith

  1. Go back to the tab running the monolith container
  • The monolith container runs in the foreground with stdout/stderr printing to the screen; when it receives a request, it will display 200 “OK”
 * 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 -

Containerize the Mythical Mysfits monolith

  1. In the tab running the monolith container, use the shortcut Ctrl + C to stop running the container.

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

Containerize the Mythical Mysfits monolith

  1. List the docker containers to check the running container
docker ps

Containerize the Mythical Mysfits monolith

  1. See the running monolith in the list (store the Container ID to use with docker logs). Repeat the curl command, then check the logs
docker logs <CONTAINER_ID>

Containerize the Mythical Mysfits monolith

  1. Currently, we have an active Docker image, we proceed to assign a tag and push to ECR. AWS ECR is a fully managed Docker container registry service by AWS that simplifies storing, managing, and deploying Docker container images. ECR can be integrated with Amazon Elastic Container Service (ECS) to simplify setting up deployment pipelines for production systems as well as eliminate the complexity of managing a storage repository for container images. Next, we use ECS to pull the image from ECR.
  • Access ECS and select Repositories
  • We will have 2 repositories: STACK_NAME-mono-xxx and STACK_NAME-like-xxx
  • Click on the copy icon for the URL of the Mono repository (used in subsequent steps)

Note: the repository URI is unique

Containerize the Mythical Mysfits monolith

  1. Tag and push the container image into the monolith repository
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

Containerize the Mythical Mysfits monolith

  1. Access the ECR repository page, the uploaded image has appeared and is tagged latest.

Containerize the Mythical Mysfits monolith