{"id":5531,"date":"2019-08-21T11:17:37","date_gmt":"2019-08-21T15:17:37","guid":{"rendered":"\/db-blog\/?p=5531"},"modified":"2020-03-11T19:26:14","modified_gmt":"2020-03-11T23:26:14","slug":"docker-launch-a-web-server-and-a-php-processor","status":"publish","type":"post","link":"https:\/\/droidbasement.com\/db-blog\/docker-launch-a-web-server-and-a-php-processor\/","title":{"rendered":"Docker \u2013 Launch a Containerized Web Server and a PHP Processor"},"content":{"rendered":"\n<p>In this article we will use docker compose to spin up two containers.  One will run a web server (Nginx) and one will run php-fpm (Hypertext Preprocessor FastCGI Process Manager).  All requests for .php will be forwarded on to the php-fpm service for processing which is running in a separate container and be presented by Nginx.  We will use images based off of <a href=\"https:\/\/alpinelinux.org\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Alpine (opens in a new tab)\">Alpine<\/a> which is a minimal Linux environment with a small footprint.<\/p>\n\n\n\n<p>Please go through <a rel=\"noreferrer noopener\" aria-label=\"aws-azure-development-environment-setup-for-containerization (opens in a new tab)\" href=\"https:\/\/droidbasement.com\/db-blog\/aws-azure-development-environment-setup-for-containerization\/\" target=\"_blank\">aws-azure-development-environment-setup-for-containerization<\/a> prior to commencing with this article.<\/p>\n\n\n\n<p>&#8211;&gt;<br>Change directory in to the dev\/docker path located within your home directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd ~\/dev\/docker<\/pre>\n\n\n\n<p>Create a directory called myweb and go in to it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mkdir myweb &amp;&amp; cd myweb<\/pre>\n\n\n\n<p>Create five directories called nginx, nginx\/conf, php-fpm, src and src\/web:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mkdir -p nginx\/conf php-fpm -p src\/web<\/pre>\n\n\n\n<p>Create a docker-compose.yml file like so:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat &lt;&lt; 'EOF' &gt; docker-compose.yml\n&gt; version: '3'\n&gt;\n&gt; services:\n&gt;     nginx:\n&gt;         image: myweb-nginx:v1\n&gt;         build: .\/nginx\n&gt;         volumes:\n&gt;             - .\/src\/web:\/var\/www\/html\n&gt;         ports:\n&gt;             - 80:80\n&gt;         depends_on:\n&gt;             - php-fpm\n&gt;\n&gt;     php-fpm:\n&gt;         image: myweb-php-fpm:v1\n&gt;         build: .\/php-fpm\n&gt;         volumes:\n&gt;             - .\/src\/web:\/var\/www\/html\n&gt; EOF<\/pre>\n\n\n\n<p>This sets the version to 3 for docker-compose and sets up two services called nginx and php-fpm.  It will build what is inside each folder, overlay src\/web in to them (so you can work independently on the content without rebuilding the container) and map port 80 (HTTP) to the inside HTTP\/80 port.  Nginx requires php-fpm so you denote that with a depends_on; this service will start first.  The &#8216;image:&#8217; will name and tag this accordingly on the docker images.<\/p>\n\n\n\n<p>Create a Dockerfile inside nginx:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat &lt;&lt; 'EOF' &gt; nginx\/Dockerfile\n&gt; FROM nginx:stable-alpine\n&gt; COPY .\/conf\/default.conf \/etc\/nginx\/conf.d\n&gt; EOF<\/pre>\n\n\n\n<p>Create a default.conf inside nginx\/conf:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat &lt;&lt; 'EOF' &gt; nginx\/conf\/default.conf\n&gt; server {\n&gt;    index index.php;\n&gt;    server_name \"\";\n&gt;    error_log \/var\/log\/nginx\/error.log;\n&gt;    access_log \/var\/log\/nginx\/access.log;\n&gt;    root \/var\/www\/html;\n&gt;\n&gt;    location ~\\.php$ {\n&gt;        try_files $uri =404;\n&gt;        fastcgi_split_path_info ^(.+.php)(\/.+)$;\n&gt;        fastcgi_pass php-fpm:9000;\n&gt;        fastcgi_index index.php;\n&gt;        include fastcgi_params;\n&gt;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n&gt;        fastcgi_param PATH_INFO $fastcgi_path_info;\n&gt;    }\n&gt; }\n&gt; EOF<\/pre>\n\n\n\n<p>This will pull the stable Nginx docker image based off of Alpine and copy our default.conf in to the container.  The default.conf sets up the web server to forward .php over to our container which is called &#8216;php-fpm&#8217; on the default port of 9000 and sets the default index page as index.php.<\/p>\n\n\n\n<p>Create a Dockerfile inside php-fpm:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat &lt;&lt; 'EOF' &gt; php-fpm\/Dockerfile\n&gt; FROM php:7.3-fpm-alpine\n&gt; EOF<\/pre>\n\n\n\n<p>This will pull the php-fpm 7.3 docker image based off of Alpine.<\/p>\n\n\n\n<p>Create a index.php in src\/web:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat &lt;&lt; 'EOF' &gt; src\/web\/index.php\n&gt; &lt;html&gt;\n&gt;  &lt;head&gt;\n&gt;   &lt;title&gt;Hello World - PHP Test&lt;\/title&gt;\n&gt;  &lt;\/head&gt;\n&gt;  &lt;body&gt;\n&gt;   &lt;?php echo '&lt;p&gt;Hello World&lt;\/p&gt;'; ?&gt;\n&gt;  &lt;\/body&gt;\n&gt; &lt;\/html&gt;\n&gt; EOF<\/pre>\n\n\n\n<p>Now build both containers\/launch both services and send in to non-interactive mode:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker-compose up -d<\/pre>\n\n\n\n<p>Now use curl to make a request:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ curl http:\/\/localhost<\/pre>\n\n\n\n<p>You should see:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html&gt;\n &lt;head&gt;\n  &lt;title&gt;Hello World - PHP Test&lt;\/title&gt;\n &lt;\/head&gt;\n &lt;body&gt;\n  &lt;p&gt;Hello World&lt;\/p&gt;\n &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p>You can see the containers running by typing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker ps<\/pre>\n\n\n\n<p>You can list the containers:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker container ls<\/pre>\n\n\n\n<p>You can stop the containers:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker-compose stop<\/pre>\n\n\n\n<p>You can remove the containers:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker-compose down<\/pre>\n\n\n\n<p>You can list the images:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker images<\/pre>\n\n\n\n<p>&lt;&#8211;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>References:<\/p>\n\n\n\n<p>Source: <br><a href=\"https:\/\/github.com\/pershoot\/docker_myweb\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"docker_myweb (opens in a new tab)\">docker_myweb<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we will use docker compose to spin up two containers. One will run a web server (Nginx) and one will run php-fpm (Hypertext Preprocessor FastCGI Process Manager). All requests for .php will be forwarded on to the php-fpm service for processing which is running in a separate container and be presented by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5531","post","type-post","status-publish","format-standard","hentry","category-devops"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/5531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/comments?post=5531"}],"version-history":[{"count":21,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/5531\/revisions"}],"predecessor-version":[{"id":6215,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/5531\/revisions\/6215"}],"wp:attachment":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/media?parent=5531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/categories?post=5531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/tags?post=5531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}