|
| 1 | +## Introduction |
| 2 | +This is a Dockerfile to build a container image for nginx and php-fpm, with the ability to pull website code from git. The container can also use environment variables to configure your web application using the templating detailed in the special features section. |
| 3 | + |
| 4 | +### Git reposiory |
| 5 | +The source files for this project can be found here: [https://github.com/ngineered/nginx-php-fpm](https://github.com/ngineered/nginx-php-fpm) |
| 6 | + |
| 7 | +If you have any improvements please submit a pull request. |
| 8 | + |
| 9 | +### Docker hub repository |
| 10 | +The Docker hub build can be found here: [https://registry.hub.docker.com/u/richarvey/nginx-php-fpm/](https://registry.hub.docker.com/u/richarvey/nginx-php-fpm/) |
| 11 | + |
| 12 | +## Nginx Versions |
| 13 | +- Mainline Version: **1.7.9** |
| 14 | +- Stable Version: **1.6.2** |
| 15 | +- *Latest = Mainline Version* |
| 16 | + |
| 17 | +## Installation |
| 18 | +Pull the image from the docker index rather than downloading the git repo. This prevents you having to build the image on every docker host. |
| 19 | + |
| 20 | +``` |
| 21 | +docker pull richarvey/nginx-pfp-fpm:latest |
| 22 | +``` |
| 23 | +To pull the Stable Version: |
| 24 | + |
| 25 | +``` |
| 26 | +docker pull richarvey/nginx-pfp-fpm:stable |
| 27 | +``` |
| 28 | +## Running |
| 29 | +To simply run the container: |
| 30 | + |
| 31 | +``` |
| 32 | +sudo docker run --name nginx -p 8080:80 -d richarvey/nginx-php-fpm |
| 33 | +``` |
| 34 | +You can then browse to http://<docker_host>:8080 to view the default install files. |
| 35 | +### Volumes |
| 36 | +If you want to link to your web site directory on the docker host to the container run: |
| 37 | + |
| 38 | +``` |
| 39 | +sudo docker run --name nginx -p 8080:80 -v /your_code_directory:/usr/share/nginx/html -d richarvey/nginx-php-fpm |
| 40 | +``` |
| 41 | +### Pulling code from git |
| 42 | +One of the nice features of this container is its ability to pull code from a git repository with a couple of environmental variables passed at run time. |
| 43 | + |
| 44 | +**Note:** You need to have your SSH key that you use with git to enable the deployment. I recommend using a special deploy key per project to minimise the risk. |
| 45 | + |
| 46 | +To run the container and pull code simply specify the GIT_REPO URL including *git@* and then make sure you have a folder on the docker host with your id_rsa key stored in it: |
| 47 | + |
| 48 | +``` |
| 49 | +sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-php-fpm |
| 50 | +``` |
| 51 | + |
| 52 | +To pull a repository and specify a branch add the GIT_BRANCH environment variable: |
| 53 | + |
| 54 | +``` |
| 55 | +sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -e 'GIT_BRANCH=stage' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-php-fpm |
| 56 | +``` |
| 57 | +### Linking |
| 58 | +Linking to containers also exposes the linked container environment variables which is useful for templating and configuring web apps. |
| 59 | + |
| 60 | +Run MySQL container with some extra details: |
| 61 | + |
| 62 | +``` |
| 63 | +sudo docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=yayMySQL -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress_user -e MYSQL_PASSWORD=wordpress_password -d mysql |
| 64 | +``` |
| 65 | + |
| 66 | +This exposes the following environment variables to the container when linked: |
| 67 | + |
| 68 | +``` |
| 69 | +MYSQL_ENV_MYSQL_DATABASE=wordpress |
| 70 | +MYSQL_ENV_MYSQL_ROOT_PASSWORD=yayMySQL |
| 71 | +MYSQL_PORT_3306_TCP_PORT=3306 |
| 72 | +MYSQL_PORT_3306_TCP=tcp://172.17.0.236:3306 |
| 73 | +MYSQL_ENV_MYSQL_USER=wordpress_user |
| 74 | +MYSQL_ENV_MYSQL_PASSWORD=wordpress_password |
| 75 | +MYSQL_ENV_MYSQL_VERSION=5.6.22 |
| 76 | +MYSQL_NAME=/sick_mccarthy/mysql |
| 77 | +MYSQL_PORT_3306_TCP_PROTO=tcp |
| 78 | +MYSQL_PORT_3306_TCP_ADDR=172.17.0.236 |
| 79 | +MYSQL_ENV_MYSQL_MAJOR=5.6 |
| 80 | +MYSQL_PORT=tcp://172.17.0.236:3306 |
| 81 | +
|
| 82 | +``` |
| 83 | + |
| 84 | +To link the container launch like this: |
| 85 | + |
| 86 | +``` |
| 87 | +sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 --link some-mysql:mysql -d richarvey/nginx-php-fpm |
| 88 | +``` |
| 89 | +### Enabling SSL or Special Nginx Configs |
| 90 | +As with all docker containers its possible to link resources from the host OS to the guest. This makes it really easy to link in custom nginx default config files or extra virtual hosts and SSL enabled sites. For SSL sites first create a directory somewhere such as */opt/deployname/ssl/*. In this directory drop you SSL cert and Key in. Next create a directory for your custom hosts such as */opt/deployname/sites-enabled*. In here load your custom default.conf file which references your SSL cert and keys at the location, for example: */etc/nginx/ssl/xxxx.key* |
| 91 | + |
| 92 | +Then start your container and connect these volumes like so: |
| 93 | + |
| 94 | +``` |
| 95 | +sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -v /opt/deployname/ssl:/etc/nginx/ssl -v /opt/deplyname/sites-enabled:/etc/nginx/sites-enabled -p 8080:80 --link some-mysql:mysql -d richarvey/nginx-php-fpm |
| 96 | +``` |
| 97 | + |
| 98 | +## Special Features |
| 99 | + |
| 100 | +### Templating |
| 101 | +This container will automatically configure your web application if you template your code. For example if you are linking to MySQL like above, and you have a config.php file where you need to set the MySQL details include $$_MYSQL_ENV_MYSQL_DATABASE_$$ style template tags. |
| 102 | + |
| 103 | +Example: |
| 104 | + |
| 105 | +``` |
| 106 | +<?php |
| 107 | +database_name = $$_MYSQL_ENV_MYSQL_DATABASE_$$; |
| 108 | +database_host = $$_MYSQL_PORT_3306_TCP_ADDR_$$; |
| 109 | +... |
| 110 | +?> |
| 111 | +``` |
| 112 | + |
| 113 | +### Using environment variables |
| 114 | +If you want to link to an external MySQL DB and not using linking you can pass variables directly to the container that will be automatically configured by the container. |
| 115 | + |
| 116 | +Example: |
| 117 | + |
| 118 | +``` |
| 119 | +sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -e 'GIT_BRANCH=stage' -e 'MYSQL_HOST=host.x.y.z' -e 'MYSQL_USER=username' -e 'MYSQL_PASS=password' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-php-fpm |
| 120 | +``` |
| 121 | + |
| 122 | +This will expose the following variables that can be used to template your code. |
| 123 | + |
| 124 | +``` |
| 125 | +MYSQL_HOST=host.x.y.z |
| 126 | +MYSQL_USER=username |
| 127 | +MYSQL_PASS=password |
| 128 | +``` |
| 129 | +To use these variables in a template you'd do the following in your file: |
| 130 | + |
| 131 | +``` |
| 132 | +<?php |
| 133 | +database_host = $$_MYSQL_HOST_$$; |
| 134 | +database_user = $$_MYSQL_USER_$$; |
| 135 | +database_pass = $$_MYSQL_PASS_$$ |
| 136 | +... |
| 137 | +?> |
| 138 | +``` |
| 139 | +### Template anything |
| 140 | +Yes ***ANYTHING***, any variable exposed by a linked container or the **-e** flag lets you template your config files. This means you can add redis, mariaDB, memcache or anything you want to your application very easily. |
0 commit comments