Link Docker log to AWS CloudWatch and monitor in real time with VS Code

Introduction

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

About Docker log driver

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"

Register for CloudWatch Logs

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`.

About AWS extension of VS Code and viewing logs

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. スクリーンショット 2020-09-29 17.43.59.png Follow the instructions to set the region and access key. スクリーンショット 2020-09-29 17.47.32.png 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.

reference

Recommended Posts

Link Docker log to AWS CloudWatch and monitor in real time with VS Code
Create a private key / public key in CentOS8.2 and connect to SSH with VS Code
Docker management with VS Code
Log aggregation and analysis (working with AWS Athena in Java)
How to get values in real time with TextWatcher (Android)
Sample code to parse date and time with Java SimpleDateFormat
How to monitor application information in real time using JConsole
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
Summary of steps for developing in Docker container with VS Code
How to run a job with docker login in AWS batch
Getting Started with Docker with VS Code
Put Zabbix in Ubuntu with Docker and monitor Docker on the same host
Link Java and C ++ code with SWIG
Connect with VS Code from a Windows client to Docker on another server
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
Experience .NET 5 with Docker and Visual Studio Code
Try to link Ruby and Java with Dapr
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a Java (Maven) project with VS Code and develop it on a Docker container
How to implement UICollectionView in Swift with code only
Change date and time to Japanese notation in Rails
Introduce JavaFX 15 and do GUI development with VS Code
How to display a browser preview in VS Code
How to delete untagged images in bulk with Docker
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
Build WordPress environment with Docker (Local) and AWS (Production)
How to launch Swagger UI and Swagger Editor in Docker
With podman in docker, everyone wants to get along and use docker on a shared computer
Workspace setting location when connecting remotely with VS Code and working on a Docker container