See posts by tags

15 Bash Scripts to Simplify Docker Container Management

  • 3 min read
  • 26 Jan, 2025

1. Stop All Running Containers

Halt all currently active containers in a single command:

#!/bin/bash
# Stop all running containers
docker stop $(docker ps -q)
  • docker ps -q: Retrieves IDs of active containers.
  • docker stop: Stops these containers using their IDs.

2. Automatically Start All Containers

After a reboot or maintenance, restart all containers effortlessly:

#!/bin/bash
# Start all stopped containers
docker start $(docker ps -aq)
  • docker ps -aq: Lists all container IDs, including stopped ones.
  • docker start: Initiates all containers by their IDs.

3. Remove Stopped Containers

Clear unnecessary stopped containers to free up space:

#!/bin/bash
# Remove all stopped containers
docker rm $(docker ps -aq -f "status=exited")
  • docker ps -aq -f "status=exited": Filters only stopped containers.
  • docker rm: Deletes these containers.

4. Monitor Resource Usage of Containers

Keep track of your containers' CPU, memory, and network usage in real-time:

#!/bin/bash
# Monitor resource usage of all running containers
docker stats --all
  • docker stats: Provides live statistics for containers.
  • --all: Includes both running and stopped containers.

5. Remove Dangling Images

Free up disk space by deleting unused images:

#!/bin/bash
# Remove dangling images
docker rmi $(docker images -q -f "dangling=true")
  • docker images -q -f "dangling=true": Finds unused images.
  • docker rmi: Removes these dangling images.

6. Backup a Container’s Data

Create a backup of a container’s filesystem as a tar archive:

#!/bin/bash
# Backup a container's 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: Saves the container’s filesystem as a file.
  • Specify the container ID as an argument.

7. Restore a Container from Backup

Rebuild a container from a backup tar file:

#!/bin/bash
# Restore a container from a tar backup
BACKUP_FILE=$1
docker import $BACKUP_FILE restored_container:latest
echo "Container restored as 'restored_container:latest'"
  • docker import: Converts the tar file into a Docker image.
  • The image can be used to create new containers.

8. Run a Temporary Container

Execute a container that automatically removes itself after stopping:

#!/bin/bash
# Run a container and clean up
IMAGE_NAME=$1
docker run --rm $IMAGE_NAME
  • --rm: Deletes the container when it stops.
  • Perfect for one-time tasks.

9. Auto-Prune Unused Resources

Schedule regular cleanup of unused Docker items:

#!/bin/bash
# Prune unused resources
docker system prune -f --volumes
  • docker system prune: Removes unused containers, images, and networks.
  • --volumes: Deletes unused volumes as well.

10. Check Logs of All Containers

View logs from multiple containers in one place:

#!/bin/bash
# Display logs of all containers
docker ps -q | xargs -I {} docker logs {}
  • docker ps -q: Gathers IDs of active containers.
  • xargs: Passes IDs to the docker logs command.

11. Restart All Containers

Quickly restart all running containers:

#!/bin/bash
# Restart all containers
docker restart $(docker ps -q)
  • docker restart: Stops and restarts containers using their IDs.

12. Update Running Containers

Upgrade containers to use the latest version of their image:

#!/bin/bash
# Update a 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
  • docker inspect: Retrieves the image name of a container.
  • Pulls the latest image and recreates the container.

13. Copy Files from a Container

Extract files or directories from a container to your host system:

#!/bin/bash
# Copy files from a 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"
  • docker cp: Transfers files between container and host.
  • Provide container ID, source, and destination paths as inputs.

14. List All Exposed Ports

View the exposed ports of running containers:

#!/bin/bash
# List all exposed ports
docker ps --format '{{.ID}}: {{.Ports}}'
  • docker ps --format: Customizes output to show container IDs and ports.

15. Restart a Container Automatically

Set a container to restart on failure automatically:

#!/bin/bash
# Restart a container with restart policy
CONTAINER_NAME=$1
docker update --restart always $CONTAINER_NAME
echo "$CONTAINER_NAME will now restart automatically on failure."
  • docker update --restart always: Configures the restart policy.
  • Input the container name as an argument.