0

I have a Dockerfile:

FROM php:7.4-cli
VOLUME ./result/ /usr/src/result
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer;
RUN composer require phpoffice/phpspreadsheet
CMD [ "php", "./demo.php" ]

When demo.php executes it creates a file, I would like to copy that file to the volume on the host. Is it possible to get file back out of the container onto the host using volumes like this?

bclingan
  • 99
  • 2
  • 12
  • You can't specify a host-directory location in a Dockerfile `VOLUME`; instead, you've told Docker to create two different anonymous volumes. `VOLUME` isn't required for this case and you can delete it entirely. When you run the container you need to specify the `docker run -v` option. – David Maze Apr 15 '22 at 11:20

1 Answers1

2

The -v flag lets you mount a directory persistently:

docker run image -v /some/host/path:/some/container/path

so for example, if your container saves it's file to /var/output/file.txt in the container, you could issue this command:

docker run my-image -v /var/output:$PWD

and you would end up with a file.txt in the current directory.

code_monk
  • 9,451
  • 2
  • 42
  • 41