Managing Docker containers efficiently often requires automation. In this guide, we share 20 commonly used Docker container script commands to help you streamline container management, reduce manual effort, and keep your environment stable and optimized.
1. View Image Details #
Get detailed information about a Docker image, such as size, creation date, and configuration.
#!/bin/bash
# View detailed information about an image
docker inspect $1
docker inspect
: Shows full metadata of a Docker image.
2. Limit Container Resources #
Prevent a single container from consuming too many system resources.
#!/bin/bash
# Limit container resources
docker run -d --memory="512m" --cpus="1" --name my_container my_image
--memory="512m"
: Limit memory usage to 512MB.--cpus="1"
: Limit usage to one CPU core.
3. Backup Container Data #
Export container data to a backup file.
#!/bin/bash
# Backup container data
CONTAINER_ID=$1
BACKUP_FILE="${CONTAINER_ID}_backup_$(date +%F).tar"
docker export $CONTAINER_ID > $BACKUP_FILE
echo "Backup saved to $BACKUP_FILE"
docker export
: Exports the filesystem of a container.- Backup file includes container ID + date.
4. Remove Stopped Containers #
Clean up exited containers automatically.
#!/bin/bash
# Remove all stopped containers
docker rm $(docker ps -aq -f "status=exited")
5. Enable Auto-Restart for Containers #
Keep containers running even after failure.
#!/bin/bash
# Enable auto-restart
CONTAINER_NAME=$1
docker update --restart always $CONTAINER_NAME
echo "$CONTAINER_NAME will now restart automatically."
6. Run and Clean Up Automatically #
Automatically remove a container after it exits.
#!/bin/bash
# Run container and auto-clean
IMAGE_NAME=$1
docker run --rm $IMAGE_NAME
--rm
: Automatically deletes the container after stopping.
7. List Image Layers of Containers #
Helpful for debugging and understanding image composition.
#!/bin/bash
# List image layers for all containers
docker inspect --format '{{.Id}}: {{.Image}}' $(docker ps -q)
8. Clean Unused Docker Resources #
Free up disk space by removing unused containers, images, networks, and volumes.
#!/bin/bash
# Clean unused resources
docker system prune -f --volumes
9. Remove Dangling Images #
Delete untagged and unused images.
#!/bin/bash
# Remove dangling images
docker rmi $(docker images -q -f "dangling=true")
10. Inspect Container Details #
Check container configuration, environment variables, and networking.
#!/bin/bash
# Inspect container details
CONTAINER_ID=$1
docker inspect $CONTAINER_ID
11. Monitor Container Resource Usage #
Track real-time resource usage for all containers.
#!/bin/bash
# Monitor container resources
docker stats --all
12. Restart All Containers #
Quickly restart all running containers.
#!/bin/bash
# Restart all containers
docker restart $(docker ps -q)
13. Start All Stopped Containers #
Bring back all containers with a single command.
#!/bin/bash
# Start all stopped containers
docker start $(docker ps -aq)
14. Copy Files From a Container #
Easily transfer files between host and container.
#!/bin/bash
# Copy files from container
CONTAINER_ID=$1
SOURCE_PATH=$2
DEST_PATH=$3
docker cp $CONTAINER_ID:$SOURCE_PATH $DEST_PATH
echo "Copied $SOURCE_PATH from $CONTAINER_ID to $DEST_PATH"
15. List Exposed Ports #
View container port mappings.
#!/bin/bash
# List exposed ports
docker ps --format '{{.ID}}: {{.Ports}}'
16. Restore Container From Backup #
Rebuild containers using backup tar files.
#!/bin/bash
# Restore container from backup
BACKUP_FILE=$1
docker import $BACKUP_FILE restored_container:latest
echo "Container restored as 'restored_container:latest'"
17. View Logs of All Containers #
Check logs for troubleshooting and monitoring.
#!/bin/bash
# Show logs of all containers
docker ps -q | xargs -I {} docker logs {}
18. Remove Paused Containers #
Clean up containers stuck in paused state.
#!/bin/bash
# Remove paused containers
docker rm $(docker ps -aq -f "status=paused")
19. Update Running Container #
Update a container to use the latest image version.
#!/bin/bash
# Update running container
CONTAINER_NAME=$1
IMAGE_NAME=$(docker inspect --format='{{.Config.Image}}' $CONTAINER_NAME)
docker pull $IMAGE_NAME
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
docker run -d --name $CONTAINER_NAME $IMAGE_NAME
20. Stop All Running Containers #
Shut down every running container at once.
#!/bin/bash
# Stop all running containers
docker stop $(docker ps -q)
Final Thoughts #
These 20 Docker container script commands cover everyday management tasks such as monitoring, resource optimization, backup, cleanup, and recovery. By integrating them into your DevOps workflow, you’ll save time, reduce manual effort, and keep your containerized applications running efficiently.