(window.webpackJsonp=window.webpackJsonp||[]).push([[2449],{2857:function(e,t,a){"use strict";a.r(t);var s=a(31),r=Object(s.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"docker-deployment"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#docker-deployment"}},[e._v("#")]),e._v(" Docker deployment")]),e._v(" "),a("p",[a("a",{attrs:{href:"http://www.docker.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("Docker"),a("OutboundLink")],1),e._v(" is a very popular container solution being used widely for deploying code in production environments. It makes it easier to "),a("strong",[e._v("Manage")]),e._v(" and "),a("strong",[e._v("Scale")]),e._v(" web-applications and microservices.")]),e._v(" "),a("h2",{attrs:{id:"get-docker-image-for-php"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#get-docker-image-for-php"}},[e._v("#")]),e._v(" Get docker image for php")]),e._v(" "),a("p",[e._v("In order to deploy the application on docker, first we need to get the image from registry.")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[e._v("docker pull php\n\n")])])]),a("p",[e._v("This will get you the latest version of image from "),a("strong",[e._v("official php repository")]),e._v(". Generally speaking, "),a("code",[e._v("PHP")]),e._v(" is usually used to deploy web-applications so we need an http server to go with the image. "),a("code",[e._v("php:7.0-apache")]),e._v(" image comes pre-installed with apache to make deployment hastle free.")]),e._v(" "),a("h2",{attrs:{id:"writing-dockerfile"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#writing-dockerfile"}},[e._v("#")]),e._v(" Writing dockerfile")]),e._v(" "),a("p",[a("code",[e._v("Dockerfile")]),e._v(" is used to configure the custom image that we will be building with the web-application codes. Create a new file "),a("code",[e._v("Dockerfile")]),e._v(" in the root folder of project and then put the following contents in the same")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token constant"}},[e._v("FROM")]),e._v(" php"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(":")]),a("span",{pre:!0,attrs:{class:"token number"}},[e._v("7.0")]),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("apache\n"),a("span",{pre:!0,attrs:{class:"token constant"}},[e._v("COPY")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("etc"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("php"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("php"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v(".")]),e._v("ini "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("usr"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("local"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("etc"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("php"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("\n"),a("span",{pre:!0,attrs:{class:"token constant"}},[e._v("COPY")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v(".")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("var")]),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("www"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("html"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("/")]),e._v("\n"),a("span",{pre:!0,attrs:{class:"token constant"}},[e._v("EXPOSE")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token number"}},[e._v("80")]),e._v("\n\n")])])]),a("p",[e._v("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.")]),e._v(" "),a("p",[e._v("Second line is simply to upload "),a("code",[e._v("php.ini")]),e._v(" file to our image. You can always change that file to some other custom file location.")]),e._v(" "),a("p",[e._v("The third line would copy the codes in current directory to "),a("code",[e._v("/var/www/html")]),e._v(" which is our webroot. Remember "),a("code",[e._v("/var/www/html")]),e._v(" inside the image.")]),e._v(" "),a("p",[e._v("The last line would simply open up port 80 inside the docker container.")]),e._v(" "),a("h3",{attrs:{id:"ignoring-files"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#ignoring-files"}},[e._v("#")]),e._v(" Ignoring files")]),e._v(" "),a("p",[e._v("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 "),a("code",[e._v(".env")]),e._v(". Now in order to ignore this file, we can simply add it to "),a("code",[e._v(".dockerignore")]),e._v(" in the root folder of our codebase.")]),e._v(" "),a("h2",{attrs:{id:"building-image"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#building-image"}},[e._v("#")]),e._v(" Building image")]),e._v(" "),a("p",[e._v("Building image is not something specific to "),a("code",[e._v("php")]),e._v(", but in order to build the image that we described above, we can simply use")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[e._v("docker build "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("t "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("Image name"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v(".")]),e._v("\n\n")])])]),a("p",[e._v("Once the image is built, you can verify the same using")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[e._v("docker images\n\n")])])]),a("p",[e._v("Which would list out all the images installed in your system.")]),e._v(" "),a("h2",{attrs:{id:"starting-application-container"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#starting-application-container"}},[e._v("#")]),e._v(" Starting application container")]),e._v(" "),a("p",[e._v("Once we have an image ready, we can start and serve the same. In order to create a "),a("code",[e._v("container")]),e._v(" from the image, use")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[e._v("docker run "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("p "),a("span",{pre:!0,attrs:{class:"token number"}},[e._v("80")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(":")]),a("span",{pre:!0,attrs:{class:"token number"}},[e._v("80")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("d "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("Image name"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v("\n\n")])])]),a("p",[e._v("In the command above "),a("code",[e._v("-p 80:80")]),e._v(" would forward port "),a("code",[e._v("80")]),e._v(" of your server to port "),a("code",[e._v("80")]),e._v(" of the container. The flag "),a("code",[e._v("-d")]),e._v(" tells that the container should run as background job. The final specifies which image should be used to build the container.")]),e._v(" "),a("h3",{attrs:{id:"checking-container"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#checking-container"}},[e._v("#")]),e._v(" Checking container")]),e._v(" "),a("p",[e._v("In order to check running containers, simply use")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[e._v("docker ps\n\n")])])]),a("p",[e._v("This will list out all the containers running on docker daemon.")]),e._v(" "),a("h3",{attrs:{id:"application-logs"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#application-logs"}},[e._v("#")]),e._v(" Application logs")]),e._v(" "),a("p",[e._v("Logs are very important to debug the application. In order to check on them use")]),e._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[e._v("docker logs "),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("Container id"),a("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v("\n\n")])])]),a("h4",{attrs:{id:"remarks"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#remarks"}},[e._v("#")]),e._v(" Remarks")]),e._v(" "),a("p",[e._v("This document assumes that docker is installed and the daemon running. You can refer to "),a("a",{attrs:{href:"https://docs.docker.com/engine/installation/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Docker installation"),a("OutboundLink")],1),e._v(" to check on how to install the same.")])])}),[],!1,null,null,null);t.default=r.exports}}]);