How to use docker container update command to modify container resources

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to dynamically modify the resource limits and restart policy of a running Docker container using the docker container update command. You will begin by creating a container with initial CPU and memory limits.

Following the container creation, you will explore how to update the container's CPU shares and memory limit to adjust its resource allocation. Finally, you will learn how to modify the container's restart policy, controlling how Docker handles container restarts in different scenarios. This hands-on experience will demonstrate the flexibility and power of managing container resources in Docker.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555127{{"How to use docker container update command to modify container resources"}} docker/rm -.-> lab-555127{{"How to use docker container update command to modify container resources"}} docker/exec -.-> lab-555127{{"How to use docker container update command to modify container resources"}} docker/inspect -.-> lab-555127{{"How to use docker container update command to modify container resources"}} docker/pull -.-> lab-555127{{"How to use docker container update command to modify container resources"}} end

Create a container with initial resource limits

In this step, you will learn how to create a Docker container and set initial resource limits for CPU and memory. Resource limits are important for preventing a single container from consuming all available resources on the host machine, which can impact the performance of other containers and the host itself.

First, let's pull the ubuntu image from Docker Hub. This image will be used as the base for our container.

docker pull ubuntu:latest

You should see output indicating that the image is being pulled and extracted.

Now, let's create a container named my-limited-container with specific resource limits. We will use the docker run command with the --cpus and --memory flags.

The --cpus flag limits the amount of CPU resources the container can use. The value is a floating-point number representing the number of CPU cores. For example, --cpus 0.5 means the container can use at most half of a CPU core.

The --memory flag limits the amount of memory the container can use. The value can be specified in bytes, kilobytes (k), megabytes (m), or gigabytes (g). For example, --memory 512m limits the container to 512 megabytes of memory.

In this example, we will create a container that runs the sleep infinity command, which keeps the container running indefinitely. We will limit its CPU usage to 0.5 cores and memory to 256 megabytes.

docker run -d --name my-limited-container --cpus 0.5 --memory 256m ubuntu:latest sleep infinity

Let's break down the command:

  • docker run: This command is used to create and start a new container.
  • -d: This flag runs the container in detached mode, meaning it runs in the background.
  • --name my-limited-container: This assigns the name my-limited-container to the container.
  • --cpus 0.5: This limits the container's CPU usage to 0.5 cores.
  • --memory 256m: This limits the container's memory usage to 256 megabytes.
  • ubuntu:latest: This specifies the Docker image to use (the latest version of Ubuntu).
  • sleep infinity: This is the command that will be executed inside the container. It keeps the container running.

After running the command, Docker will output the container ID.

To verify that the container is running and the resource limits are applied, you can use the docker inspect command. This command provides detailed information about a container.

docker inspect my-limited-container

The output of docker inspect is a large JSON object. You can look for the CpuShares and Memory fields within the HostConfig section to see the applied limits. Note that --cpus 0.5 is translated to CpuShares value of 512 (by default, 1 CPU is 1024 shares). The Memory value will be in bytes.

Update the container's CPU shares

In this step, you will learn how to update the CPU shares of a running container using the docker update command. CPU shares are a way to prioritize container access to CPU cycles when the system is under load. A higher share value means the container gets a larger proportion of CPU time compared to containers with lower shares.

In the previous step, we created a container named my-limited-container with a CPU limit of 0.5 cores, which corresponds to 512 CPU shares. Now, let's update its CPU shares to 768. This will give it a higher priority for CPU resources compared to other containers with default shares (1024) or lower shares.

To update the CPU shares, we use the docker update command with the --cpu-shares flag, followed by the container name and the new share value.

docker update --cpu-shares 768 my-limited-container

You should see the container name printed as output, indicating that the update was successful.

To verify that the CPU shares have been updated, you can again use the docker inspect command and check the CpuShares field in the HostConfig section.

docker inspect my-limited-container

Look for the CpuShares field in the output. Its value should now be 768.

It's important to note that updating CPU shares does not require restarting the container. The changes are applied dynamically to the running container.

Update the container's memory limit

In this step, you will learn how to update the memory limit of a running container using the docker update command. Limiting memory usage is crucial to prevent containers from consuming excessive amounts of RAM, which can lead to performance issues or even system instability on the host.

In the first step, we created the my-limited-container with a memory limit of 256 megabytes. Now, let's increase its memory limit to 512 megabytes.

To update the memory limit, we use the docker update command with the --memory flag, followed by the container name and the new memory limit value.

docker update --memory 512m my-limited-container

You should see the container name printed as output, indicating that the update was successful.

To verify that the memory limit has been updated, you can use the docker inspect command and check the Memory field in the HostConfig section.

docker inspect my-limited-container

Look for the Memory field in the output. Its value should now be 536870912 (which is 512 megabytes in bytes).

Similar to updating CPU shares, updating the memory limit of a running container does not require restarting the container. The changes are applied dynamically.

Update the container's restart policy

In this step, you will learn how to update the restart policy of a container using the docker update command. A restart policy determines how a container should behave when it exits or when the Docker daemon restarts. This is important for ensuring the availability of your applications running in containers.

By default, containers have a restart policy of no, meaning they will not automatically restart after exiting. Let's update the my-limited-container to have a restart policy of on-failure. This policy will restart the container only if it exits with a non-zero exit code (indicating an error).

To update the restart policy, we use the docker update command with the --restart flag, followed by the container name and the desired policy.

docker update --restart on-failure my-limited-container

You should see the container name printed as output, indicating that the update was successful.

To verify that the restart policy has been updated, you can use the docker inspect command and check the RestartPolicy field within the HostConfig section.

docker inspect my-limited-container

Look for the RestartPolicy field in the output. The Name field within RestartPolicy should now be on-failure.

Other common restart policies include:

  • no: Do not automatically restart the container.
  • on-failure[:max-retries]: Restart the container only if it exits with a non-zero exit code. Optionally limit the number of restart attempts.
  • always: Always restart the container if it stops.
  • unless-stopped: Always restart the container unless it is explicitly stopped.

Updating the restart policy also does not require restarting the container. The change is applied dynamically.

Finally, let's clean up the container we created.

docker stop my-limited-container
docker rm my-limited-container

This stops and removes the container.

Summary

In this lab, you learned how to manage Docker container resources using the docker update command. You began by creating a container with initial CPU and memory limits using docker run with the --cpus and --memory flags. This demonstrated how to set resource constraints during container creation to prevent resource starvation on the host.

Subsequently, you explored how to dynamically modify these resource limits on a running container. You learned to update the container's CPU shares using docker update --cpu-shares and adjust the memory limit with docker update --memory. Finally, you practiced changing the container's restart policy using docker update --restart, illustrating how to control the container's behavior in case of failures. These steps provided practical experience in using the docker update command to fine-tune container resource allocation and behavior after creation.