The log of the service (managed by docker-compose) running on the Docker container in AWS EC2 is thrown to CloudWatch Logs, and the procedure until real-time monitoring with VS Code is summarized.
--About Docker's log driver --Register for CloudWatch Logs --About the AWS extension of VS Code and viewing logs
The standard function of Docker is to output the log in the container to / var / lib / docker / containers /
. Therefore, it is not necessary to use Linux logrotate
etc.
The implementation example in docker-compose is as follows.
docker-compose.yml
version: "3"
services:
hello:
image: "busybox:latest"
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
You can change the log output destination to CloudWatch Logs using Docker's log driver explained above. The implementation example in docker-compose is as follows.
docker-compose.yml
version: '3'
services:
app:
build:
context: .
dockerfile: prod/Dockerfile
restart: always
expose:
- 8000
environment:
- TZ=Asia/Tokyo
logging:
driver: awslogs
options:
awslogs-region: ap-northeast-1
awslogs-group: log-group-name
tag: "{{.ImageName}}.{{.Name}}.{{.FullID}}"
nginx:
build:
context: .
dockerfile: nginx/Dockerfile
restart: always
ports:
- 8080:80
depends_on:
- app
logging:
driver: awslogs
options:
awslogs-region: ap-northeast-1
awslogs-group: log-group-name
tag: "{{.ImageName}}.{{.Name}}.{{.FullID}}"
In docker-compose, describe the log driver settings for each service. Write the log group name created in the management console in ʻawslogs-group`.
If you type ʻaws in the search window of the VS Code extension, ʻAWS Toolkit
as shown in the image below will be displayed. Install it.
Follow the instructions to set the region and access key.
When you open the CloudWatch Logs
tab in the image above, the log group will be displayed. Right-click and select view log stream
.
Output Colorizer If you install the extension, you can see the log with highlights, so it is recommended to include it.
Recommended Posts