How to start the cloudwatch agent in container?

You just to run the container with log-opt, as the log agent is the main process of the container.

docker run --log-driver=awslogs --log-opt awslogs-region=us-west-2 --log-opt awslogs-group=myLogGroup amazon/cloudwatch-agent

You can find more details here and here.

I do not know why you need an agent in a container, but the best practice is to send each container log directly to cloud watch using aws log driver.

Btw this is entrypoint of the container.

  "Entrypoint": [
         "/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"
 ],

All you need to call

/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent

I got this working! I was having the same issue with you when you see Reading json config file path: /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json ... Cannot access /etc/cwagentconfig: lstat /etc/cwagentconfig: no such file or directoryValid Json input schema.

What you need to do is put your config file in /etc/cwagentconfig. A functioning dockerfile:

FROM amazon/cloudwatch-agent:1.230621.0
COPY config.json /etc/cwagentconfig

Where config.json is some cloudwatch agent configuration, such as given by LinPy's answer.

You can ignore the warning about /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json, or you can also COPY the config.json file to that location in the dockerfile as well.

I will also share how I found this answer:

I needed this run in ECS as a sidecar, and I could only find docs on how to run it in kubernetes. Following this documentation: https://docs.aws.amazon.com/en_pv/AmazonCloudWatch/latest/monitoring/Container-Insights-setup-StatsD.html I decided to download all the example k8s manifests, when I saw this one:

apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: amazonlinux
spec:
  containers:
    - name: amazonlinux
      image: amazonlinux
      command: ["/bin/sh"]
      args: ["-c", "sleep 300"]
    - name: cloudwatch-agent
      image: amazon/cloudwatch-agent
      imagePullPolicy: Always
      resources:
        limits:
          cpu:  200m
          memory: 100Mi
        requests:
          cpu: 200m
          memory: 100Mi
      volumeMounts:
        - name: cwagentconfig
          mountPath: /etc/cwagentconfig
  volumes:
    - name: cwagentconfig
      configMap:
        name: cwagentstatsdconfig
  terminationGracePeriodSeconds: 60

So I saw that the volume mount cwagentconfig mounts to /etc/cwagentconfig and that's from the cwagentstatsdconfig configmap, and that's just the json file.