EDGE: Elastic Dockerfile Generating Environment
EDGE is the environment to develop generic Dockerfile. EDGE will provide the easier method to build container image from Dockerfile and test the Dockerfile. By this method, you can realize TDD(Test Driven Development) of Dockerfile easily.
$ git clone https://github.com/bbrfkr/edge
-
Install dependent package.
# gem install docker-api serverspec inifile activesupport
-
Change directory to edge repository.
$ cd edge
-
Check
Bin/edge
command is executable.
$ Bin/edge
edge: Elastic Dockerfile Generating Environment
=== Usage ===
* Bin/edge [-h] [-v]
-h : show this help message
-v : show version
* Bin/edge list
show installed project list
* Bin/edge params PROJECT
show default parameters of the specified project
* Bin/edge build PROJECT
build the specified project
* Bin/edge spec PROJECT
test the specified project
- create httpd project
$ mkdir Projects/httpd
$ mkdir Projects/httpd/properties
$ mkdir Projects/httpd/spec
$ touch Projects/httpd/Dockerfile
$ touch Projects/httpd/properties/default.ini
$ touch Projects/httpd/spec/main_spec.rb
$ touch Env/httpd.ini
- describe mock in
Projects/httpd/Dockerfile
FROM docker.io/centos:latest
- describe default image name of httpd project in
Projects/httpd/properties/default.ini
image=edge_test/httpd:latest
[args]
- describe test code in
Projects/httpd/spec/main_spec.rb
require './.spec_helper'
describe ("check httpd package is installed") do
describe package("httpd") do
it { should be_installed }
end
end
describe ("check httpd process is running") do
describe process("httpd") do
it { should be_running }
end
end
- run test and confirm test fails
$ Bin/edge spec httpd
- describe implementation in
Projects/httpd/Dockerfile
FROM docker.io/centos:latest
RUN yum install -y httpd
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
- run test and confirm test succeeds
$ Bin/edge spec httpd
- describe image name used when image is built in
Env/httpd.ini
image=bbrfkr0129/httpd:latest
[args]
- build image
$ Bin/edge build httpd
- run container from the tested image
# docker run -d -p 80:80 bbrfkr0129/httpd:latest
EDGE has the following directory architecture;
./edge
|-- Bin
| `-- edge
|-- Env
| `-- [project].ini
`-- Projects
`-- [project]
|-- Dockerfile
|-- properties
| |-- default.ini
| `-- [test case].ini
`-- spec
`-- [spec file]_spec.rb
The explanation of each directory and file as follow;
- Bin/edge
The body ofBin/edge
command. Through this command, you can build container image from Dockerfile and test Dockerfile. - Env/[project].ini
The ini file which has properties of the project[project]
. - Projects/[project]
The project[project]
directory. In EDGE, project is a unit of building container image and testing Dockerfile. - Projects/[project]/Dockerfile
The Dockerfile which belongs to the project[project]
. For this Dockerfile, container image will be built and test will be executed. - Projects/[project]/properties/default.ini
The ini file which has default parameters of the project[project]
. All parameters of the project[project]
should be described in this file. - Projects/[project]/properties/[test case].ini
The ini file which has properties of the project[project]
for test. The ini files of the directoryProjects/[project]/properties
will be used for test. - Projects/spec/[spec file]_spec.rb
The body of code for test. In EDGE, test is executed by serverspec. Therefore, the test code should be written according to the syntax of serverspec.
Properties file is an ini file used when project is built or tested. Properties file contains the following contents.
image=[image_name]
[args]
[key]=[value]
[key]=[value]
In first entry image=[image_name]
, describe the name of container image created from Dockerfile when the project is built or tested. In [args]
section, define key and value used in Dockerfile as arguments.
After creating properties file, if the file is used when project [project]
is built, put the file in Env
directory and rename the file [project].ini
. if the file is used when project [project]
is tested, put the file in Projects/[project]/properties
directory.
Each project should have special properties file default.ini
in directory Projects/[project]/properties
. the properties of default.ini
are used as default values when the project is built or tested.
Write test code according to the syntax of serverspec, then put the test code in the directory Projects/[project]/spec
, and rename the test code *_spec.rb
. In the test code, describe statement to load spec_helper
. Moreover, properties described properties file can be called by writing as property['args']['[key]']
;
require './.spec_helper'
describe ("check ruby version") do
describe command("source /root/.bash_profile && rbenv version") do
its(:stdout) { should match /^#{ property['args']['RUBY_VER'] }\s/ }
end
end
After writing test code, execute test and confirm the test is failed. To do this, execute the following command;
Bin/edge spec [project]
Note that in the test, properties of the properties file in the directory Projects/[project]/properties
are used, and for each of properties files test is executed.
Write Dockerfile as to pass the tests of the written test code. Here, properties described properties file are passed via the docker command option --build-arg
.
After writing Dockerfile, execute test and confirm the test is succeeded. To do this, execute the following command;
Bin/edge spec [project]
After all tests for Dockerfile are passed, build Dockerfile to make container image. To do this, execute the following command;
Bin/edge build [project]
Note that in the build, properties of the properties file [project].ini
in the directory Env
are used.
Run a container from the completely tested container image!
To view the projects defined in EDGE, execute the following command.
$ Bin/edge list
To show the properties is defined in project [project]
, execute the following command.
$ Bin/edge params [project]