0

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.

3
  • you need to mont if you want to see the results Commented Sep 13, 2021 at 19:09
  • Can you explain more about it in detail? Commented Sep 13, 2021 at 19:11
  • yes preparing a answer Commented Sep 13, 2021 at 19:16

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.