Overview
In building the development environment, we all hope that the building process can be simple, and once and for all, other colleagues can reuse the already built development environment to save development time. When building a development environment, we are often plagued by complex configurations and repetitive download installations. Before Docker technology was available, we could manage complex configurations using configuration management tools such as Pupet, Chef, ansible, and so on, which is still one of the more popular ways to manage configuration. The configuration management tools use their own DSL syntax definitions, and given the complexity of the environment, configuring a common set of development environments needs to be customized for each system, which is still a high maintenance cost for most development environments. After the advent of Docker technology, the system's dependencies are completely resolved, and we can simplify the installation of the environment by mirroring it. In conjunction with the Docker Development deployment Tool Fig, we can use the Fig.yml file to define all environments, once defined, used in multiple places, simple and efficient.
1.1 Ways to pack
Docker does not create a real virtual machine, it simply encapsulates additional system files based on Linux kernel and leverages Linux kernel-related technologies such as Cgroup, namespace to isolate user applications. With Docker technology, teams share an image or dockefile with the development environment. To simplify writing dockerfile, Fig provides a more streamlined DSL definition file fig.yml that allows new members to quickly build a development environment and devote their energies to the development process rather than studying how to properly install and configure databases such as PostgreSQL. Currently, the environment for software development needs image, most of which can be found in the Docker hub, you need to use the direct download.
1.2 Ways to apply a combination
After using Docker, we no longer need to install all the packages on the local machine. We can search for an image of the relevant software on the Docker hub based on the project and then use the fig pull to directly download it from the Docker hub and associate the image with the link command called Docker by Fig. This allows all applications to invoke the services of the specified port directly, such as the 3306 port of MySQL to provide the database service. Developers don't need to know much about Docker, they can debug their environment at any time by mastering a few of the common fig commands.
1.3 How the environment is shared
Fig directly defines the image, we do not need to care too much about containers or mirrors. When sharing an environment, simply share the corresponding dockerfile and Fig.yml files with colleagues, and they can run on their machines and build the environment they need, without worrying about the unintended debugging of environmental dependencies. After a team member has the GIT clone project code, it can start its own development environment with a command like this:
2. Fig Installation Guide
First, we need to install the Docker Engine, and the official installation manuals are available for the mainstream system. Here, my development machine system is a MacBook Pro, so I need to install Boot2docker to support Docker.
Next, we can install the fig run file for the corresponding system version. For example, install fig on Mac OS or on 64-bit Linux:
$ curl-l https://github.com/docker/fig/releases/download/1.0.0/fig-' uname-s '-' uname-m ' >/usr/local/bin/fig; chmod +x/usr/local/bin/fig
When the above installation method cannot be successful, we can choose to use the Python package installation method:
$ sudo pip install-u Fig
Finally, regardless of the Linux system used, after installing fig, run the following command to ensure the consistency of the environment.
$ fig--version
If all goes well, congratulations, fig installation was successful. Please follow me to learn how to configure the development environment with FIG.
3. Detailed configuration of the rails development environment
Let's configure a set of the most common rails+postgresql projects.
The first step is to make sure that the fig is properly installed on the host system, and if not, refer to the Installation Guide in the previous section.
In the second step, a file named Dockerfile is stored under the project root directory:
From Rubyrun apt-get update-qq && apt-get install-y build-essential libpq-devrun mkdir/myappworkdir/myappadd G Emfile/myapp/gemfilerun bundle Installadd. /myapp
The third step is to create a gemfile file to define the rails package. The contents are as follows:
SOURCE ' https://rubygems.org ' gem ' rails ', ' 4.0.2 '
The fourth step is to create a fig.yml file and use the following configuration file to do the final environment initialization.
DB: image:postgres ports: -"5432" Web: Build:. Command:bundle exec rackup-p volumes: -.:/ MyApp Ports: -"3000:3000" Links: -db
Fifth, the current directory is empty, you can use the following command to create a set of rails project skeleton:
$ fig Run web rails new. --force--database=postgresql--skip-bundle
When you run through the above commands, you will get a new rails project.
$ lsdockerfile rakefile config fig.yml public vendorgemfile app config.ru Lib Testreadme.rdoc bin DB log tmp
Edit the Gemfile file, remove the annotations from the Therubyracer package, and let rails rely on the JavaScript runtime environment.
Gem ' Therubyracer ', platforms:: Ruby
After all the file edits are done, run the command to create the development environment image.
$ FIG Build
After running the build command, we have an image that can be used immediately. The names of the two image are web and DB respectively. In order for the DB to connect to the Web, we usually also need to modify the DATABASE.YML to support database connections.
Development: &default adapter:postgresql encoding:unicode database:postgres pool:5 username:postgres password:ho ST:DB test: <<: *default database:myapp_test
OK, let's run it:
$ fig Up
The command line displays the following log:
Recreating figtest_db_1...creating figtest_web_1...attaching to Figtest_db_1, figtest_web_1db_1 | LOG: database system was shut under at 2014-10-01 23:53:11 utcdb_1 | LOG: autovacuum launcher starteddb_1 | LOG: database system is ready to accept Connectionsweb_1 | [2014-10-01 23:53:16] INFO Webrick 1.3.1web_1 | [2014-10-01 23:53:16] INFO Ruby 2.1.2 (2014-05-08) [X86_64-linux]web_1 | [2014-10-01 23:53:16] INFO webrick::httpserver#start:pid=1 port=3000db_1 | FATAL: Database "Myapp_development" does not existdb_1 | FATAL: Database "Myapp_development" does not existweb_1 | 192.168.59.3--[01/oct/2014 23:53:40] "get/http/1.1" 50 0 13476 0.5112web_1 | 192.168.59.3--[01/oct/2014 23:53:40] "GET%2ffavicon.ico http/1.1" 200-0.0067
The above log lets you know that the development database has not yet been created. At this point we can open another terminal and create a development database:
$ fig Run Web Rake db:create
Once all these steps have been successfully run, it is necessary to verify the correctness of the development environment. By visiting Http://localhost:3000/we should be able to see the familiar Rails Welcome page:
4. A detailed description of the Django development environment configuration
Next, let's use fig to configure a set of applications that run Django/postgresql.
First we create a new project directory and 3 files in the directory. The first file is a definition file for the Docker image : Dockerfile, which describes the software dependencies installed in the Docker container. The file is as follows:
From python:2.7env pythonunbuffered 1RUN mkdir/codeworkdir/codeadd requirements.txt/code/run pip install-r Requirement S.txtadd. /code/
The above file describes this image to install the Python dependency package specified by Requirements.txt.
The second file is requirements.txt, which is a Python-dependent package definition profile, which reads as follows:
Djangopsycopg2
Finally, fig needs to connect all the environments and run them. This file is named fig.yml . It describes the service components required by the project, the version of the specified image, how the service is connected, what volumes can be loaded inside the container, what ports can be exposed, and so on. The content is shaped like:
DB: image:postgresweb: build:. Command:python manage.py runserver 0.0.0.0:8000 volumes: -.:/ Code ports: -"8000:8000" Links: -db
Here we can use the fig run to create a Django project:
$ fig Run Web django-admin.py startproject figexample.
When you're done, you can see the new project file created in the current directory:
$ lsdockerfile fig.yml figexample manage.py requirements.txt
The next thing is to create a database link, modify figexample/settings.py's databases = ... Part.
DATABASES = {' default ': { ' ENGINE ': ' django.db.backends.postgresql_psycopg2 ', ' NAME ': ' Postgres ', ' USER ': ' Postgres ', ' HOST ': ' db ', ' PORT ': 5432, }}
This configuration information is used to connect the Postgres mirroring service.
Then, run the command fig up to start the container.
Recreating myapp_db_1...recreating myapp_web_1...attaching to Myapp_db_1, myapp_web_1myapp_db_1 |myapp_db_1 | PostgreSQL Stand-alone Backend 9.1.11myapp_db_1 | 2014-01-27 12:17:03 UTC LOG: database system is ready to accept Connectionsmyapp_db_1 | 2014-01-27 12:17:03 UTC log:
autovacuum Launcher Startedmyapp_web_1 | Validating models...myapp_web_1 |myapp_web_1 | 0 Errors Foundmyapp_web_1 | January, 2014-12:12:40myapp_web_1 | Django version 1.6.1, using Settings ' figexample.settings ' myapp_web_1 | Starting development Server at Http://0.0.0.0:8000/myapp_web_1 | Quit the server with Control-c.
At this point, you can open the browser and enter localhost:8000 to access the Django app.
Note that when you run the app, you can initialize the database. Here, make sure that fig up is running and another command-line window is opened to execute the command:
$ fig Run Web python manage.py syncdb
If you use the fig up again and again, you can realize that it will start the web and the db two images at once, and if you control-c, the database will stop the service. You can even go directly into the container with the fig run Web/bin/bash.
5. WordPress Development Environment Configuration Detailed
Fig can cope with the development needs of PHP applications.
First, create a dockerfile to support the build development with image.
$ Curl Https://wordpress.org/latest.tar.gz | TAR-XVZF-
The above command creates a directory named WordPress. To enter this directory, create the image file Dockerfile, which reads as follows:
From Stackbrew/ubuntu:13.10run apt-get update && apt-get install PHP5 php5-mysql-yadd. /code
The next step is to create the FIG.YML, which will define the Web service and MySQL DB service.
Web: build:. Command:php-s 0.0.0.0:8000-t/code ports: -"8000:8000" Links: -db volumes: -.:/ CodeDb: image:mysql Environment: mysql_database:wordpress mysql_root_password:wordpress
WordPress has a support file that needs to be modified and it is wp-config.php:
<?phpdefine (' db_name ', ' WordPress ');d efine (' Db_user ', ' root ');d efine (' Db_password ', ' WordPress ');d efine (' Db_host ', getenv ("Db_1_port_3306_tcp_addr"). ":" . Getenv ("Db_1_port_3306_tcp_port"));d efine (' db_charset ', ' UTF8 ');d efine (' db_collate ', ');d efine (' Auth_key ', ' Put Y Our unique phrase ");d efine (' Secure_auth_key ', ' put your unique phrase here ');d efine (' Logged_in_key ', ' Put your UN Ique phrase here ');d the Efine (' Nonce_key ', ' put your unique phrase here ');d efine (' Auth_salt ', ' Put your unique P Hrase here ');d the Efine (' Secure_auth_salt ', ' put your unique phrase here ');d efine (' Logged_in_salt ', ' put your unique phrase Here ');d efine (' Nonce_salt ', ' put your a unique phrase here '), $table _prefix = ' wp_ ';d efine (' Wplang ', ');d efine (' Wp_de BUG ', false), if (!defined (' Abspath ')) define (' Abspath ', DirName (__file__). '/'); require_once (Abspath. ' wp-settings.php ');
Once the above three files have been modified, you can run the fig up in this directory to start WordPress. You can use the browser to access localhost:8000 browse WordPress home page.
6. Conclusion
Using FIG to build a docker-based development environment can make our development a multiplier. In fact, if readers follow my steps to build a development environment, there will still be a lot of challenges.
First, because of the limitation of domestic bandwidth, downloading image from the official is a long waiting process. Even though Docker has used the CDN service, it is still slow to use in the domestic network. In order to be able to download quickly, I also have a VPN line as a data channel to download the image. Currently this is the ideal way to save a lot of development time.
Second, fig up does not guarantee that the image will succeed at once. But you don't need to lose heart. You can debug the container directly by Fig run Web/bin/bash. I have encountered many puzzling problems are directly in the inside run orders to solve. When you exit, the fig is automatically updated to the corresponding image. When you have the fig up again, it will use the new image to create the running container.
Third, fig up creates more than two container instances, starts the fig up again after exiting the container, and does not reuse the container instance that was previously exited, but creates a new container instance. A frequently used command like fig up will cause the expired container files to still be stored on your development machine and occupy hard disk space, and fig has not provided the corresponding commands to automatically clean them up. The solution now is to use the Docker Rm/rmi command to clear it manually.
The above summary of the actual combat experience, still can not cover the unique highlights of fig: the use of fig can define a unified running steps, so that the deployment environment can be completely isolated in a separate container environment, and the isolation environment can be in the development, testing, production of multiple steps to maintain consistency. The current fig project is still very young and requires a lot of participation in the project discussion, presenting its own questions in order to make the fig better to use, more information can be consulted here.
In Layman's Docker (v): Build a development environment based on FIG