Solderdot
Solderdot

Reputation: 11

Access files from RAM-disk from Docker/PHP

I'm running a little local web page on my Raspberry Pi in a Docker environment with Portainer. The image is php:8-apache. The index.php and all files are stored in /data/DockerVolumes/MyWebServer/www/html which is mapped via bind to /var/www/html in the container. This works fine.

The index.php includes images which are frequently updated by a cron job running outside the container, in native Raspi OS. This works fine as well: After the cron-job executed and new images are found in /data/DockerVolumes/MyWebServer/www/html a browser update will show the new image. The command in the index.php is as follows:

<img src="./graph.png" alt="" style="width:80%;height:80%;">

Now I'd like to move those non-static files to a ram disk which is mounted to mnt/ram_disk. I added a new volume to the Portainer config: /mnt/ram_disk in the container is mapped to /mnt/ram_disk on the host via bind. I also changed the PHP file as follows:

<img src="/mnt/ram_disk/graph.png" alt="" style="width:80%;height:80%;">

For some reason this does not work. No image is shown. I also attempted to add a symlink from /data/DockerVolumes/MyWebServer/www/html/graph.png to /mnt/ram_disk/graph.png but this does not work as well.

My understanding is that /mnt/ram_disk is accessible from within the docker container. But why can't the index.php access information from that folder? Could anybody show, where my doing is quirky?

Best regards and thanks in advance

Upvotes: 0

Views: 55

Answers (1)

Julien B.
Julien B.

Reputation: 3334

It looks like you are trying to see an image in your browser using the absolute path from your filesystem.

So let's say your URL is https://localhost/ then the image your are loading with <img src="/mnt/ram_disk/graph.png" alt="" style="width:80%;height:80%;"> will be loaded from https://localhost/mnt/ram_disk/graph.png which is most likely incorrect.

You should ensure your ram_disk is mounted in your /data/DockerVolumes/MyWebServer/www/html folder or that you have a symlink and that symlinks are enabled in your web server.

Try running your container with something like this:

docker run -d \
  -v /data/DockerVolumes/MyWebServer/www/html:/var/www/html \
  -v /mnt/ram_disk:/var/www/html/images \
  --name my_web_server \
  your-image-name

And then load your image with:

<img src="/images/graph.png" alt="" style="width:80%;height:80%;">

Something similar could be done with a compose file if you are using docker compose.

Upvotes: 1

Related Questions