-1

Code of Dockerfile is here

FROM php:7.0-cli
COPY ./ /

Code of create.php is here

<?php
$fp = fopen($argv[1],'wa+');
fwrite($fp,'sample');
fclose($fp);
?>

I build my docker using following command in debian - stretch

docker build -t create-tool .

I run it using following command in debian - stretch

docker run create-tool php create.php /home/user1/Desktop/test

But it is not creating the file, instead it throws the error "failed to open stream".

mastef
  • 2,787
  • 1
  • 6
  • 16
Hasmukh Mistry
  • 129
  • 1
  • 18

2 Answers2

0

How about using touch()? It can solve your problem!


http://php.net/manual/en/function.touch.php

mik13ST
  • 95
  • 7
Mr.Apr
  • 66
  • 1
  • 6
0

3 Scenarios I would investigate :

  1. Because /home/user1/Desktop/test doesn't exist within the docker container. That file exists on your host system, so it's out of reach for the container.

    If you want to access files on your host, you'll need to share the folders you want the container to access with a volume.

  2. The path exists in your container, but is for example a directory. You can check quickly if a file exists and/or is accessible within your container by running docker run create-tool ls -lsah /home/user1/Desktop/test.

    This will then list what the container can access with the given path.

  3. Double-check what the value of $argv[1] in your PHP script is. You wouldn't want to get escaped characters, etc. print_r($argv); should help.

Community
  • 1
  • 1
mastef
  • 2,787
  • 1
  • 6
  • 16