Simple Dockerfile to test AWS ECR and ECS

For a specific task (run and exit), Dockerfile could be:

FROM ubuntu
CMD ["echo", "Hello, World!"]

Build this Dockerfile:

docker build -t helloworld:1.0.0 .

Tag the image:

docker tag helloworld:1.0.0 <aws_account_id>.dkr.ecr.<region>.amazonaws.com/helloworld:1.0.0

Upload the image to ECR:

docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/helloworld:1.0.0

In ECS, create “Cluster”, then create “Task Definition”, finally “Run new task”.

For a continuous service (like Web server), Dockerfile could be:

# 1. Use the official lightweight base image that comes with Python
FROM python:3.11-slim

# 2. Set the working directory
WORKDIR /app

# 3. Create a simple HTML webpage to satisfy the AWS health checks
RUN echo "Hello, World! I am alive!" > index.html

# 4. Explicitly inform Docker that our container will use port 80 (this aligns with Express Mode default settings)
EXPOSE 80

# 5. Start Python's built-in HTTP server. 
# It perfectly responds to all requests sent by the ALB (Application Load Balancer) and will never exit on its own!
CMD ["python", "-m", "http.server", "80"]

Build this Dockerfile:

docker build -t helloworld:2.0.0 .

Tag the image:

docker tag helloworld:2.0.0 <aws_account_id>.dkr.ecr.<region>.amazonaws.com/helloworld:2.0.0

Upload the image to ECR:

docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/helloworld:2.0.0

In ECS, choose “Express Mode”.

How to check “gperftools” version based on “tcmalloc” library?

strings <path>/libtcmalloc.so | grep -i "gperftools"

or

grep -ao "gperftools [0-9.]*" <path>/libtcmalloc.so

-a: forces it to read the binary as text.
-o: tells it to only print the matching version string instead of dumping a massive wall of corrupted binary text into your terminal.

How to check the root cause of the difference between “pytest” and “python -m pytest”?

CommandUses which Python?Risk
pytestWhatever executable is first in PATHMay run wrong environment
python -m pytestThe Python you explicitly calledMuch safer
cat $(which pytest)

The shebang line can show which “python” that pytest uses.

python -c "import sys; print(sys.executable)"

or

which python

This shows the interpreter you’re explicitly invoking.

Use getrusage() to check memory usage

To check the memory usage of a C++ program on POSIX-compliant systems (like Linux, macOS, BSD) using getrusage, you can access the ru_maxrss field of the rusage structure. This field provides the maximum resident set size (maximum amount of physical RAM used) in KB. 

#include <sys/resource.h>
#include <iostream>

struct rusage usage;
if( getrusage(RUSAGE_SELF, &usage) == 0 ) {
   std::cout << usage.ru_maxrss / 1024 << " MB" << std::endl;
}