WeSee
WeSee

Reputation: 3772

Minimal Docker Image for PHP-CLI execution

I want to run a php script as a oneliner from the command line. Since on this linux machine no PHP is installed but Docker I thought about executing the PHP interpreter by running an little PHP Docker image which executing the script and deletes the container and image after finishing.

My internet connection is not fast and thus the image should be as small as possible.

The script process.php should read the File data.json in the current working directory and generate some text files in subdirectories. So no special PHP modules are required.

My questions:

My research on Dockerhub resulted in tons of PHP images with full blown PHP and not optimized on Host volume modifications as a one liner.

Upvotes: 0

Views: 945

Answers (1)

WeSee
WeSee

Reputation: 3772

Found a solution close to what I need.

The script process.php:

<?php
echo "hello";
$data = json_decode(file_get_contents(__DIR__."/data.json"),true);
file_put_contents("message.txt","message is ".$data["message"]);

The data file data.json:

{
    "message": "hello world"
}

The one-liner:

$ docker run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp \ 
php:7.2.2-cli-alpine3.6 php process.php

And the result is message.txt:

message is hello world

Not the smallest image, but has a size of 62 MB and removes itself after finishing.

Upvotes: 1

Related Questions