Practical Examples of Using Docker: Grossum + Symfony + GitHub
March 28th, 2017
Docker is a DevOps open-source tool for virtualization on system core level based on Linux. We have already discussed the reasons you may consider using Docker for your projects. Today we’re going to talk about how to set it up.
Take Ubuntu 14.04.4 as a basis. There are later releases but they haven’t been tested as much as we prefer to use the Linux version we’re sure about.
What does the code do:
#Create docker user: to make sure the servers aren’t launched by root, but by docker user. This also takes care of the potential problems with log and cache creation in Symfony (PHP5fpm is also launched by docker user)
#install PHP: install PHP + Nginx and substitute the default config files by the ones that have been adapted to work with Symfony.
#MySQL install + password: install MySQL server and set the root password for the root user. If you prefer, you can use other database analogs such as postGRE, mariaDB, mongoDB, percona, etc.
#SSH service: install the service to get the ability to connect to the container using SSH. It’s not a required step, but if you skip this one, you would have to use docker attach otherwise.
#configs bash start: implement the services autostart system via bash script (autostart.sh). If you skip this step, the services you need won’t launch.
#Install locale: install the languages. This step is a recommended one because some of the libraries might not work correctly if you haven’t specified the locales beforehand.
#Install Java 8 и ant: ant is an automatic project compiler. If you do not install it, you would have to do everything manually. You can substitute it for analog projects like Maven or other.
#Composer: Symfony does not work correctly if the composer isn’t installed (it is used to upload vendors).
#Code standard: a useful feature that helps to check the code inside the container.
#Autocomplete symfony2: also a useful (not required) feature that speeds up the programming process (used in doctrine, etc.)
#etcKeeper: this is an admin tool for logging the changes in services configurations.
#open ports: open the ports that you need for work with the container from the outside. By default, all ports are closed initially, so even if someone scans your container, they will not find anything.
To open the ports manually afterward, you have to do it all the time whenever you work and if you want to later change that, you would have to recompile your container from the very beginning, specifying the ports that should be open. Again, this can be done without downtime, but your servers might get overloaded. Therefore, to avoid all the headache, specify the ports when you create the image.
Once you’ve created the image for Symfony2:
docker run -i -t -d --name=symfony2 -h=symfony2 -p 1080:80 -p 1022:22 -p 9001:9000 grossumua/symfony2 /bin/bash
The container is based on the grossumua/symfony2 with shell script start (/bin/bash) for services autostart with open ports 80 (web server), 22 (SSH), 9000 (xdebug) that are pushed outside through the ports 1080, 1022, 9001 accordingly, where:
- -h (host name) - you can specify any other name you like here.
- --name (container name) - create a unique name for the container so that it would be easier later to distinguish among them (not by their IDs)
- -i, -t, -d - interactive, terminal, detached
For developers with mount volume:
docker run -i -t -d --name=symfony2 -h=symfony2 -p 1080:80 -p 1022:22 -p 9001:9000 -v <local path>:<container path (/var/www/)> grossumua/symfony2 /bin/bash
Instead of Shell script, you can use supervisor and put the start configs for the required services and dependencies. After that, when you create a container from an image, specify supervisor launch instead of /bin/bash.
You can also use volumes to connect the file system of the server to the project in order to synchronize the project between the developer and server. This way, you get increased speed and security, as well as easier access to the project files. Using volumes also is convenient because you do not have to specify a backup for each container separately, but rather create a backup for all folders / volumes on the server.
Notes about using Symfony3 + PHP7
The main difference is in the line #Install Percona MySQL 5.6 server. You can use the standard MySQL (or other databases, such as mongoDB, etc.), but Percona is convenient to use because of the way it handles backups. Keep in mind, however, that the filters and sorting preferences are different from the standard MySQL ones.