PHP TutorialCompile PHP ExtensionsContributing to the PHP CoreContributing to the PHP ManualCreate PDF files in PHPInstalling a PHP environment on WindowsPHP Alternative Syntax for Control StructuresPHP APCuPHP Array iterationPHP ArraysPHP Asynchronous programmingPHP Autoloading PrimerPHP BC Math (Binary Calculator)PHP Built in serverPHP CachePHP Classes and ObjectsPHP ClosurePHP Coding ConventionsPHP Command Line Interface (CLI)PHP CommentsPHP Common ErrorsPHP Compilation of Errors and WarningsPHP Composer Dependency ManagerPHP ConstantsPHP Control StructuresPHP CookiesPHP CryptographyPHP DateTime ClassPHP DebuggingPHP Dependency InjectionPHP Design PatternsPHP Docker deploymentPHP Exception Handling and Error ReportingPHP Executing Upon an ArrayPHP File handlingPHP Filters & Filter FunctionsPHP Functional ProgrammingPHP FunctionsPHP GeneratorsPHP Headers ManipulationPHP How to break down an URLPHP How to Detect Client IP AddressPHP HTTP AuthenticationPHP Image Processing with GDPHP ImagickPHP IMAPPHP Installing on Linux/Unix EnvironmentsPHP JSONPHP LocalizationPHP LoopsPHP Machine learningPHP Magic ConstantsPHP Magic MethodsPHP Manipulating an ArrayPHP mongo-phpPHP Multi Threading ExtensionPHP MultiprocessingPHP MySQLiPHP MySQLi affected rows returns 0 when it should return a positive integerPHP NamespacesPHP Object SerializationPHP OperatorsPHP Output BufferingPHP Outputting the Value of a VariablePHP Parsing HTMLPHP Password Hashing FunctionsPHP PDOPHP PerformancePHP PHPDocPHP Processing Multiple Arrays TogetherPHP PSRPHP Reading Request DataPHP RecipesPHP ReferencesPHP ReflectionPHP Regular Expressions (regexp/PCRE)PHP Secure Remeber MePHP SecurityPHP Sending EmailPHP SerializationPHP SessionsPHP SimpleXMLPHP SOAP ClientPHP SOAP ServerPHP SocketsPHP SPL data structuresPHP SQLite3PHP StreamsPHP String formattingPHP String Parsing

PHP Docker deployment

From WikiOD

Docker is a very popular container solution being used widely for deploying code in production environments. It makes it easier to Manage and Scale web-applications and microservices.

Remarks[edit | edit source]

This document assumes that docker is installed and the daemon running. You can refer to Docker installation to check on how to install the same.

Get docker image for php[edit | edit source]

In order to deploy the application on docker, first we need to get the image from registry.

docker pull php

This will get you the latest version of image from official php repository. Generally speaking, PHP is usually used to deploy web-applications so we need an http server to go with the image. php:7.0-apache image comes pre-installed with apache to make deployment hastle free.

Writing dockerfile[edit | edit source]

Dockerfile is used to configure the custom image that we will be building with the web-application codes. Create a new file Dockerfile in the root folder of project and then put the following contents in the same

FROM php:7.0-apache
COPY /etc/php/php.ini /usr/local/etc/php/
COPY . /var/www/html/

The first line is pretty straight forward and is used to describe which image should be used to build out new image. The same could be changed to any other specific version of PHP from the registry.

Second line is simply to upload php.ini file to our image. You can always change that file to some other custom file location.

The third line would copy the codes in current directory to /var/www/html which is our webroot. Remember /var/www/html inside the image.

The last line would simply open up port 80 inside the docker container.

Ignoring files[edit | edit source]

In some instances there might be some files that you don't want on server like environment configuration etc. Let us assume that we have our environment in .env. Now in order to ignore this file, we can simply add it to .dockerignore in the root folder of our codebase.

Building image[edit | edit source]

Building image is not something specific to php, but in order to build the image that we described above, we can simply use

docker build -t <Image name> .

Once the image is built, you can verify the same using

docker images

Which would list out all the images installed in your system.

Starting application container[edit | edit source]

Once we have an image ready, we can start and serve the same. In order to create a container from the image, use

docker run -p 80:80 -d <Image name>

In the command above -p 80:80 would forward port 80 of your server to port 80 of the container. The flag -d tells that the container should run as background job. The final specifies which image should be used to build the container.

Checking container[edit | edit source]

In order to check running containers, simply use

docker ps

This will list out all the containers running on docker daemon.

Application logs[edit | edit source]

Logs are very important to debug the application. In order to check on them use

docker logs <Container id>