This guide helps you to deploy a PHP service in a Docker container using the D2C platform. It might be helpful for beginners as well as for advanced developers.
A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Containers isolate software from its surroundings, for example, differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure.
Please, check out the original Docker post about containers for a better understanding of all the benefits of the technology.
Creating PHP service
At first, you need to open or create any project and click “+Create service“. You will see a list of services which can be deployed with D2C:
As you can see, there are two variants for PHP: PHP-FPM and Apache+PHP.
The difference is that using PHP-FPM it requires to have an NGINX at the same host as PHP-FPM service when Apache+PHP can be used as a stand-alone app. Let’s start with Apache+PHP by clicking on it.
Creating PHP service. Service settings
The name should start with a letter and contain up to 16 characters (Latin letters, numbers, hyphen).
Each service has its unique name. Services can communicate with each other by container names (e.g.
gracie-1) or alias-names (e.g.
gracie). Moreover, we use them to create public domains like: [servicename]-[www].[accountID].[at].d2c.io for your services which are served by NGINX or HAProxy. We will talk about edge services in the next articles.
You can choose a version for your application from a list:
PHP extensions and PECL packages
You can install PHP extensions starting to enter its name or just picking from a list.
Any PHP version has its own built-in set of extensions. You can check what is included to a current one using a terminal of a container and command php-m.
Some PECL packages require to install additional libraries to be built. You should install it manually in Global Dependencies. For example: mcrypt pecl package require to install package libmcrypt-dev via apt-get (debian package manager).
If your application requires node.js or its modules (e.g. for building) you can choose what you need to install into a container:
In this field, you can specify commands for installing global dependencies and modifying Docker image of your service.
npm install -g
Creating PHP service. Application source
You can choose what to use for getting application sources. The most recommended is Git. If you use a private repository you should add an SSH key to your account. Here are manuals about adding SSH keys into your GitHub, BitBucket, GitLab accounts. Another way is to use Login/Password, but the best practice is to use SSH keys.
Another method is to specify a link to sources.
Protocols: http, https, ftp (for closed ftp you should specify login/password).
File formats: .tar.bz2, .tar.gz, .tar, .zip
Moreover, you can upload from your machine.
File formats: .tar.bz2, .tar.gz, .tar, .zip
Maximum size: 50MB
Creating PHP service. Ports
This is an important part. And this is a difference with PHP-FPM which doesn’t have this block because it cannot serve itself. Read in the next article how to deploy NGINX.
By default, application containers are started inside a private network and have dynamically assigned local IP addresses. Apps can reference each other by container name. It does not matter on which host the app is running – all private network intercommunication is transparent for all services in your account.
Access from the Internet is disabled by default (except edge services). You can enable access from the Internet while creating or editing service. For example, if you publish your application on port 8080, you can access it at ip_address_of_a_host:8080.
Creating a PHP service. Application settings
Local dependencies and code’s preparation
bower install, etc. or do some for preparation:
grunt build, etc.
Commands which are executed only once on the first container after the first deploying a service. You can use it for populate database or migration.
You can specify environment variables for your application. They can be edited after creating a service.
Creating PHP service. Advanced settings. Persistent data volumes
Click “Show advanced settings”.
The first block in this area is Persistent data volumes.
D2C separates the application itself from its data. Docker volumes are used to store persistent data. Data is stored locally on the hosts. Any data which is generated by an application should be added to Persistent data volumes. All modifications outside of these directories will be deleted after restart/rebuild/redeploy of a container/service (Docker restores the original state of a container).
When you have several containers and want to synchronise data between them you should mark “Sync” checkbox. We use Lsync for synchronisation.
However, we recommend storing user generated content in a cloud storage like Amazon S3 or CDN.
Creating PHP service. Advanced settings. Configs
Some services have default config files. Configs are editable. You can always return to defaults using the “Generate new config” button.
Moreover, you can add your additional config files. Click “+Add custom config” and specify a full path where it should be stored. These files do not change after restart/rebuild/redeploy of your applications.
After that, fill it in.
Generating new config button, in this case, erase all content because it’s a custom config.
Creating PHP service. Select hosts
You can choose one or several hosts where the similar containers will be deployed. It’s not necessary at the start, and you can scale your services after deployment. Also, at this step you can create other hosts and choose them even they are not online yet. The containers will be deployed when they are ready.
After succesful deployment your project should look like: