Docker

How to Check PHP Version in Docker Container?

Docker is a popular forum used for containerizing applications. It is utilized by millions of users during the implementation of extensive development projects. Users can build Dockerfiles, images, and containers. Moreover, you can install different extensions including PHP, Python, etc., in your Dockerfile and also check their versions.

This article will explain the methods to check the PHP version in the Docker container.

How to Check Version of PHP in Docker Container?

There are two methods to check the PHP version in the Docker container, such as:

Method 1: Checking PHP Version While Executing the Container

To check the PHP version in the Docker container, first, execute the “docker run –rm -it php:7.2 bash” command to run the Docker container and enable Bash to write the commands in PHP inside the Docker container. Then, check the PHP version using the “php -v” command.

Step 1: Execute Docker Container

First, run the Docker container using the provided command:

docker run --rm -it php:7.2 bash

Here:

  • docker run” command is used to run a container based on a specified image.
  • –rm” option automatically removes/deletes the container when it exists.
  • -it” flag tells Docker to open an interactive terminal inside the container.
  • php:7.2” is the official Docker image on Docker Hub to use for the container.
  • bash” option is specified to tell Docker to start a Bash shell inside the container once it is running.

After executing the above-stated command, Docker will download the “php:7.2” image, start a new container and then open a Bash shell to run commands inside the Docker container:

Step 2: Check PHP Version

Now, execute the following command in the Bash shell to check the PHP version in Docker container:

php -v

In the below output, the PHP version can be seen, i.e., “PHP 7.2.34”:

Method 2: Checking PHP Version Using “docker exec” Command

To check the PHP version, first, build and run the container from the image. Then, execute the “docker exec -it <container-name> sh” command to start a shell within the Docker container. Lastly, execute the “php -v” command to check the PHP version.

Step 1: Create and Run Container From Image

First, run the provided command to build and run the Docker container from Docker image:

docker run -it --name php-cont php:7.2

Here:

  • -it” is used to run Docker in interactive mode.
  • –name” is used to define the container name. For instance, we have defined “php-cont”.
  • php:7.2” is the Docker image:

Step 2: Open Bash Shell

Then, open a new terminal and write out the given-below command to open a Bash shell:

docker exec -it php-cont sh

Here:

  • exec” is used to run a command inside a running container.
  • -it” flag runs Docker in interactive mode.
  • php-cont” is the container name.
  • sh” starts a new shell session inside the container:

Step 3: Check PHP version

Lastly, check the PHP version within the Docker container:

php -v

The below image will display the PHP version in the Docker container:

That was all about checking the PHP version in the Docker container.

Conclusion

To check the PHP version in the Docker container, utilize the “docker run –rm -it php:7.2 bash” command to run the Docker container and enable Bash to write the commands in PHP inside the Docker container. Alternatively, users can build and run the Docker container from the Docker image and then run the “docker exec -it <container-name> sh” command to start a shell inside the Docker container. This article explained the methods to check the PHP version in the Docker container.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.