<p>The output of <code>docker images</code> is really intended for humans, and it's not very parse-friendly. Instead, most Docker commands support a <a href="https://docs.docker.com/engine/admin/formatting/" rel="nofollow noreferrer"><code>--format</code> flag using Go templates</a>. As you can see in the <a href="https://docs.docker.com/engine/reference/commandline/images/#format-the-output" rel="nofollow noreferrer"><code>images</code> documentation</a>, you only really care about the <code>.Size</code> value, so you really want this:</p>

<pre><code>docker images --format "{{.Size}}"
</code></pre>

<p>This just gives an output like this:</p>

<pre><code>90.6 MB
</code></pre>

<p>If you have multiple images, they will each be on a separate line. I'm going to assume you will only ever have one line, because it vastly simplifies the next steps.</p>

<p>To get rid of the <code>MB</code>, we can simply use <code>sed</code>:</p>

<pre><code>docker images --format "{{.Size}}" | sed "s/MB//g"
# =&gt; 90.6
</code></pre>

<p>But, you probably <strong>don't</strong> want this for your bash script. Most shells don't support floating point comparisons, so inputting 90.6 would just fail. Instead, let's use <code>bc</code> to truncate the number first:</p>

<pre><code>docker images --format "{{.Size}}" | sed "s/MB/\/1/g" | bc
# =&gt; 90
</code></pre>

<p>Note that I've replaced the <code>MB</code> with a division by 1, which <a href="https://stackoverflow.com/questions/20558710/bc-truncate-floating-point-number">truncates in <code>bc</code></a>.</p>

<p>You can then pipe it into a shell script. I tested with this script:</p>

<pre><code>VAR=$(cat)

if [ $VAR -gt "250" ]
    then
    echo "Image too large"
    exit 1
fi
</code></pre>

<p>Then run with:</p>

<pre><code>docker images --format "{{.Size}}" | sed "s/MB/\/1/g" | bc | ./check_size.sh
# Exits with 0 (for my example)
</code></pre>

CC BY-SA 3.0
2017-04-16T09:40:41.553Z
2017-04-16T12:35:20.243Z
2017-05-23T12:39:34.013Z
Aurora0001
<p><code>docker images</code> returns:</p>

<pre><code>REPOSITORY            TAG            IMAGE ID       CREATED             SIZE
&lt;none&gt;                &lt;none&gt;         fcc125757e66   About an hour ago   270 MB
</code></pre>

<p><strong>Attempts</strong></p>

<p><code>docker images | awk '{print $6}'</code> returns:</p>

<pre><code>SIZE
ago
hour
ago
ago
ago
</code></pre>

<p><strong>Expected outcome</strong></p>

<p><code>270</code> so this could be used in an if statement:</p>

<pre><code>if [ VAR &gt; 250 ] then
   echo "Image too large"
   exit 1
fi
</code></pre>

CC BY-SA 3.0
2017-04-16T02:14:02.987Z
It looks like you already have a wrapper script framed out, and you're just asking how to get the size of an image. Is that correct?
Xiong Chiamiov
CC BY-SA 3.0
2017-04-16T07:15:18.987Z
Unverified: awf -F ´\t' ´{ split(" ",$5,arr); print arr[1]}'
Tensibai
<docker><continuous-integration>
How to let build fail when docker image size exceeds certain threshold?
030
# How to let build fail when docker image size exceeds certain threshold?

(Asked by: username_0 on 2017-04-16)

docker images returns:
REPOSITORY            TAG            IMAGE ID       CREATED             SIZE
                         fcc125757e66   About an hour ago   270 MB

Attempts
docker images | awk '{print $6}' returns:
SIZE
ago
hour
ago
ago
ago

Expected outcome
270 so this could be used in an if statement:
if [ VAR > 250 ] then
   echo "Image too large"
   exit 1
fi

**Tags:** docker, continuous-integration

> **Comments:**
> **username_1**: It looks like you already have a wrapper script framed out, and you're just asking how to get the size of an image. Is that correct?  
> **username_2**: Unverified: awf -F ´\t' ´{ split(" ",$5,arr); print arr[1]}'  

---

## 1 answers

### Answer by username_3 on 2017-04-16. Score: 7 | Accepted answer

The output of docker images is really intended for humans, and it's not very parse-friendly. Instead, most Docker commands support a --format flag using Go templates. As you can see in the images documentation, you only really care about the .Size value, so you really want this:
docker images --format "{{.Size}}"

This just gives an output like this:
90.6 MB

If you have multiple images, they will each be on a separate line. I'm going to assume you will only ever have one line, because it vastly simplifies the next steps.
To get rid of the MB, we can simply use sed:
docker images --format "{{.Size}}" | sed "s/MB//g"
# => 90.6

But, you probably don't want this for your bash script. Most shells don't support floating point comparisons, so inputting 90.6 would just fail. Instead, let's use bc to truncate the number first:
docker images --format "{{.Size}}" | sed "s/MB/\/1/g" | bc
# => 90

Note that I've replaced the MB with a division by 1, which truncates in bc.
You can then pipe it into a shell script. I tested with this script:
VAR=$(cat)

if [ $VAR -gt "250" ]
    then
    echo "Image too large"
    exit 1
fi

Then run with:
docker images --format "{{.Size}}" | sed "s/MB/\/1/g" | bc | ./check_size.sh
# Exits with 0 (for my example)


---

