core@localhost ~/git/DockerExam/volume $ docker run --rm -v /home/core/git/DockerExam/volume/share:/tmp/host:rw -i centos touch /tmp/host/hello
core@localhost ~/git/DockerExam/volume $ ls share/
hello
+------------------------+ +--------------------+ | (1) データ格納コンテナ | mount | (2) アプリコンテナ | | VOLUME /data <---------+--------+-/data | +------------------------+ | /bin | | /tmp | | /home | | ... | +--------------------+
# Busybox is ths simplest linux distribution. It's only few Mega Bytes,
# but contains 200 ordinary unix commands.
FROM busybox
RUN mkdir /var/www/html
RUN echo "<html><body>Hello Docker World!</body></html>" > /var/www/html/index.html
VOLUME /var/www/html
CMD /bin/sh
$ docker build -t atsushi/volume ./
FROM atsushi/ubuntu_monit_ssh
RUN apt-get -y update && apt-get -y install apache2 php5
ADD apache.conf /etc/monit/conf.d/apache.conf
check process apache2 with pidfile /var/run/apache2/apache2.pid
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
$ docker build -t atsushi/volume ./
$ docker create --name test1 atsushi/volume $ docker run --volumes-from test1 -p 10022:22 -p 10080:80 -d atsushi/httpd $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76e26ee78129 atsushi/httpd:latest "/usr/bin/monit -I" 2 minutes ago Up 2 minutes 2812/tcp, 0.0.0.0:10022->22/tcp, 0.0.0.0:10080->80/tcp sad_pasteur 829537c8c6c8 atsushi/volume:latest "/bin/sh -c /bin/sh" 7 days ago Exited (0) 7 days ago test1
<html><body>
<?php
exec("/usr/games/fortune", $output);
foreach ($output as $line) {
echo $line . "</br>";
}
?>
</body></html>
$ docker run --rm --volumes-from test1 -i busybox ls /var/www/html index.html index.php
$ docker create --name test2 atsushi/volume $ docker run --volumes-from test2 -p 20080:80 -d atsushi/httpd
core@localhost ~ $ docker run --rm --volumes-from test1 -v $(pwd):/backup busybox tar cvf /backup/backup.tar /var/www
tar: removing leading '/' from member names
var/www/
var/www/html/
var/www/html/index.html
var/www/html/index.php
var/www/html/.index.php.swp
core@localhost ~ $ ls
backup.tar git
core@localhost ~ $ tar tvf backup.tar
drwxr-xr-x www-data/www-data 0 2014-09-24 16:47 var/www/
drwxr-xr-x root/root 0 2014-09-17 14:13 var/www/html/
-rw-r--r-- root/root 46 2014-09-16 16:26 var/www/html/index.html
-rw-r--r-- root/root 129 2014-09-17 14:13 var/www/html/index.php
-rw-r--r-- root/root 12288 2014-09-17 14:13 var/www/html/.index.php.swp
逆は
$ docker run --rm --volumes-from test2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
ホストの$(pwd) = Dockerの/backup なので、Dockerの/backup/backup.tar = ホストの $(pwd)/backup.tar