<p>I have a docker container which running in a pod where I need to restart the pod / container when it exceeds memory usage or CPU limit. How to configure it in docker file</p>

<p>CPU and memory limits cannot be given when building a docker and it cannot be configured in <code>Docker File</code>. It is a scheduling problem. You could run your docker with <code>docker run</code> command with different flags to control resources. See <a href="https://docs.docker.com/config/containers/resource_constraints/" rel="nofollow noreferrer">Docker Official Document</a> for those control flags for <code>docker run</code>.</p>

<p>As your question is tagged with <code>kubernetes</code>, there is <code>kubernetes</code> way to limit your resources. You would want to add <code>resources</code> in specs for those <code>deployment</code> or <code>pod</code> yaml. For example:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: your_deployment
  labels:
    app: your_app
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: your_app
    spec:
      containers:
      - name: your_container
        image: your_image
        resources:
          limits:
            cpu: "500m"
            memory: "128Mi"
          requests:
            cpu: "250m"
            memory: "64Mi"
        ...
</code></pre>

<p><code>requests</code> affects how docker pod is scheduled on nodes. <code>Memory limit</code> determines when the docker will be killed for OOM and <code>cpu limit</code> determines how the container cpu usage will be throttled (The pod will not be killed). </p>

<p>Meaning of <code>cpu</code> is different for each cloud service providers. For more information, please refer to <a href="https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/" rel="nofollow noreferrer">manage compute resources for Kubernetes</a></p>
