Skip to content
A CLI Dockerfile templating tool.
Branch: master
Clone or download
qiushihe Merge pull request #1 from qiushihe/out-dir
Add custom output directory support
Latest commit 4aab971 Jun 23, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Update CircleCI and goreleaser config Jun 11, 2019
builder Add custom output directory support Jun 24, 2019
tests/fixtures Implement docker builder Jun 11, 2019
vendor Add vendor modules Jun 11, 2019
.editorconfig Update README Jun 12, 2019
.gitignore Setup goreleaser Jun 11, 2019
.goreleaser.yml Update CircleCI and goreleaser config Jun 11, 2019
README.md Update README Jun 12, 2019
go.mod Implement docker builder Jun 11, 2019
go.sum Implement docker builder Jun 11, 2019
main.go Implement docker builder Jun 11, 2019

README.md

Docker Builder

Docker Builder is a Dockerfile templating tool. It allows one to turn a Dockerfile.template:

FROM %%BASE_IMAGE%%

%%INSTALL_COMMON_PACKAGES%%

RUN apk add apache2 %%PHP_PACKAGES%%

COPY %%_COMMON/scripts/mount-nfs.sh%% /mount-nfs.sh
COPY src/run-server.sh /run-server.sh
RUN chmod +x /run-server.sh

ENTRYPOINT ["/run-server.sh"]

... into a Dockerfile:

FROM alpine:3.9

RUN apk add bash
RUN apk add shadow
RUN apk add wget

RUN apk add apache2 php7

COPY scripts/mount-nfs.sh%% /mount-nfs.sh
COPY src/run-server.sh /run-server.sh
RUN chmod +x /run-server.sh

ENTRYPOINT ["/run-server.sh"]

Download and Install

Download Docker Builder from the release page.

Put the extracted docker-builder CLI binary somewhere on your PATH, or not since Docker Builder doesn't care where it's being run from nor does it care where all the directories are.

How to Use Docker Builder

To use Docker Builder, first setup a container image directory structure as follow:

  • /path/to/my-image/
    • src/

      • entry.sh

        #!/usr/bin/env bash
        # This is the entry script, so do whatever you want in here.
        echo "Hallo!"
        while true; do sleep 5; done
        
    • Dockerfile.template

      FROM %%BASE_IMAGE%%
      COPY src/entry.sh /entry.sh
      RUN chmod +x /run-server.sh
      ENTRYPOINT ["/run-server.sh"]
      
    • Dockerfile.variables

      BASE_IMAGE: alpine:3.9
      

... then run the docker-builder CLI tool:

$ docker-builder -src /path/to/my-image -- -t my-image

Running the above command would generate a _docker-build directory that looks like:

  • /path/to/my-image/_docker-build
    • src/

      • entry.sh
    • Dockerfile

      FROM alpine:3.9
      COPY src/entry.sh /entry.sh
      RUN chmod +x /run-server.sh
      ENTRYPOINT ["/run-server.sh"]
      

... and then invoke docker build -t my-image to build the image.

Reference Block Values

Unlike one line string values which are stored in Dockerfile.variables, block values are stored in Dockerfile.variables.d directories.

Consider this example:

  • /path/to/my-image/
    • src/

      • entry.sh
    • Dockerfile.template

      FROM %%BASE_IMAGE%%
      %%INSTALL_PACKAGES%%
      COPY src/entry.sh /entry.sh
      RUN chmod +x /run-server.sh
      ENTRYPOINT ["/run-server.sh"]
      
    • Dockerfile.variables

      BASE_IMAGE: alpine:3.9
      
    • Dockerfile.variables.d/

      • INSTALL_PACKAGES

        RUN apk add bash wget \
          zip unzip \
          shadow
        

The above setup would generate this:

  • /path/to/my-image/_docker-build
    • src/

      • entry.sh
    • Dockerfile

      FROM alpine:3.9
      RUN apk add bash wget \
          zip unzip \
          shadow
      COPY src/entry.sh /entry.sh
      RUN chmod +x /run-server.sh
      ENTRYPOINT ["/run-server.sh"]
      

Reference External Values

Other than Dockerfile.variables and Dockerfile.variables.d within the image's directory itself, Docker Builder can also be told to take any number of other directories into consideration.

Consider this example:

  • /path/to/somewhere/else/

    • configs/

      • httpd.conf

        ... some apache2 httpd configuration ...
        
    • Dockerfile.variables

      APACHE_PORTS: 80 443
      
    • Dockerfile.variables.d

      • APACHE_PACKAGES

        RUN apk add apache2 apache2-proxy
        
  • /path/to/my-image/

    • src/

      • entry.sh
    • Dockerfile.template

      FROM %%BASE_IMAGE%%
      %%INSTALL_PACKAGES%%
      %%APACHE_PACKAGES%%
      COPY %%_COMMON/configs/httpd.conf%% /etc/apache2/httpd.conf
      COPY src/entry.sh /entry.sh
      EXPOSE %%APACHE_PORTS%%
      RUN chmod +x /run-server.sh
      ENTRYPOINT ["/run-server.sh"]
      
    • Dockerfile.variables

      BASE_IMAGE: alpine:3.9
      
    • Dockerfile.variables.d/

      • INSTALL_PACKAGES

        RUN apk add bash wget \
          zip unzip \
          shadow
        

Running this command:

$ docker-build -src /path/to/my-image -lib /path/to/somewhere/else -- -t my-image

... would generate:

  • /path/to/my-image/_docker-build
    • src/

      • entry.sh
    • configs/

      • httpd.conf
    • Dockerfile

      FROM alpine:3.9
      RUN apk add bash wget \
          zip unzip \
          shadow
      RUN apk add apache2 apache2-proxy
      COPY configs/httpd.conf /etc/apache2/httpd.conf
      COPY src/entry.sh /entry.sh
      EXPOSE 80 443
      RUN chmod +x /run-server.sh
      ENTRYPOINT ["/run-server.sh"]
      

And you can specify multiple -lib /PATH/TO/LIB options.

You can’t perform that action at this time.