Time whole build
time docker build .
Time steps of build
docker build . | while read line ; do echo "$(date)| $line"; done;
Output
Wed 5 Sep 2018 19:12:22 BST| Sending build context to Docker daemon 27.65kB
Wed 5 Sep 2018 19:12:22 BST| Step 1/19 : FROM centos:centos7
Wed 5 Sep 2018 19:12:22 BST| ---> 49f7960eb7e4
...
You can improve the results by only outputting the "Step ?/? :" lines, like so:
docker build . | grep "^Step" | while read line ; do echo "$(date +%s)| $line"; done;
Output
1536171476| Step 1/19 : FROM centos:centos7
1536171476| Step 2/19 : ENV TERM xterm
1536171476| Step 3/19 : RUN *** omitted ***
1536171476| Step 4/19 : RUN *** omitted ***
1536171476| Step 5/19 : COPY *** omitted ***
1536171476| Step 6/19 : RUN *** omitted ***
1536171476| Step 7/19 : COPY *** omitted ***
JSON output
You can make this into a script if you wanted to run in a CI/CD pipeline, or add to your developer tools.
#!/bin/bash
# script: time-docker-build.sh
#
# All command line arguments are passed to docker build command.
#
# usage: ./time-docker-build.sh
#
DATE_FORMAT="+%s"
(
# Output START line
echo "$(date $DATE_FORMAT) | - 0 - START"
docker build $* . | \
grep "^Step" | \
while read line ;
do
# Output build output prefixed with date
echo "$(date $DATE_FORMAT) | $line";
done;
# Output END line
echo "$(date $DATE_FORMAT) | - -1 - END"
) | (
# Generate JSON array output.
# - START is step: 0
# - END is step: -1
echo "["
FIRST_RUN=true
while read line ;
do
[[ -z "$FIRST_RUN" ]] && echo "," # if not first line, print ','
lineArray=($line)
time="${lineArray[0]}" # step is 0th
step="${lineArray[3]}" # step is 2nd
cmd="${lineArray[@]:5}" # cmd is everything after 5th
stepNum=${step/\/*/}
escapedCmd="${cmd//\"/\\\"}" # escape all double quotes '"'
echo " {"
echo " \"time\": $time,"
echo " \"step\": $stepNum,"
echo " \"cmd\": \"$escapedCmd\""
echo -n " }"
unset FIRST_RUN
done
echo
echo "]"
)
Output
bash-3.2$ ./time-docker-build.sh
[
{
"time": 1536174052,
"step": 0,
"cmd": "START"
},
{
"time": 1536174052,
"step": 1,
"cmd": "FROM centos:centos7"
},
{
"time": 1536174052,
"step": 2,
"cmd": "ENV TERM xterm"
},
Script is available as a gist here:
https://gist.github.com/philpoore/05eca572f3aadf70f529c470ac679147
RUN
command.