Categories
How-To Technical

Configuring CPU Affinity for Docker Containers

Why is this needed

If your system supports SMP (Symmetric multiprocessing) with some combination of multiple physical CPUs, CPU cores, and logical CPUs, you can achieve increased performance in some cases by assigning Docker Containers to specific CPU resources. This process is known as CPU affinity (or processor affinity).

Gather Required Information

To assign a Docker container to a CPU (or set of CPUs), you require the following information:

  • The name of the target Docker container.
  • The amount of CPUs present on the system.
  • The configuration of CPU assignments you wish to use.

Example Commands for CPU affinity with Docker Containers

The examples shown here cover the assignment of the yourcontainer Docker Container to specific CPU resources when the containers are created with the docker run command. When running commands, you must substitute your Docker Container name and CPU component numbers to suit your environment.

  • This command will assign the yourcontainer Docker Container to the first CPU (CPU0):

# docker run --cpuset 0 /bin/bash yourcontainer
  • Multiple CPUs can be specified. This command will assign the yourcontainer Docker Container to CPU 0 and 1:

# docker run --cpuset 0,1 /bin/bash yourcontainer
  • A range of CPUs can be specified. This command will assign the yourcontainer Docker Container to CPU 0, 1 and 2:

# docker run --cpuset 0-2 /bin/bash yourcontainer