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...]
Code language: CSS (css)
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, orunless-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
Code language: JavaScript (javascript)
This limits the container to 1.5 CPU cores.
2. Set a Maximum Memory Limit
docker update --memory="512m" my_container
Code language: JavaScript (javascript)
This limits the container’s memory usage to 512MB.
3. Adjust Memory Swap Limit
docker update --memory="1g" --memory-swap="2g" my_container
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
This applies the same memory limit to multiple containers.
7. Increase CPU Allocation Dynamically
docker update --cpus="2.0" my_container
Code language: JavaScript (javascript)
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."
Code language: PHP (php)
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
alwaysfor 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
| Command | Description |
|---|---|
docker update --cpus="1.5" my_container | Limit CPU usage to 1.5 cores |
docker update --memory="512m" my_container | Set a maximum memory limit of 512MB |
docker update --memory="1g" --memory-swap="2g" my_container | Limit memory to 1GB with 2GB total (including swap) |
docker update --restart=always my_container | Set restart policy to always |
docker update --blkio-weight=800 my_container | Set block I/O priority to 800 |
docker update --cpus="2" container1 container2 | Increase CPU limit for multiple containers |
docker update --restart=no my_container | Remove the restart policy |
Best Practices for Using docker update:
- Monitor resource usage (
docker stats) before adjusting limits to avoid unintended performance degradation. - Apply limits cautiously, especially for memory and swap, to prevent container crashes.
- Use
--restart=alwaysfor critical services to ensure high availability. - Combine with orchestration tools (Docker Swarm, Kubernetes) for automated resource scaling.
- Regularly review resource limits and adjust based on application requirements.
Common Errors and Solutions
- “Error response from daemon: Cannot update a stopped container”
→ Ensure the container is running. Start it withdocker start. - “Error: Cannot set memory limit below current usage”
→ Check the container’s current memory usage and set a higher limit. - “Permission denied”
→ Ensure you have sufficient privileges. Usesudoif necessary. - “Restart policy update failed”
→ Verify the container’s status and ensure it is running before updating the restart policy.
Combining docker update with Monitoring
- Monitor Resource Usage with
docker stats:docker stats my_container - Update CPU or Memory Limits Dynamically:
docker update --cpus="2" --memory="1g" my_container
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND