# 15 Scripts to Automate Docker Container Management

Link: [https://blog.devops.dev/15-scripts-to-automate-docker-container-management-4bab4c3faf73](https://blog.devops.dev/15-scripts-to-automate-docker-container-management-4bab4c3faf73)

<div class="eq er es et eu l" id="bkmrk-each-example-comes-w"><article class="meteredContent"><div class="l"><div class="l"><section>## Each example comes with functioning code and detailed explanations.

## 1. Automatically Start All Containers

Sometimes after a system reboot or maintenance, you may want to start all stopped containers at once.

```
#!/bin/bash<br></br># Start all stopped containers<br></br>docker start $(docker ps -aq) <br></br>
```

\- ‘docker ps -aq’ lists all container IDs (stopped and running).  
\- ‘docker start’ starts the containers by passing the IDs as arguments.

## 2. Stop All Running Containers

Quickly stop all currently running containers.

```
#!/bin/bash<br></br># Stop all running containers<br></br>docker stop $(docker ps -q) <br></br>
```

\- ‘docker ps -q’ lists IDs of only running containers.  
\- ‘docker stop’ stops these containers.

## 3. Remove Stopped Containers

Free up space by cleaning up stopped containers.

```
#!/bin/bash<br></br># Remove all stopped containers<br></br>docker rm $(docker ps -aq -f "status=exited") <br></br>
```

\- `docker ps -aq -f "status=exited"` filters stopped containers.  
\- ‘docker rm’ removes them.

## 4. Remove Dangling Images

Clear unused Docker images to save disk space.

```
#!/bin/bash<br></br># Remove dangling images<br></br>docker rmi $(docker images -q -f "dangling=true") <br></br>
```

\- `docker images -q -f "dangling=true"` lists image IDs with no tags (dangling).  
\- ‘docker rmi’ removes these images.

## 5. Backup a Container’s Data

Export the filesystem of a running container to a tar file.

```
#!/bin/bash<br></br># Backup a container's data<br></br>CONTAINER_ID=$1<br></br>BACKUP_FILE="${CONTAINER_ID}_backup_$(date +%F).tar"<br></br>docker export $CONTAINER_ID > $BACKUP_FILE<br></br>echo "Backup saved to $BACKUP_FILE" <br></br>
```

\- ‘docker export’ exports the filesystem of the container.  
\- Pass the container ID as an argument to the script.

## 6. Restore a Container from Backup

Recreate a container from a tar backup file.

```
#!/bin/bash<br></br># Restore a container from a tar backup<br></br>BACKUP_FILE=$1 <br></br>docker import $BACKUP_FILE restored_container:latest<br></br>echo "Container restored as 'restored_container:latest'"<br></br>
```

\- ‘docker import’ creates a new image from the tar file.  
\- The image can be used to start new containers.

## 7. Monitor Container Resource Usage

Display real-time stats for all running containers.

```
#!/bin/bash<br></br># Monitor resource usage of all running containers<br></br>docker stats --all <br></br>
```

\- ‘docker stats’ shows real-time CPU, memory, and network stats.  
\- ‘--all’ includes stopped containers.

## 8. Restart a Container Automatically

Ensure critical containers restart after failure.

```
#!/bin/bash<br></br># Restart a container with restart policy<br></br>CONTAINER_NAME=$1 <br></br>docker update --restart always $CONTAINER_NAME<br></br>echo "$CONTAINER_NAME will now restart automatically on failure."<br></br>
```

\- ‘docker update --restart always’ configures the restart policy.  
\- Pass the container name as an argument.

## 9. Run a Container and Clean Up After Exit

Automatically remove a container after it stops.

```
#!/bin/bash<br></br># Run a container and clean up<br></br>IMAGE_NAME=$1<br></br>docker run --rm $IMAGE_NAME <br></br>
```

\- ‘--rm’ removes the container when it stops.  
\- Useful for one-off tasks.

## 10. Check Logs of All Containers

Combine logs from multiple containers into one output.

```
#!/bin/bash<br></br># Display logs of all containers<br></br>docker ps -q | xargs -I {} docker logs {} <br></br>
```

\- ‘docker ps -q’ lists running container IDs.  
\- ‘xargs’ passes these IDs to ‘docker logs’.

## 11. Auto-Prune Unused Resources

Schedule automated cleanup of unused Docker resources.

```
#!/bin/bash<br></br># Prune unused resources<br></br>docker system prune -f --volumes <br></br>
```

\- ‘docker system prune’ removes unused containers, networks, and images.  
\- ‘--volumes’ also deletes unused volumes.

## 12. Update Running Containers

Recreate containers with the latest image version.

```
#!/bin/bash<br></br># Update a running container<br></br>CONTAINER_NAME=$1<br></br>IMAGE_NAME=$(docker inspect --format='{{.Config.Image}}' $CONTAINER_NAME)<br></br>docker pull $IMAGE_NAME<br></br>docker stop $CONTAINER_NAME<br></br>docker rm $CONTAINER_NAME<br></br>docker run -d --name $CONTAINER_NAME $IMAGE_NAME <br></br>
```

\- ‘docker inspect’ fetches the image name of a container.  
\- The script pulls the latest image and recreates the container.

## 13. Copy Files from a Container

Extract files or directories from a container to the host.

```
#!/bin/bash<br></br># Copy files from a container<br></br>CONTAINER_ID=$1<br></br>SOURCE_PATH=$2<br></br>DEST_PATH=$3 <br></br>docker cp $CONTAINER_ID:$SOURCE_PATH $DEST_PATH<br></br>echo "Copied $SOURCE_PATH from $CONTAINER_ID to $DEST_PATH"<br></br>
```

\- ‘docker cp’ copies files between the container and the host.  
\- Pass container ID, source path, and destination path as arguments.

## 14. Restart All Containers

Restart all running containers quickly.

```
#!/bin/bash<br></br># Restart all containers<br></br>docker restart $(docker ps -q) <br></br>
```

\- ‘docker restart’ restarts containers by their IDs.

## 15. List All Exposed Ports

Check the exposed ports of running containers.

```
#!/bin/bash<br></br># List all exposed ports<br></br>docker ps --format '{{.ID}}: {{.Ports}}' <br></br>
```

\- ‘docker ps --format’ customizes the output to show container IDs and ports.

Feel free to tweak, experiment and customize them to your needs.

</section></div></div></article></div><div class="ab cb" id="bkmrk-docker-bash-bash-scr"><div class="ci bh ev ew ex ey"><div class="px py ab jk"><div class="pz ab">[<div class="qb ed cx qc fa qd qe bf b bg z bk qf">Docker</div>](https://medium.com/tag/docker?source=post_page-----4bab4c3faf73--------------------------------)</div><div class="pz ab">[<div class="qb ed cx qc fa qd qe bf b bg z bk qf">Bash</div>](https://medium.com/tag/bash?source=post_page-----4bab4c3faf73--------------------------------)</div><div class="pz ab">[<div class="qb ed cx qc fa qd qe bf b bg z bk qf">Bash Script</div>](https://medium.com/tag/bash-script?source=post_page-----4bab4c3faf73--------------------------------)</div><div class="pz ab">[<div class="qb ed cx qc fa qd qe bf b bg z bk qf">Programming</div>](https://medium.com/tag/programming?source=post_page-----4bab4c3faf73--------------------------------)</div></div></div></div>