Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker update with examples

Here’s a complete tutorial on docker update, covering what it does, examples, and use cases.


What is docker update?

docker update is a Docker command used to modify the resource limits or configuration settings of one or more running containers. Unlike docker run, which sets these limits at container creation, docker update allows you to adjust them on the fly without stopping the container.

Key Features:

  • Adjust CPU, memory, block I/O, and restart policies for running containers.
  • Helps manage resource allocation dynamically.
  • Supports updating multiple containers at once.

Basic Syntax

docker update [OPTIONS] CONTAINER [CONTAINER...]

Common Options:

  • --cpus: Limit the CPU usage for the container (e.g., --cpus="1.5").
  • --memory: Set the maximum memory the container can use (e.g., --memory="512m").
  • --memory-swap: Set the total memory plus swap for the container.
  • --restart: Update the restart policy (no, always, on-failure, or unless-stopped).
  • --blkio-weight: Set the block I/O weight (relative I/O priority).

Examples of docker update

1. Limit CPU Usage for a Running Container

docker update --cpus="1.5" my_container

This limits the container to 1.5 CPU cores.


2. Set a Maximum Memory Limit

docker update --memory="512m" my_container

This limits the container’s memory usage to 512MB.


3. Adjust Memory Swap Limit

docker update --memory="1g" --memory-swap="2g" my_container

This sets the container’s memory limit to 1GB and total memory + swap to 2GB.


4. Change the Restart Policy

docker update --restart=always my_container

This updates the container’s restart policy to always, ensuring it restarts automatically if it stops.


5. Set Block I/O Weight

docker update --blkio-weight=500 my_container

This sets the block I/O priority for the container, with values ranging from 10 (low priority) to 1000 (high priority). The default is 500.


6. Update Multiple Containers at Once

docker update --memory="1g" container1 container2 container3

This applies the same memory limit to multiple containers.


7. Increase CPU Allocation Dynamically

docker update --cpus="2.0" my_container

If a running container needs more CPU power, you can increase its limit without stopping it.


8. Remove a Restart Policy

docker update --restart=no my_container

This removes the restart policy, meaning the container won’t automatically restart if it stops.


9. Use docker update in a Shell Script

#!/bin/bash
docker update --memory="512m" --cpus="1" web_container
docker update --memory="1g" --cpus="2" db_container
echo "Resource limits updated for web and database containers."

This script updates memory and CPU limits for two specific containers.


Use Cases for docker update

1. Dynamic Resource Management

  • Adjust resource limits for containers based on system load or performance requirements.
  • Example: Increase memory for a database container during peak usage.

2. Performance Tuning

  • Optimize CPU and I/O allocation for critical containers to ensure consistent performance.
  • Example: Give high-priority services more CPU and I/O resources.

3. System Recovery and Stability

  • Prevent system crashes by limiting memory usage for containers that consume too much memory.
  • Example: Set a strict memory limit for a container running a resource-intensive task.

4. Temporary Scaling

  • Increase CPU or memory limits temporarily to handle high traffic and reduce them later.
  • Example: Allocate more resources to a web server during a flash sale.

5. Restart Policy Updates

  • Change the restart policy for containers in production environments to ensure high availability.
  • Example: Set the restart policy to always for critical services.

6. Batch Processing and Background Tasks

  • Manage CPU and memory usage for batch jobs or data processing to avoid overloading the host system.

List of Common docker update Commands

CommandDescription
docker update --cpus="1.5" my_containerLimit CPU usage to 1.5 cores
docker update --memory="512m" my_containerSet a maximum memory limit of 512MB
docker update --memory="1g" --memory-swap="2g" my_containerLimit memory to 1GB with 2GB total (including swap)
docker update --restart=always my_containerSet restart policy to always
docker update --blkio-weight=800 my_containerSet block I/O priority to 800
docker update --cpus="2" container1 container2Increase CPU limit for multiple containers
docker update --restart=no my_containerRemove the restart policy

Best Practices for Using docker update:

  1. Monitor resource usage (docker stats) before adjusting limits to avoid unintended performance degradation.
  2. Apply limits cautiously, especially for memory and swap, to prevent container crashes.
  3. Use --restart=always for critical services to ensure high availability.
  4. Combine with orchestration tools (Docker Swarm, Kubernetes) for automated resource scaling.
  5. Regularly review resource limits and adjust based on application requirements.

Common Errors and Solutions

  1. “Error response from daemon: Cannot update a stopped container”
    → Ensure the container is running. Start it with docker start.
  2. “Error: Cannot set memory limit below current usage”
    → Check the container’s current memory usage and set a higher limit.
  3. “Permission denied”
    → Ensure you have sufficient privileges. Use sudo if necessary.
  4. “Restart policy update failed”
    → Verify the container’s status and ensure it is running before updating the restart policy.

Combining docker update with Monitoring

  1. Monitor Resource Usage with docker stats: docker stats my_container
  2. Update CPU or Memory Limits Dynamically: docker update --cpus="2" --memory="1g" my_container

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x