Adeel Tahir
Adeel Tahir

Reputation: 203

Docker PHP CLI Output File

New to Docker, I have written a code that reads a CSV file and converts it into JSON. It is working fine.

I follow the below procedure and get a PHP CLI image from the link https://hub.docker.com/_/php

Follow the instructions it shows me the "Completed" which is output from a file, but how can I get the generated file. Where the file will be?

Code for DockerFile

FROM php:7.4-cli
COPY . /challenge
WORKDIR challenge
CMD [ "php", "code.php" ]

Code for code.php

$file = fopen("./input/source.csv", 'r');
$json = array();
    while ($row = fgetcsv($file,"1024",",")) {
        array_push($json,$row);
    }
fclose($file);
file_put_contents("./output/json/target.json", json_encode($json)); 
echo "Completed";

My operating system is Windows.

Upvotes: 0

Views: 997

Answers (1)

flaxon
flaxon

Reputation: 942

the docker files COPY only copy the files ones at build, you need to mount the files in runtime if you want docker to write back to your folder in your window machine. (the script write the files in your docker, but you will never see it, as docker files are not preserved)

so instead you can use the original image, and mount your folder

FROM php:7.4-cli
WORKDIR /app
CMD [ "php", "code.php" ]

step 1. docker build -t "php-code" .

step 2. docker run --rm -v ${PWD}/:/app php-code this works in powershell see here what to use on other systems

see more about volume mounting

PS make sure the folder exists in your working directory or add code to PHP to make the folders

Upvotes: 1

Related Questions