# Configuração e Monitoração Docker

Processos e monitoração Docker.

# Bash script to log and restart docker container based on cpu usage

Link: [https://dev.to/tomasggarcia/bash-script-to-log-and-restart-docker-container-cpu-usage-1j2o](https://dev.to/tomasggarcia/bash-script-to-log-and-restart-docker-container-cpu-usage-1j2o)

I wrote this post to share with you one job experience that I had to live with recently, one problem I had and my temporal solution to this problem.  
I'm new in this tech world, I will be grateful if any of you have some improvement recommendations and very pleased if this post it's useful for someone else.  
A few mounths ago in my company we discovered that some Docker container was having problems with CPU usage. Out of nowhere the CPU usage of that container was increasing abruptly. So while the dev team was searching for the code error, I implemented a temporary solution. I made one script to log all cpu usage every 5 seconds:

```
#!/bin/bash
logs=/var/log/process_name.log
container_name=container_name

while :
do
  # Get a variable with the cpu usage for a specific container
  var=`docker stats --no-stream --format "{{.CPUPerc}}" $container_name`
  length=${#var}

  if (( $length==0 )); then
     echo "Container ${container_name} does not exist"
     echo "$(date +'%d-%m-%Y %H:%M') | Container $container_name does not exist" >> $logs
  else
    # CPU usage in number
    percent="${var[@]::-4}"

    echo "Actual cpu usage: ${percent}"

    # Save actual CPU usage in file
    echo "$(date +'%d-%m-%Y %H:%M') | ${percent}" >> $logs
  fi
  sleep 5
```

<div class="highlight js-code-highlight" id="bkmrk-" style="text-align: justify;"><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action"><svg class="highlight-action crayons-icon highlight-action--fullscreen-on" height="20px" viewbox="0 0 24 24" width="20px" xmlns="http://www.w3.org/2000/svg"><path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path></svg></div></div></div>After that I created a supervisor config to run this process:

```
[program:process]
command=/opt/scripts/script.sh
autostart=true
autorestart=true
stderr_logfile=/var/log/process.err.log
stdout_logfile=/var/log/process.err.log
```

<div class="highlight js-code-highlight" id="bkmrk--1" style="text-align: justify;"><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action"><svg class="highlight-action crayons-icon highlight-action--fullscreen-on" height="20px" viewbox="0 0 24 24" width="20px" xmlns="http://www.w3.org/2000/svg"><path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path></svg></div></div></div>Then I wrote a script to restart the problematic container, based on the logs of the previous script:

```
#!/bin/bash
container_name=container_name
logs_evaluated_lines=5
logs=/var/log/process_name.log
max_cpu=90

while :
do
    # Lines in file
    num=$(wc -l < $logs)

    counter=0

    # For 'logs_evaluated_lines' lines in logs increase counter if cpu is greater than 100%
    for ((index=$num;index>=$num-$logs_evaluated_lines+1;index--))
    do
    value=$(sed "${index}q;d" $logs)
    percent=$(echo $value | cut -c 20-)
    #echo $percent
    if (( $percent >= max_cpu )); then
    #    echo 'mayor'
        counter=$((counter+1))
    #  else
    #    echo 'menor'
    fi
    done

    echo "$(date +'%d-%m-%Y %H:%M') | Logs up to 100%: ${counter}"
    echo "$(date +'%d-%m-%Y %H:%M') | Logs lines analyzed: ${logs_evaluated_lines}"
    if (( $counter == $logs_evaluated_lines )); then
        echo "$(date +'%d-%m-%Y %H:%M') | CPU Full usage";
        echo "$(date +'%d-%m-%Y %H:%M') | Restarting Container"
        docker restart $container_name
        echo "$(date +'%d-%m-%Y %H:%M') | Container Restarted"
        echo "$(date +'%d-%m-%Y %H:%M') | Container Restarted" >> $logs
    else
        echo "$(date +'%d-%m-%Y %H:%M') | CPU Usage OK"
    fi
    echo "$(date +'%d-%m-%Y %H:%M') |"
    sleep 5
done
```

<div class="highlight js-code-highlight" id="bkmrk--2" style="text-align: justify;"><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action"><svg class="highlight-action crayons-icon highlight-action--fullscreen-on" height="20px" viewbox="0 0 24 24" width="20px" xmlns="http://www.w3.org/2000/svg"><path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path></svg></div></div></div>This script evaluate 'logs\_evaluated\_lines' lines in log and restarts the container if the count is upper 'max\_cpu' variable

# how to get docker stats using shell script

Link: [https://iqcode.com/code/typescript/how-to-get-docker-stats-using-shell-script](https://iqcode.com/code/typescript/how-to-get-docker-stats-using-shell-script)

```html
#!/bin/bash

# This script is used to complete the output of the docker stats command.
# The docker stats command does not compute the total amount of resources (RAM or CPU)

# Get the total amount of RAM, assumes there are at least 1024*1024 KiB, therefore > 1 GiB
HOST_MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2/1024/1024}')

# Get the output of the docker stat command. Will be displayed at the end
# Without modifying the special variable IFS the ouput of the docker stats command won't have
# the new lines thus resulting in a failure when using awk to process each line
IFS=;
DOCKER_STATS_CMD=`docker stats --no-stream --format "table {{.MemPerc}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.Name}}"`

SUM_RAM=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
SUM_CPU=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
SUM_RAM_QUANTITY=`LC_NUMERIC=C printf %.2f $(echo "$SUM_RAM*$HOST_MEM_TOTAL*0.01" | bc)`

# Output the result
echo $DOCKER_STATS_CMD
echo -e "${SUM_RAM}%\t\t\t${SUM_CPU}%\t\t${SUM_RAM_QUANTITY}GiB / ${HOST_MEM_TOTAL}GiB\tTOTAL"
```

# Docker Stats | Understand how to monitor Docker Metrics with docker stats

<header id="bkmrk-link%3A-https%3A%2F%2Fsignoz"><span style="text-align: justify; color: rgb(68, 68, 68);">Link: [https://signoz.io/blog/docker-stats/](https://signoz.io/blog/docker-stats/)</span></header><header id="bkmrk-"></header><header id="bkmrk-docker-containers-ar"><span style="text-align: justify; color: rgb(68, 68, 68);">Docker containers are transient (lasting for a very short time), spawning quickly and in high numbers, which causes metrics bursts. This makes monitoring a challenge due to Docker's scaling and redeployment features. Docker stats is a built-in feature of Docker containers. The </span>`docker stats`<span style="text-align: justify; color: rgb(68, 68, 68);"> command returns a live data stream of your running containers.</span><div class="margin-top--md margin-bottom--sm row"></div></header>  
Docker is a containerization platform that lets you separate your applications from your infrastructure to deliver software quickly. To monitor Docker, it is crucial to gather performance-related metrics from various system elements, such as containers, hosts, and databases.

## Methods of monitoring Docker Metrics[​](https://signoz.io/blog/docker-stats/#methods-of-monitoring-docker-metrics "Direct link to Methods of monitoring Docker Metrics")

There are several ways in which Docker metrics can be monitored. These include;

<div class="markdown undefined" id="bkmrk-docker-stats-command" itemprop="articleBody" style="text-align: justify;">- Docker stats command
- Pseudo-files in sysfs
- REST API exposed by the Docker daemon

</div>Note that these are in-built features of Docker.

> Observability for your containerized application  
> Observability is critical for modern cloud-native applications. It helps engineering teams have more confidence in their production environment. Troubleshooting performance issues is easier with a robust observability framework in place. [SigNoz](https://signoz.io/), an open-source observability tool, can help make your containerized applications observable.

To get started with SigNoz, please visit the documentation.

In this article, we will deep dive into the `docker stats` command that can be used to monitor Docker metrics right from the terminal.

## What is the `docker stats` command?[​](https://signoz.io/blog/docker-stats/#what-is-the-docker-stats-command "Direct link to what-is-the-docker-stats-command")

The `docker stats` command is a built-in feature of Docker that displays resource consumption statistics for the container in real-time. By default, it shows CPU and memory utilization for all containers. Stats here refers to “statistics”. You can restrict the statistics by entering the container names or IDs you're interested in monitoring. The `docker stats` command output includes CPU stats, memory metrics, block I/O, and network IO metrics for all active containers.

### A Practical Approach[​](https://signoz.io/blog/docker-stats/#a-practical-approach "Direct link to A Practical Approach")

Running the `docker stats` command produces an output that looks like the code snippet below. This command shows the stats for `all` the running Docker containers.

```
$ docker stats

CONTAINER ID         NAME                  CPU %          MEM USAGE / LIMIT       MEM%            NET I/O              BLOCK I/O           PIDS
56b3f523b0sd         nginx-container       0.35%          2.534MiB / 16.455GiB    0.37%           568B / 0B            134kb / 0B          3
049996113bc8         ubuntu                0.14%          1.437MiB / 16.455GiB    0.10%           3.56kb / 0B          5.12MB / 0B         1
a3f78cb32a8e         hello-world           0.00%          1.228MiB / 16.455GiB    0.06%           65.45kb / 0B         550kb / 0B          0

```

<div class="markdown undefined" id="bkmrk--1" itemprop="articleBody" style="text-align: justify;"><div class="language-visual-basic codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Toggle word wrap" class="clean-btn" title="Toggle word wrap" type="button"><svg aria-hidden="true" class="wordWrapButtonIcon_Bwma" viewbox="0 0 24 24"><path d="M4 19h6v-2H4v2zM20 5H4v2h16V5zm-3 6H4v2h13.25c1.1 0 2 .9 2 2s-.9 2-2 2H15v-2l-3 3l3 3v-2h2c2.21 0 4-1.79 4-4s-1.79-4-4-4z" fill="currentColor"></path></svg></button><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div>## Understanding the `docker stats` command output[​](https://signoz.io/blog/docker-stats/#understanding-the-docker-stats-command-output "Direct link to understanding-the-docker-stats-command-output")

The `docker stats` command returns a live snapshot of resource usage by Docker containers. Let’s break down all the stats given by the command.

### CPU% stats[​](https://signoz.io/blog/docker-stats/#cpu-stats "Direct link to CPU% stats")

CPU is expressed as a percentage (%) of the overall host capacity. One can optimize the resource usage of Docker hosts by being aware of how much CPU the hosts and containers consume. One active/busy container shouldn't slow down other containers by consuming all of the CPU resources. Containers can be optimized based on the amount of CPU they are using.

### MEM USAGE / LIMIT Stats[​](https://signoz.io/blog/docker-stats/#mem-usage--limit-stats "Direct link to MEM USAGE / LIMIT Stats")

`MEM USAGE` lists the available memory. It gives a quick overview of the container's memory usage and allocation, providing information about the container's memory statistics, including usage and memory limit. Except when it is defined for a specific container, the memory usage limit corresponds to the host machine's memory limit.

### MEM % Stat[​](https://signoz.io/blog/docker-stats/#mem--stat "Direct link to MEM % Stat")

`MEM %` shows the memory percentage that the container is using from its host machine.

### Network(NET) I/O Stats[​](https://signoz.io/blog/docker-stats/#networknet-io-stats "Direct link to Network(NET) I/O Stats")

`NET I/O` shows the volume of information the container's network interface has transmitted(TX) and received (RX). It represents network traffic.

### BLOCK I/O Stats[​](https://signoz.io/blog/docker-stats/#block-io-stats "Direct link to BLOCK I/O Stats")

`BLOCK I/O` helps to identify containers that are writing data and shows the total number of bytes read and written to the container file system. Block I/O stats can give you an idea about issues with data persistence.

### PIDS[​](https://signoz.io/blog/docker-stats/#pids "Direct link to PIDS")

PIDS is a count of the processes that the container has created or the number of kernel process IDs running inside the corresponding container.

## More on the usage of `docker stats`[​](https://signoz.io/blog/docker-stats/#more-on-the-usage-of-docker-stats "Direct link to more-on-the-usage-of-docker-stats")

### Getting stats of a particular container[​](https://signoz.io/blog/docker-stats/#getting-stats-of-a-particular-container "Direct link to Getting stats of a particular container")

To get the stats of a particular container, provide the container Id and run the command `docker stats <containerID>`

```
$ docker stats 56b3f523b0sd

CONTAINER ID         NAME                  CPU %          MEM USAGE / LIMIT       MEM%            NET I/O              BLOCK I/O           PIDS
56b3f523b0sd         nginx-container       0.35%          2.534MiB / 16.455GiB    0.37%           568B / 0B            134kb / 0B          3

```

<div class="markdown undefined" id="bkmrk--2" itemprop="articleBody" style="text-align: justify;"><div class="language-visual-basic codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Toggle word wrap" class="clean-btn" title="Toggle word wrap" type="button"><svg aria-hidden="true" class="wordWrapButtonIcon_Bwma" viewbox="0 0 24 24"><path d="M4 19h6v-2H4v2zM20 5H4v2h16V5zm-3 6H4v2h13.25c1.1 0 2 .9 2 2s-.9 2-2 2H15v-2l-3 3l3 3v-2h2c2.21 0 4-1.79 4-4s-1.79-4-4-4z" fill="currentColor"></path></svg></button><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div>You can also get the stats of multiple containers by name and id if you run

`docker stats <containerName> <containerId>`

```
$ docker stats ubuntu 56b3f523b0sd

CONTAINER ID         NAME                  CPU %          MEM USAGE / LIMIT       MEM%            NET I/O              BLOCK I/O           PIDS
049996113bc8         ubuntu                0.14%          1.437MiB / 16.455GiB    0.10%           3.56kb / 0B          5.12MB / 0B         1
56b3f523b0sd         nginx-container       0.35%          2.534MiB / 16.455GiB    0.37%           568B / 0B            134kb / 0B          3


```

<div class="markdown undefined" id="bkmrk--3" itemprop="articleBody" style="text-align: justify;"><div class="language-visual-basic codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Toggle word wrap" class="clean-btn" title="Toggle word wrap" type="button"><svg aria-hidden="true" class="wordWrapButtonIcon_Bwma" viewbox="0 0 24 24"><path d="M4 19h6v-2H4v2zM20 5H4v2h16V5zm-3 6H4v2h13.25c1.1 0 2 .9 2 2s-.9 2-2 2H15v-2l-3 3l3 3v-2h2c2.21 0 4-1.79 4-4s-1.79-4-4-4z" fill="currentColor"></path></svg></button><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div>### Display options that Docker Provides[​](https://signoz.io/blog/docker-stats/#display-options-that-docker-provides "Direct link to Display options that Docker Provides")

These display options allow you to specify how you want the output to be shown.

Docker stats offers the following options for display:

<div class="markdown undefined" id="bkmrk---all%C2%A0which-shows-al" itemprop="articleBody" style="text-align: justify;">1. `--all` which shows all containers, whether stopped or running.
2. `--format` which uses the [Go Template syntax](https://pkg.go.dev/text/template) to print images out.
3. `--no-stream` which disables streaming stats and only pulls the first result
4. `--no-trunc` which instructs Docker not to truncate (shorten) output.

</div>The syntax for this is shown below:

```
$ docker stats [OPTIONS] [CONTAINER...]

```

<div class="markdown undefined" id="bkmrk--4" itemprop="articleBody" style="text-align: justify;"><div class="language-visual-basic codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div>### Using `docker stats --format`[​](https://signoz.io/blog/docker-stats/#using-docker-stats---format "Direct link to using-docker-stats---format")

Let’s take a look at the `--format` option.

Docker format is used to modify the output format of commands that have the `--format` option. If a command supports this option, it can be used to change the output format of the command to suit our needs since the default command does not display all the fields connected to that object.

By using the Go Template syntax, the formatting option `--format` presents container output in an easy-to-read way.

For example,

```
$ docker stats --format "{{.Container}}: {{.CPUPerc}}"

049996113bc8: 0.14%
56b3f523b0sd: 0.35%

```

<div class="markdown undefined" id="bkmrk--5" itemprop="articleBody" style="text-align: justify;"><div class="language-visual-basic codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div>This prints out all images with the Container and CPUPerc (CPU Percentage) elements, separated by a colon (:) and it uses a template without headers.

To display all container information in a table format, including name, CPU percentage, and memory consumption, use the following syntax:

```
$ docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"

CONTAINER ID         CPU %          PRIV WORKING SET
56b3f523b0sd         0.35%          2.534MiB / 16.455GiB
049996113bc8         0.14%          1.437MiB / 16.455GiB
a3f78cb32a8e         0.00%          1.228MiB / 16.455GiB

```

<div class="markdown undefined" id="bkmrk--6" itemprop="articleBody" style="text-align: justify;"><div class="language-visual-basic codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Toggle word wrap" class="clean-btn" title="Toggle word wrap" type="button"><svg aria-hidden="true" class="wordWrapButtonIcon_Bwma" viewbox="0 0 24 24"><path d="M4 19h6v-2H4v2zM20 5H4v2h16V5zm-3 6H4v2h13.25c1.1 0 2 .9 2 2s-.9 2-2 2H15v-2l-3 3l3 3v-2h2c2.21 0 4-1.79 4-4s-1.79-4-4-4z" fill="currentColor"></path></svg></button><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div>Here is the list of applicable placeholders to use with the Go template syntax:

<div class="markdown undefined" id="bkmrk-placeholder-descript" itemprop="articleBody" style="text-align: justify;"><table><thead><tr><th>Placeholder</th><th>Description</th></tr></thead><tbody><tr><td>.container</td><td>Container name or ID (user input)</td></tr><tr><td>.Name</td><td>Container name</td></tr><tr><td>.ID</td><td>Container ID</td></tr><tr><td>.CPUPerc</td><td>CPU percentage</td></tr><tr><td>.MemUsage</td><td>Memory usage</td></tr><tr><td>.NetIO</td><td>Network IO</td></tr><tr><td>.BlockIO</td><td>Block IO</td></tr><tr><td>.MemPerc</td><td>Memory percentage (Not available on Windows)</td></tr><tr><td>.PIDs</td><td>Number of PIDs (Not available on Windows)</td></tr></tbody></table>

</div>## Final Thoughts[​](https://signoz.io/blog/docker-stats/#final-thoughts "Direct link to Final Thoughts")

In this article, we discussed ways to monitor resource usage metrics in Docker focused on the `docker stats` command. Other ways of using The Docker stats, Pseudo-files in sysfs, and REST API exposed by the Docker daemon are native ways of monitoring resource utilization metrics.

Docker container monitoring is critical for running containerized applications. For a robust monitoring and observability setup, you need to use a tool that visualizes the metrics important for container monitoring and also lets you set alerts on critical metrics. SigNoz is an open-source observability tool that can help you do that.

It uses [OpenTelemetry](https://opentelemetry.io/) to collect metrics from your containers for monitoring. OpenTelemetry is becoming the world standard for instrumentation of cloud-native applications, and it is backed by [CNCF](https://www.cncf.io/) foundation, the same foundation under which Kubernetes graduated.

If you want to set up a robust observability framework for your containerized application, you can use SigNoz. You can create unified views to monitor your Docker containers effectively.

It is easy to get started with SigNoz. It can be installed on macOS or Linux computers in just three steps by using a simple installation script.

The install script automatically installs Docker Engine on Linux. However, you must manually install [Docker Engine](https://docs.docker.com/engine/install/) on macOS before running the install script.

```
git clone -b main https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./install.sh

```

<div class="markdown undefined" id="bkmrk--7" itemprop="articleBody" style="text-align: justify;"><div class="language-jsx codeBlockContainer_Ckt0 theme-code-block"><div class="codeBlockContent_biex"><div class="buttonGroup__atx"><button aria-label="Copy code to clipboard" class="clean-btn" title="Copy" type="button"><span aria-hidden="true" class="copyButtonIcons_eSgA"><svg class="copyButtonIcon_y97N" viewbox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" fill="currentColor"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewbox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" fill="currentColor"></path></svg></span></button></div></div></div></div><div class="markdown undefined" id="bkmrk--9" itemprop="articleBody" style="text-align: justify;"></div>

# Monitora containers

Link: [https://gist.github.com/haukurk/a6e0751a8b8746265f8b2c55d9476230](https://gist.github.com/haukurk/a6e0751a8b8746265f8b2c55d9476230)

<div id="bkmrk-%23%21%2Fbin%2Fbash-%23-author"><table class="highlight tab-size js-file-line-container js-code-nav-container js-tagsearch-file" data-hpc="" data-paste-markdown-skip="" data-tab-size="8" data-tagsearch-lang="Shell" data-tagsearch-path="check_docker_container.sh"><tbody><tr><td>  
</td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23%21%2Fbin%2Fbash"><span class="pl-c">\#!/bin/bash</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-author%3A-haukur-kri"><span class="pl-c">\# Author: Haukur Kristinsson / Erik Kristensen</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-email%3A-haukur%40haux"><span class="pl-c">\# Email: haukur@hauxi.is / erik@erikkristensen.com</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-license%3A-mit"><span class="pl-c">\# License: MIT</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-nagios-usage%3A-chec"><span class="pl-c">\# Nagios Usage: check\_nrpe!check\_docker\_container!\_container\_id\_</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-usage%3A-.%2Fcheck_doc"><span class="pl-c">\# Usage: ./check\_docker\_container.sh \_container\_id\_</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23"><span class="pl-c">\#</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-the-script-checks-"><span class="pl-c">\# The script checks if a container is running.</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-ok---running"><span class="pl-c">\# OK - running</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-warning---containe"><span class="pl-c">\# WARNING - container is ghosted</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-critical---contain"><span class="pl-c">\# CRITICAL - container is stopped</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-%23-unknown---does-not"><span class="pl-c">\# UNKNOWN - does not exist</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-"></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-container%3D%241">CONTAINER=<span class="pl-smi">$1</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk--1"></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-running%3D%24%28docker-ins">RUNNING=<span class="pl-s"><span class="pl-pds">$(</span>docker inspect --format=<span class="pl-pds">"</span>{{ .State.Running }}<span class="pl-pds">"</span> <span class="pl-smi">$CONTAINER</span> <span class="pl-k">2&gt;</span> /dev/null<span class="pl-pds">)</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk--2"></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-if-%5B-%24%3F--eq-1-%5D%3B-the"><span class="pl-k">if</span> \[ <span class="pl-smi">$?</span> <span class="pl-k">-eq</span> 1 \]<span class="pl-k">;</span> <span class="pl-k">then</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-echo-%22unknown---%24con"><span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>UNKNOWN - <span class="pl-smi">$CONTAINER</span> does not exist.<span class="pl-pds">"</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-exit-3"><span class="pl-c1">exit</span> 3</td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-fi"><span class="pl-k">fi</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk--3"></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-if-%5B-%22%24running%22-%3D%3D-%22"><span class="pl-k">if</span> \[ <span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">$RUNNING</span><span class="pl-pds">"</span></span> <span class="pl-k">==</span> <span class="pl-s"><span class="pl-pds">"</span>false<span class="pl-pds">"</span></span> \]<span class="pl-k">;</span> <span class="pl-k">then</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-echo-%22critical---%24co"><span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>CRITICAL - <span class="pl-smi">$CONTAINER</span> is not running.<span class="pl-pds">"</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-exit-2"><span class="pl-c1">exit</span> 2</td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-fi-1"><span class="pl-k">fi</span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk--4"></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-started%3D%24%28docker-ins">STARTED=<span class="pl-s"><span class="pl-pds">$(</span>docker inspect --format=<span class="pl-pds">"</span>{{ .State.StartedAt }}<span class="pl-pds">"</span> <span class="pl-smi">$CONTAINER</span><span class="pl-pds">)</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-name%3D%24%28docker-inspec">NAME=<span class="pl-s"><span class="pl-pds">$(</span>docker inspect --format=<span class="pl-pds">"</span>{{ .Name }}<span class="pl-pds">"</span> <span class="pl-smi">$CONTAINER</span><span class="pl-pds">)</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-networkmode%3D%24%28docker">NETWORKMODE=<span class="pl-s"><span class="pl-pds">$(</span>docker inspect --format=<span class="pl-pds">"</span>{{ .HostConfig.NetworkMode }}<span class="pl-pds">"</span> <span class="pl-smi">$CONTAINER</span><span class="pl-pds">)</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-network%3D%24%28docker-ins">NETWORK=<span class="pl-s"><span class="pl-pds">$(</span>docker inspect --format=<span class="pl-pds">"</span>{{ .NetworkSettings.Networks.<span class="pl-pds">"</span><span class="pl-smi">$NETWORKMODE</span><span class="pl-pds">"</span>.IPAddress }}<span class="pl-pds">"</span> <span class="pl-smi">$CONTAINER</span><span class="pl-pds">)</span></span></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk--5"></td></tr><tr><td class="blob-code blob-code-inner js-file-line" id="bkmrk-echo-%22ok---%24containe"><span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>OK - <span class="pl-smi">$CONTAINER</span> is running. IP: <span class="pl-smi">$NETWORK</span>, StartedAt: <span class="pl-smi">$STARTED</span>, Named: <span class="pl-smi">$NAME</span><span class="pl-pds">"</span></span></td></tr></tbody></table>

</div>

# Coletar estatísticas Docker com CTOP

Link: [https://github.com/bcicen/ctop](https://github.com/bcicen/ctop)

git clone [https://github.com/bcicen/ctop.git](https://github.com/bcicen/ctop.git)

[![ctop](https://github.com/bcicen/ctop/raw/master/_docs/img/logo.png)](https://github.com/bcicen/ctop/blob/master/_docs/img/logo.png)

#  

[![release](https://camo.githubusercontent.com/f2e8c51bf0c38981d85be87986a8f7d5e8ed686d6db21290f152552fbc7f76f4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62636963656e2f63746f702e737667 "ctop")](https://camo.githubusercontent.com/f2e8c51bf0c38981d85be87986a8f7d5e8ed686d6db21290f152552fbc7f76f4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62636963656e2f63746f702e737667) [![homebrew](https://camo.githubusercontent.com/ba72a31e322c2dfdeb4c4e6c7d2e482471acdb427b3c08b986a2d76783a88197/68747470733a2f2f696d672e736869656c64732e696f2f686f6d65627265772f762f63746f702e737667 "ctop")](https://camo.githubusercontent.com/ba72a31e322c2dfdeb4c4e6c7d2e482471acdb427b3c08b986a2d76783a88197/68747470733a2f2f696d672e736869656c64732e696f2f686f6d65627265772f762f63746f702e737667) [![macports](https://camo.githubusercontent.com/a2b67573a6645861201389db3fd55a7b9779ee0828d42f2762f076e7c59393cc/68747470733a2f2f7265706f6c6f67792e6f72672f62616467652f76657273696f6e2d666f722d7265706f2f6d6163706f7274732f63746f702e7376673f6865616465723d6d6163706f727473 "ctop")](https://camo.githubusercontent.com/a2b67573a6645861201389db3fd55a7b9779ee0828d42f2762f076e7c59393cc/68747470733a2f2f7265706f6c6f67792e6f72672f62616467652f76657273696f6e2d666f722d7265706f2f6d6163706f7274732f63746f702e7376673f6865616465723d6d6163706f727473) [![scoop](https://camo.githubusercontent.com/3f3571a714fde8a5e4b95da52f2f8ec901af0ca64c8d775f213872939564f4be/68747470733a2f2f696d672e736869656c64732e696f2f73636f6f702f762f63746f703f6275636b65743d6d61696e "ctop")](https://camo.githubusercontent.com/3f3571a714fde8a5e4b95da52f2f8ec901af0ca64c8d775f213872939564f4be/68747470733a2f2f696d672e736869656c64732e696f2f73636f6f702f762f63746f703f6275636b65743d6d61696e)

Top-like interface for container metrics

`ctop` provides a concise and condensed overview of real-time metrics for multiple containers:

[![ctop](https://github.com/bcicen/ctop/raw/master/_docs/img/grid.gif)](https://github.com/bcicen/ctop/blob/master/_docs/img/grid.gif)

as well as a [single container view](https://github.com/bcicen/ctop/blob/master/_docs/single.md) for inspecting a specific container.

`ctop` comes with built-in support for Docker and runC; connectors for other container and cluster systems are planned for future releases.

## Install

<div class="markdown-heading" dir="auto" id="bkmrk--2" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#install)</div>Fetch the [latest release](https://github.com/bcicen/ctop/releases) for your platform:

#### Debian/Ubuntu

<div class="markdown-heading" dir="auto" id="bkmrk--4" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#debianubuntu)</div>Maintained by a [third party](https://packages.azlux.fr/)

```
sudo apt-get install ca-certificates curl gnupg lsb-release
curl -fsSL https://azlux.fr/repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/azlux-archive-keyring.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/azlux-archive-keyring.gpg] http://packages.azlux.fr/debian \
  $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/azlux.list >/dev/null
sudo apt-get update
sudo apt-get install docker-ctop
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--6" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>#### Arch

<div class="markdown-heading" dir="auto" id="bkmrk--7" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#arch)</div>```
sudo pacman -S ctop
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--9" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>*`ctop` is also available for Arch in the [AUR](https://aur.archlinux.org/packages/ctop-bin/)*

#### Linux (Generic)

<div class="markdown-heading" dir="auto" id="bkmrk--10" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#linux-generic)</div>```
sudo wget https://github.com/bcicen/ctop/releases/download/v0.7.7/ctop-0.7.7-linux-amd64 -O /usr/local/bin/ctop
sudo chmod +x /usr/local/bin/ctop
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--12" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>#### OS X

<div class="markdown-heading" dir="auto" id="bkmrk--13" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#os-x)</div>```
brew install ctop
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--15" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>or

```
sudo port install ctop
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--16" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>or

```
sudo curl -Lo /usr/local/bin/ctop https://github.com/bcicen/ctop/releases/download/v0.7.7/ctop-0.7.7-darwin-amd64
sudo chmod +x /usr/local/bin/ctop
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--17" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>#### Windows

<div class="markdown-heading" dir="auto" id="bkmrk--18" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#windows)</div>`ctop` is available in [scoop](https://scoop.sh/):

```
scoop install ctop
```

<div class="highlight highlight-source-powershell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--20" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>#### Docker

<div class="markdown-heading" dir="auto" id="bkmrk--21" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#docker)</div>```
docker run --rm -ti \
  --name=ctop \
  --volume /var/run/docker.sock:/var/run/docker.sock:ro \
  quay.io/vektorlab/ctop:latest
```

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" id="bkmrk--23" style="text-align: justify;"><div class="zeroclipboard-container"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></div></div>## Building

<div class="markdown-heading" dir="auto" id="bkmrk--24" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#building)</div>Build steps can be found [here](https://github.com/bcicen/ctop/blob/master/_docs/build.md).

## Usage

<div class="markdown-heading" dir="auto" id="bkmrk--26" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#usage)</div>`ctop` requires no arguments and uses Docker host variables by default. See [connectors](https://github.com/bcicen/ctop/blob/master/_docs/connectors.md) for further configuration options.

### Config file

<div class="markdown-heading" dir="auto" id="bkmrk--28" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#config-file)</div>While running, use `S` to save the current filters, sort field, and other options to a default config path (`~/.config/ctop/config` on XDG systems, else `~/.ctop`).

Config file values will be loaded and applied the next time `ctop` is started.

### Options

<div class="markdown-heading" dir="auto" id="bkmrk--30" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#options)</div><table id="bkmrk-option-description--"><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td>`-a`</td><td>show active containers only</td></tr><tr><td>`-f <string>`</td><td>set an initial filter string</td></tr><tr><td>`-h`</td><td>display help dialog</td></tr><tr><td>`-i`</td><td>invert default colors</td></tr><tr><td>`-r`</td><td>reverse container sort order</td></tr><tr><td>`-s`</td><td>select initial container sort field</td></tr><tr><td>`-v`</td><td>output version information and exit</td></tr></tbody></table>

### Keybindings

<div class="markdown-heading" dir="auto" id="bkmrk--32" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#keybindings)</div><table id="bkmrk-key-action-%3Center%3E-o"><thead><tr><th align="center">Key</th><th>Action</th></tr></thead><tbody><tr><td align="center"><kbd>&lt;ENTER&gt;</kbd></td><td>Open container menu</td></tr><tr><td align="center"><kbd>a</kbd></td><td>Toggle display of all (running and non-running) containers</td></tr><tr><td align="center"><kbd>f</kbd></td><td>Filter displayed containers (`esc` to clear when open)</td></tr><tr><td align="center"><kbd>H</kbd></td><td>Toggle ctop header</td></tr><tr><td align="center"><kbd>h</kbd></td><td>Open help dialog</td></tr><tr><td align="center"><kbd>s</kbd></td><td>Select container sort field</td></tr><tr><td align="center"><kbd>r</kbd></td><td>Reverse container sort order</td></tr><tr><td align="center"><kbd>o</kbd></td><td>Open single view</td></tr><tr><td align="center"><kbd>l</kbd></td><td>View container logs (`t` to toggle timestamp when open)</td></tr><tr><td align="center"><kbd>e</kbd></td><td>Exec Shell</td></tr><tr><td align="center"><kbd>c</kbd></td><td>Configure columns</td></tr><tr><td align="center"><kbd>S</kbd></td><td>Save current configuration to file</td></tr><tr><td align="center"><kbd>q</kbd></td><td>Quit ctop</td></tr></tbody></table>

## Alternatives

<div class="markdown-heading" dir="auto" id="bkmrk--34" style="text-align: justify;">[<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>](https://github.com/bcicen/ctop#alternatives)</div>See [Awesome Docker list](https://github.com/veggiemonk/awesome-docker/blob/master/README.md#terminal) for similar tools to work with Docker.

# 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>