Access files inside docker container over php

1.7k Views Asked by At

I have jboss server on docker and I'm trying to check over php whether audio file is saved or not. If file exists I would like to copy it to local machine.

  $location = shell_exec('docker exec -it  name3 /bin/bash -c cd .**********; ls');
  $filepath = $location;
  if (file_exists($filepath)) {
       //copy that file to local machine 
   } 
  echo $filepath;

but for $location I get folders inside htdocs (xampp). Is it possible to access files inside docker container over php (which is on local server)?

2

There are 2 best solutions below

0
Abdou Rayes On

You can use docker cp to tell docker to copy that files for you from the container to the local machine

docker cp <containerId>:/file/path/within/container /host/path/target

to get the containerId you can run

docker ps
0
gsaslis On

If I understand correctly, PHP is running on your local machine and you want to check if a file exists or not inside the running docker container.

Regardless of whether or not PHP has access to the part of the filesystem outside your doc root, or whether it has permission to execute shell commands (both security concerns), I would still use a different approach.

  • Use a docker volume for storing the recordings.
  • Mount the volume on the path you are checking - i.e. /opt/Restcomm-JBoss-AS7/standalone/deployments/restcomm.war/recordings/
  • Set the host path of your docker volume somewhere where PHP has read access on the filesystem
  • Use PHP to check whether a file exists, without using any shell commands. e.g. https://secure.php.net/manual/en/function.file-exists.php

Please note that this way your recordings will also persist across restarts / crashes of the docker container, which I guess is important...