📜 ⬆️ ⬇️

Creating an environment for web development based on Docker

Under the cut, I'll tell you how I improved the automatic creation and deployment of the environment for web development based on Docker , Fig , DNSMasq and nsenter . In essence, this is the deployment of the LAMP server and its record in DNSMasq, but the priorities are the non-clogging of the host machine, unnecessary software like web and db servers on the host machine and the minimum number of commands to start

Foreword


In a previous article, http://habrahabr.ru/post/236573/, I described how to organize a quick deployment of the environment for web development based on VirtualBox , at the same time, more experienced users have suggested that I look towards Docker. After that, I opened the manus and started experimenting, collecting for myself three containers with different versions of PHP ( 5.3 , 5.4 and 5.5 ), which I used successfully and conveniently. There was a desire in the future to rewrite the bootstrap script in a more sane language and somehow organize it all and write mana. But, as always, my hands would not reach this point and, most likely, would never have arrived if I hadn't accidentally deleted the docker's home directory on Christmas Eve. Yes, sometimes. In the end, everything was rewritten, reorganized and uploaded to GitHub .

What do we need?


  1. Docker.IO ( https://www.docker.com/ )
  2. fig ( http://www.fig.sh )
  3. DNSMasq ( http://www.thekelleys.org.uk/dnsmasq/doc.html )
  4. nsenter ( https://github.com/jpetazzo/nsenter )


What's happening?


When launched through a script using fig , all necessary containers are expanded and linked. After that, if a file with a database dump is specified, the dump is poured into the created database in the container. Next, the container entries are added to the DNSMasq config and the daemon is restarted.
When you turn off using the script, the database is back dumped into a file, records are removed from the DNSMasq config, and the daemon is restarted.
')

How to set it up?


A minimum DNSMasq setting is required :
 # cat /etc/dnsmasq.conf | grep -E -v '^(#.*)?$' listen-address=127.0.0.1 

To work with all this .efig , we clone the repository https://github.com/dvapelnik/efig and look for the .efig daddy .efig , which we put in the folder with the project. In this folder is already there:
  1. logs - web server log directory
  2. xd_profile , xd_trace - two directories for XDebug files
  3. db - directory for working with databases with two scripts for deployment and backup
  4. efig.yml - config for fig
  5. efig.conf - config of efig itself
  6. httpd.conf - apache2 config
  7. efig.sh - the efig script itself for work

In efig.yml you need to specify the database name, user name for the database and password. If necessary, then a database for tests. In order for the database to be correctly deployed and dumped back, you should indicate how the names of the databases are associated with the dump files.
E_DB_DUMPfile name for the main database
E_DB_NAMEprimary database name
E_DB_TEST_DUMPtest database file name
E_DB_TEST_NAMEtest database name

If the test database is not needed, then the last two lines can be commented and removed the name of the test database from the variable DB_NAME . Files with dumps will be searched against the db folder.
httpd.conf configurable for your application.
The following values ​​are specified in efig.conf
PROJECT_NAMEproject name (to be used in the URL )
FIG_CONFconfig name for fig
SUBDOMAINS_ENABLEDwhether you need to create subdomains for each container
DNS_ZONEDNS zone for projects, initially used .doc
MAIN_CONTAINER_NAMEthe name of the main container, initially web (take from the fig -config)
DB_CONTAINER_NAMEname of the container db , initially db (take from fig config)
DNSMASQ_CONFIG_PATHDNSMasq configuration path

Addition to SUBDOMAINS_ENABLED
For example, so that the database container can be accessed by a domain name (for example, http: //db.myproject.doc/ , and not by IP, which will constantly change with each launch).


And now with all these configs, folders and scripts we will try to fly try to run


In order to take off, you need to go to the .efig folder and run the script through sudo :
 sudo ./efig.sh 

At this time, the containers are started, the databases are deployed, the new containers are written to /etc/dnsmasq.conf , and the daemon is restarted. After that, we can safely go to the link in the browser http: //project.doc/ and watch your project already in the browser.
In order to disable-remove containers and back up the bases, we write, .efig following in the same folder ( .efig ):
 sudo ./efig.sh rm 

The bases will be dumped back into the files, the containers will stop and be deleted - everything is clean, as intended.

What images need to be used for containers?


I advise you to use images that have been configured in the same way as web containers (images based on Debian / Ubuntu with different versions of PHP (5.3, 5.4, 5.5, 5.6) are available on DockerHub ). Packages for PHP were tailored to the requirements of YiiFramework (1, 2). If necessary, you can add other development packages you need. As db-containers, I use the sameersbn image.

And to practice?


You can try to deploy, for example, the same Joomla CMS (it first came to my mind as a CMS, which is easy to deploy and it will generate the database):
  1. Clone efig from github
  2. We pull archive Joomla CMS
  3. Unpacking
  4. Copy .efig to a folder with Joomla CMS
  5. Specify the parameters for the database in .efig/efig.yml
  6. Run this case cd .efig/ && sudo bash ./efig.sh
  7. Rejoice life We look / set
  8. Stop-delete cd .efig/ && sudo bash ./efig.sh rm


Past, present and future?


At a minimum, this was written for myself in order to ease the deployment of applications, at least in order to run. I do not know how anyone, but it bothers me to create somewhere a new subdomain and upload files there or use subfolders for different projects. Besides, I wanted to have all 4 versions of PHP . As requests and my needs, I will finish what is already there. I plan to fasten support for PostgreSQL , but since I don’t use it myself, I haven’t screwed it. The script has run in on Ubuntu OS , but I do not think that problems should arise on other Linux distributions. Check on other distributions was not possible.

Links?


  1. Project repository on GitHub : https://github.com/dvapelnik/efig
  2. Image repository for Docker on GitHub : https://github.com/dvapelnik/docker-lap
  3. Image repository for Docker on DockerHub : https://registry.hub.docker.com/u/dvapelnik/docker-lap/


Underwater rocks?


  1. If there is no database dump and the database is generated by itself (migrations, deployment as in Joomla CSM , etc.), then when the containers are disconnected, the base will be dumped, but it will be dumped as root . This is due to the fact that in containers we dump actually under the container’s ruts. Before starting, you can create an empty file into which the database will be dumped when it is shut down - this is such a workaroud. The situation is similar with web containers. If you mount a folder in a container, then all the files that will be created from the container will be created by root. I describe the workaround that I used: a donkey user is created in the container with a UID and GID like my user, under which I will be developing. I have it equal to 1000. If the UID and GID of your user is different from 1000, then you need to take the corresponding Dockerfile and replace the user's UID and GID there and rebuild the image. For these database images, this is not particularly critical, but when dumping it gets sideways. Because such a crutch.
  2. A reasonable question arises: how to get to the database inside the container? It is logical and for this reason I made the option to create domain names for all SUBDOMAINS_ENABLED containers. If the flag is set to 1 , then all containers will be created by an entry in the DNSMasq config in the form CONTAINER_NAME.PROJECT_NAME.DNS_ZONE CONTAINER_NAME.PROJECT_NAME.DNS_ZONE . The container spits out the port for access to the databases and can be reached using this domain, port and user data, which were recorded in efig.conf

Source: https://habr.com/ru/post/247547/


All Articles