Odds and Ends

Useful Docker Patterns (On GNU/Linux)

To easily copy files from a Docker image when you dont want to start a container, you can do the following:

docker cp $(docker create --rm $IMAGE):$FROM_PATH $TO_PATH

Sometimes it is useful to compare the output of a command between two Docker images, especially comparing different versions of a particular image:

docker run --rm -ti --entrypoint=bash $IMAGE1 -c "$CMD" > /tmp/output1.tmp
docker run --rm -ti --entrypoint=bash $IMAGE2 -c "$CMD" > /tmp/output2.tmp
diff -s /tmp/output1.tmp /tmp/output2.tmp

I have both these command patterns saved as executable Bash shell scripts on my system path as cp_docker and diff_docker_cmd respectively.

Each of the scripts can be found here:

cp_docker

#!/bin/bash

IMAGE=$1
FROM_PATH=$2
TO_PATH=$3

docker cp $(docker create --rm $IMAGE):$FROM_PATH $TO_PATH

diff_docker_cmd

#!/bin/bash

IMAGE1=$1
IMAGE2=$2
CMD=$3

docker run --rm -ti --entrypoint=bash $IMAGE1 -c "$CMD" > /tmp/output1.tmp  \
&& docker run --rm -ti --entrypoint=bash $IMAGE2 -c "$CMD" > /tmp/output2.tmp \
&& diff -s /tmp/output1.tmp /tmp/output2.tmp && rm /tmp/output*.tmp
Tags: docker diff cp command gnu bash