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
Lệnh này cần được chạy trong cùng thư mục chứa Dockerfile của bạn.
docker build -t monolith-service .
Nếu có sự cố xảy ra trong quá trình build, quá trình build sẽ không thành công và dừng lại (văn bản màu đỏ và các cảnh báo trên đường đi cũng được miễn là quá trình build không bị lỗi).
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
Vì bạn chuyển monolith thành các microservices, bạn sẽ chỉnh sửa source code (ví dụ mythicalMysfitsService.py) và build image này một vài lần.
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 .
Thực hiện rebuild monolith image một lần nữa. Tham chiếu bộ nhớ cache trong lần build lại thứ hai này
Sử dụng docker images [OPTIONS] [REPOSITORY[:TAG]] để liệt kê danh sách image
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
Lưu ý: Bạn có thể tìm thấy tên bảng DynamoDB của mình trong tệp workshop-1/cfn-output.json có nguồn gốc từ kết quả đầu ra của CloudFormation Stack
curl http://localhost:8000/mysfits
Lưu ý: Các quy trình chạy bên trong vùng chứa Docker có thể xác thực bằng DynamoDB vì chúng có thể truy cập điểm cuối API siêu dữ liệu EC2 đang chạy tại 169.254.169.254để truy xuất thông tin đăng nhập cho hồ sơ cá thể đã được đính kèm với môi trường Cloud9 của chúng tôi trong tập lệnh thiết lập ban đầu. Các quy trình trong vùng chứa không thể truy cập tệp ~/.aws/credentials trong hệ thống tệp máy chủ (trừ khi nó được gắn vào vùng chứa một cách rõ ràng).
* 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 -
Lưu ý: Container chạy ở foreground với tính năng stdout / stderr in vào bảng điều khiển. Trong môi trường production, khi chạy container trong background và phải cấu hình điểm đến của logs. Chúng ta có thể chạy container trong background (chạy ngầm) bằng cách sử dụng -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 là duy nhất
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