Public Repository

Last pushed: a year ago
Short Description
A simple test scenario for Docker volumes.
Full Description

To test, create a volume_host and volume_host_2 folder in the current directory
filled with a few test data files, and then run the below script to compare
different methods of mounting volumes between two containers.

#!/bin/sh

# reset
echo "# Reset from prior runs"
docker stop volume-sandbox-a volume-sandbox-b 
docker rm -v volume-sandbox-a volume-sandbox-b 
docker volume rm volume-sandbox-named
docker volume rm volume-sandbox-named-2
docker volume rm volume-sandbox-named-3
rm -f volume_host/volume-sandbox*.data
rm -f volume_host_2/volume-sandbox*.data

echo "# Build image and setup containers"
docker build -t volume-sandbox:latest .

echo "## Create a named volume"
docker volume create --name volume-sandbox-named
#docker volume create --name volume-sandbox-named-2

echo "## Create container A"
docker run -itd \
  -v /volume_b \
  -v /volume_c \
  -v /volume_d \
  -v `pwd`/volume_host:/volume_e \
  -v `pwd`/volume_host:/volume_e2 \
  -v volume-sandbox-named:/volume_f \
  -v volume-sandbox-named-2:/volume_g \
  -v `pwd`/volume_host:/volume_ro:ro \
  -h volume-sandbox-a \
  --name volume-sandbox-a \
  volume-sandbox:latest top

echo "## Create container B"
docker run -itd \
  -v /volume_b \
  -v `pwd`/volume_host_2:/volume_e2 \
  -v volume-sandbox-named:/volume_f2 \
  -v volume-sandbox-named-3:/volume_g \
  --volumes-from volume-sandbox-a \
  -h volume-sandbox-b \
  --name volume-sandbox-b \
  volume-sandbox:latest top

echo "# Begin tests: "

echo "## Write to volumes on A"
docker exec volume-sandbox-a /volume-touch.sh volume-sandbox-a.data
echo "## Show contents of A"
docker exec volume-sandbox-a /volume-ls.sh
echo "## Show contents of host"
ls -l volume_host*/*

echo ""
echo "## Write to volumes on B"
docker exec volume-sandbox-b /volume-touch.sh volume-sandbox-b.data
echo "## Show contents of A"
docker exec volume-sandbox-a /volume-ls.sh
echo "## Show contents of B"
docker exec volume-sandbox-b /volume-ls.sh

echo ""
echo "## Stop container A"
docker stop volume-sandbox-a
echo "## Show contents of B after stop"
docker exec volume-sandbox-b /volume-ls.sh
echo "## Remove container A"
docker rm -v volume-sandbox-a
echo "## Show contents of B after remove"
docker exec volume-sandbox-b /volume-ls.sh

# cleanup the test with the following:
# docker stop volume-sandbox-a volume-sandbox-b 
# docker rm -v volume-sandbox-a volume-sandbox-b 
# docker volume rm volume-sandbox-named
# docker volume rm volume-sandbox-named-2
# docker volume rm volume-sandbox-named-3
# rm -f volume_host/volume-sandbox*.data
# rm -f volume_host_2/volume-sandbox*.data

Dockerfile

FROM busybox

VOLUME [/volume_a]

COPY volume-touch.sh volume-ls.sh volume-test.sh /

RUN mkdir -p /volume_a && \
    mkdir -p /volume_b && \
    mkdir -p /volume_c && \
    touch /volume_a/dockerfile_touch.data && \
    touch /volume_b/dockerfile_touch.data && \
    touch /volume_c/dockerfile_touch.data && \
    chmod 755 /volume*.sh

COPY volume_host/host_init.data /volume_a/dockerfile_copy.data
COPY volume_host/host_init.data /volume_b/dockerfile_copy.data
COPY volume_host/host_init.data /volume_c/dockerfile_copy.data

VOLUME [/volume_b,/volume_c]

CMD /bin/sh
Docker Pull Command
Owner
bmitch3020

Comments (0)