Package: d4
D4: Dock to Develop Dynamic DockerfileabstractD4 is the tool to develop generic Dockerfile. D4 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.requirementsD4 requires the following software.dockercontainer-structure-testpython 3.4+python packagesdockerpyyamlinstall$ pip install d4quick startCreate projecthttpd$ d4 init httpd
$ cd httpdDescribe image name and registry server incommon.yamlimage_name: <your dockerhub username>/httpd:latest
registry_server: docker.ioDescribe test code intests/config.yamlschemaVersion: '2.0.0'
commandTests:
- name: "check httpd pkg"
  command: "rpm"
  args:
  - "-qa"
  expectedOutput:
  - "httpd-.*"
metadataTest:
  exposedPorts:
  - "80"
  cmd:
  - "httpd"
  - "-DFOREGROUND"Describe mock inDockerfileFROM docker.io/centos:latestRun develop subcommand and confirm test fails$ d4 developDescribe implementation inDockerfileFROM docker.io/centos:latest
RUN yum install -y httpd
EXPOSE 80
CMD ["httpd", "-DFOREGROUND"]Run develop subcommand and confirm test succeeds$ d4 developRelease image to your repository of dockerhub$ d4 release -u <your dockerhub username> -p <your dockerhub password>usageproject directory architectured4 project need to have the following directory architecture;(project name)
|-- Dockerfile
|-- build.yaml
|-- common.yaml
`-- tests
    |-- config_1.yaml
    |-- ...
    `-- config_N.yamlDockerfileThe Dockerfile which belongs to the project. For this Dockerfile, container image will be built and test will be executed.build.yamlThe yaml file which has arguments to be used by building image. All arguments of the Dockerfile should be described in this file. for example;args:
  ARG_1: "VALUE_1"
  ARG_2: "VALUE_2"
  ARG_3: "VALUE_3"common.yamlThe yaml file which has parameters to be used by both building image and testing image. for example;image_name: bbrfkr0129/httpd:latest
registry_server: docker.iotests/<yaml config file>The yaml files which has tests to be used by testing image. These tests in the yaml files need to be written as container-structure-test can be processed.TDD method with D4Create project with subcommandinit. By this subcommand, basis directory architecture and sample files are created.$ d4 init tdd_project
$ cd tdd_project
$ tree . --charset=C
.
|-- Dockerfile
|-- build.yaml
|-- common.yaml
`-- tests
    `-- config.yamlSpecify developed image name and registry server pushed developed image incommon.yaml;$ cat <<EOF > common.yaml
image_name: <your dockerhub username>/tdd_image:latest
registry_server: docker.io
EOFWrite mock inDockerfileandbuild.yaml. Implementation is not done yet.$ cat <<EOF > Dockerfile
FROM <base image name>:<tag>
EOF
$ cat <<EOF > build.yaml
args:
EOFWrite test code according to the syntax of container-structure-test intests/config.yaml. In TDD, test code is written before writingDockerfile.Execute test with subcommanddevelop. By this subcommand, mock container image is built and tested. Then tests should be failed.$ d4 developWrite implementation ofDockerfileandbuild.yaml. Inbuild.yaml, The arguments inARGstatements ofDockerfileshould be written;args:
  ARG_1: "VALUE_1"
  ARG_2: "VALUE_2"
  ARG_3: "VALUE_3"Execute build and test with subcommanddevelop. By this subcommand, implementation ofDockerfileis applied, and then generated container image is tested. All tests should be passed.$ d4 developRepeat procedures 4.-7.. until required container image is got.Release got container image to registry server with subcommandrelease. By this subcommand, final build and test will be processed, then pushed to registry server specified incommon.yaml.$ d4 releaseRun container from pushed container image!$ docker run <your dockerhub username>/tdd_image:latestvalid subcommandsd4 initCreate and initialize project.d4 buildOnly build process runs.d4 testOnly test process runs.d4 loginOnly login into registry server specified incommon.yaml.d4 pushOnly push container image specified incommon.yaml.d4 developbuild and test processes run.d4 releasebuild, test, login and push processes run.