How to Run PHP 5.4 CLI using Docker in Ubuntu Linux 18.04

Posted on


This is just an example in the form of article to show how to run a single PHP file script using docker. Docker will run in Ubuntu Linux 18.04. It is running in the form of a container where there is a PHP CLI program executes a single PHP file script.

Before running docker, install it first. The installation process is available in the article with the title of ‘How to Install Docker in Linux Ubuntu 18.04 Bionic Beaver’ in this link.

Running one PHP file script using PHP 5.4 CLI in a docker container

1. Create a new folder as the folder for preparing all the available requirements. For an example, just choose ‘docker’ as the name. Create it in the user’s home folder. So, the full path will be ‘/home/user/docker’.

2. Add another folder inside the ‘docker’ folder just to name it specifically as a reminder. For an example ‘php-5.4-cli’.

3. Next, create a new file with the name of ‘Dockerfile’. In order for docker to build images automatically, it will read the instructions from a file. The file name is Dockerfile. Basically, it is a text file containing all commands, in order, needed to build a given image. So, fill that file with the following content or instruction :

FROM php:5.4-cli
COPY . /home/user/docker/php-5.4-cli
WORKDIR /home/user/docker/php-5.4-cli
CMD ["php","index.php"]

There are explanations for the above script. The following are those explanations :

FROM is a reserved keywords. It will create creates a layer from the ubuntu:18.04 Docker image.
COPY is a reserved keywords for adding files from the docker client’s current directory, in this context '/home/user/docker/php-5.4-cli.
WORKDIR is a reserved keywords for setting default working directory to  run binaries within a container. By default, the working directory is the root directory (/), but by specifying it in the WORKDIR, it will have set a different default working directory. So, the php-5.4-cli binary exist in the '/home/user/docker/php-5.4-cli for the execution. 
CMD is a reserved keyword for specifying a command to run within the container. It will execute php binary with the additional parameter index.php. In other words, it will run the index.php file script. 

4. Don’t forget to create the index.php file as follows :

user@hostname:~/docker/php-5.4-cli$ touch index.php

5. After successfully creating the file, fill it with the following content just for an example :

echo "Hello World \n";

6. Next, execute the following command to build the docker image container but don’t forget to switch to ‘root’ account first :

user@localhost:/home/user/docker/php-5.4-cli# sudo su - 
[sudo] password for user: 

7. For building the docker image container, execute the following command inside the working folder directory :

docker build -t [app_name] [docker_folder_location]

Below is the above command execution :

root@localhost:/home/user/docker/php-5.4-cli# docker build -t app .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM php:5.4-cli
---> 6f56a91020df
Step 2/4 : COPY . /home/user/docker/php-5.4-cli
---> Using cache
---> d339950aa50a
Step 3/4 : WORKDIR /home/user/docker/php-5.4-cli
---> Using cache
---> 61d337d4dc52
Step 4/4 : CMD ["php","index.php"]
---> Using cache
---> a7eabf814a8f
Successfully built a7eabf814a8f
Successfully tagged app:latest

8. After successfully building the image, run the docker image container with the following command :

docker run -it --rm --name app app

The above command execution is in the following output :

root@localhost:/home/user/docker/php-5.4-cli# docker run --rm --name [docker_container_name] [docker_image_name] 
Hello World ! 
run is one of the command available upon executing docker. 
--rm is the additional attribute parameter to automatically remove the container after running
--name is the addtional attribute parameter for specifying the docker container name

9. Finally, according to the output above, the execution for running docker is a success. The output of the file which is the “Hello World” appear in the command line.

Leave a Reply