How to get contents generated by a docker containe

2019-07-14 06:15发布

问题:

This question is a minimal failing version of this other one:

How to get contents generated by a docker container on the local fileystem

I have the following files:

./test
-rw-r--r--   1 miqueladell  staff   114 Jan 21 15:24 Dockerfile
-rw-r--r--   1 miqueladell  staff    90 Jan 21 15:23 docker-compose.yml
drwxr-xr-x   3 miqueladell  staff   102 Jan 21 15:25 html

./test/html:
-rw-r--r--  1 miqueladell  staff    0 Jan 21 15:22 file_from_local_filesystem

DockerFile

FROM php:7.0.2-apache
RUN touch /var/www/html/file_generated_inside_the_container
VOLUME /var/www/html/

docker-compose.yml

test:
  image: test
  volumes:
     - ./html:/var/www/html/

After running a container built from the image defined in the Dockerfile what I want is having:

./html
-- file_from_local_filesystem
-- file_generated_inside_the_container

Instead of this I get the following:

build the image

$ docker build --no-cache -t test .

Sending build context to Docker daemon 4.096 kB
Step 1 : FROM php:7.0.2-apache
 ---> 2f16964f48ba
Step 2 : RUN touch /var/www/html/file_generated_inside_the_container
 ---> Running in b957cc9d7345
 ---> 5579d3a2d3b2
Removing intermediate container b957cc9d7345
Step 3 : VOLUME /var/www/html/
 ---> Running in 6722ddba76cc
 ---> 4408967d2a98
Removing intermediate container 6722ddba76cc
Successfully built 4408967d2a98

run a container with previous image

$ docker-compose up -d

Creating test_test_1

list files on the local machine filesystem

$ ls -al html

total 0
drwxr-xr-x  3 miqueladell  staff  102 Jan 21 15:25 .
drwxr-xr-x  5 miqueladell  staff  170 Jan 21 14:20 ..
-rw-r--r--  1 miqueladell  staff    0 Jan 21 15:22 file_from_local_filesystem

list files from the container

$ docker exec -i -t test_test_1 ls -alR /var/www/html


/var/www/html:
total 4
drwxr-xr-x 1 1000 staff  102 Jan 21 14:25 .
drwxr-xr-x 4 root root  4096 Jan  7 18:05 ..
-rw-r--r-- 1 1000 staff    0 Jan 21 14:22 file_from_local_filesystem

The volume from the local filesystem gets mounted on the container file system replacing the contents of it.

This is contrary at what I understand in the section "Permissions and Ownership" of this guide Understanding volumes

How could I get the desired output?

Thanks


EDIT: As is said in the accepted answer I did not understand volumes when asking the question. Volumes, as mountponint, replace the container content with the local filesystem that is mounted.

The solution I needed was to use ENTRYPOINT to run the necessary commands to initialize the contents of the mounted volume once the container is running.

The code that originated the question can be seen working here: https://github.com/MiquelAdell/composed_wordpress/tree/1.0.0

回答1:

This is from the guide you've pointed to

This won’t happen if you specify a host directory for the volume

Volumes you share from other containers or host filesystem replace directories from container.

If you need to add some files to volume, you should do it after you start container. You can do an entrypoint for example which does touch and then runs your main process.



回答2:

Yep, pretty sure it should be the full path:

docker-compose.yml

test:
  image: test
  volumes:
     - ./html:/var/www/html/

./html should be /path/to/html

Edit

Output after changing to full path and running test.sh:

$ docker exec -ti dockervolumetest_test_1 bash
root@c0bd7a722b63:/var/www/html# ls -la
total 8
drwxr-xr-x 2 1000 adm  4096 Jan 21 15:19 .
drwxr-xr-x 3 root root 4096 Jan  7 18:05 ..
-rw-r--r-- 1 1000 adm     0 Jan 21 15:19 file_from_local_filesystem

Edit 2

Sorry, I misunderstood the entire premise of the question :)

So you're trying to get file_generated_inside_the_container (which is created inside your docker image only) mounted to some location on your host machine - like a "reverse mount".

This isn't possible to do with any docker commands, but if all you're after is access to your VOLUMEs files on your host, you can find the files in the docker root directory (normally /var/lib/docker). To find the exact location of the files, you can use docker inspect [container_id], or in the latest versions use the docker API.

See cpuguy's answer in this github issue: https://github.com/docker/docker/issues/12853#issuecomment-123953258 for more details.