When using docker compose to run the service, we can view the container‘s log in the command line, but this may not be enough. We may need to store the log persistently and hope to be able to retrieve it conveniently. The cloudwatch service of AWS provides us with powerful log management capability. How can we use docker compose This paper provides a successful practice step to output the log generated by the service of cloudwatch.
Configure AWS permissions
- create a file
Write the following to a file
[Service] Environment="AWS_ACCESS_KEY_ID=<aws_access_key_id>" Environment="AWS_SECRET_ACCESS_KEY=<aws_secret_access_key>"
sudo systemctl daemon-reloadReload virtual machine configuration
sudo service docker restartRestart docker
systemctl show --property=Environment dockerCheck that the configuration is in effect
Configure docker- compose.yml
Add logging configuration to the service that needs to collect logs (for more configuration items, please refer to the official documentation)
version: '2' services: my-service: image: my-service:latest logging: driver: awslogs options: awslogs-region: ap-northeast-1 awslogs-group: /log-prod/my-service
docker-compose up -dRestart the docker compose container.
Check whether the docking is successful
Enter the cloudwatch menu of AWS and open the log group where the service is located. By default, we should be able to see the log stream named by the container ID, which indicates that the docking has been successfully completed.