Using Docker in PHP application
Now a days developers are using docker to develop the applications. It is used in the local or production both environment to developing, shipping and running application on the server and cloud.
Docker can be used in the existing or new application by creating a Dockerfile. This file will have all the instruction to build a docker image.
Creating docker file
Create an empty file called “Dockerfile” in the application source.
$ touch Dockerfile
I’m using an official image of the php-cli from the docker hub to build the image and use in-build web server to run our code.
COPY . /var/www/html
CMD ["php", "-S", "0.0.0.0:80"]
This is the code used in Dockerfile to build the image and run the local web-server.
Building the Image
Need to run the docker build command to build an image on the local system. This needs to be run from the directory where the Dockerfile is present.
$ docker build -t docker-php .
It will build an image and we can see the images by running the “docker images” command.
Running the Container
After building the image we need to run the container
$ docker run -p 8000:80 --name hello-php -d docker-php
After starting the container open a browser and open the http://localhost:8000 url to run the php script.