百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

dockerfile-generator

> DevOps
开源

dfg - 根据各种输入通道生成 dockerfile。

187 stars0 点赞5 次浏览
访问官网GitHub

工具介绍

dfg - 根据各种输入通道生成 dockerfile。

dfg - Dockerfile Generator

dfg is both a go library and an executable that produces valid Dockerfiles using various input channels.

Table of Contents

  • Overview
  • Installation
    • Installing as an Executable
    • Installing as a Library
  • Getting Started
    • Using dfg as an Executable
    • Using dfg as a Library
  • Examples
    • YAML File Per Dockerfile Example
    • YAML File Target Field Example
    • Library Usage Example
  • TODO

Overview

dfg is a Dockerfile generator that accepts input data from various sources, produces and redirects the generated Dockerfile to an output target such as a file or stdout.

It is especially useful for generating Dockerfile instructions conditionally since Dockerfile language has no control flow logic.

Installation

Installing as an Executable

  • MacOS
bash
curl -o dfg -L https://github.com/ozankasikci/dockerfile-generator/releases/download/v1.0.0/dfg_v1.0.0_darwin_amd64
chmod +x dfg && sudo mv dfg /usr/local/bin
  • Linux
bash
curl -o dfg -L https://github.com/ozankasikci/dockerfile-generator/releases/download/v1.0.0/dfg_v1.0.0_linux_amd64
chmod +x dfg && sudo mv dfg /usr/local/bin
  • Windows
bash
curl -o dfg.exe -L https://github.com/ozankasikci/dockerfile-generator/releases/download/v1.0.0/dfg_v1.0.0_windows_amd64.exe

Installing as a Library

go get -u github.com/ozankasikci/dockerfile-generator

Getting Started

Using dfg as an Executable

Available commands:

dfg generate --input path/to/yaml --out Dockerfile generates a file named Dockerfile

dfg generate --input path/to/yaml --target-field ".server.dockerfile" --out Dockerfile generates a file named Dockerfile reading the .server.dockerfile field of the YAML file.

dfg generate --help lists available flags

Using dfg as a Library

When using dfg as a go library, you need to pass a []dfg.Stage slice as data. This approach enables and encourages multi staged Dockerfiles. Dockerfile instructions will be generated in the same order as in the []dfg.Instruction slice.

Some Instructions accept a runForm field which specifies if the Instruction should be run in the shell form or the exec form. If the runForm is not specified, it will be chosen based on Dockerfile best practices.

For detailed usage example please see Library Usage Example

Examples

Single YAML File per Dockerfile Example (Expects a stages key on top level)

yaml
stages:
  final:
    - from:
        image: kstaken/apache2
    - run:
        runForm: shell
        params:
          - apt-get update &&
          - apt-get install -y
          - php5
          - libapache2-mod-php5 &&
          - apt-get clean &&
          - rm -rf /var/lib/apt/lists/*
    - cmd:
        params:
          - /usr/sbin/apache2
          - -D
          - FOREGROUND

Use dfg as binary:

bash
dfg generate -i ./example-input-files/apache-php.yaml --stdout

Or as a library

go
data, err := dfg.NewDockerFileDataFromYamlFile("./example-input-files/apache-php.yaml")
tmpl := dfg.NewDockerfileTemplate(data)
err = tmpl.Render(output)

Output

dockerfile
FROM kstaken/apache2
RUN apt-get update && apt-get install -y php5 libapache2-mod-php5 && apt-get clean && rm -rf /var/lib/apt/lists/*
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

YAML File Example With Target Field (Allows using any field)

yaml
someConfig:
  key: value
serverConfig:
  dockerfile:
    stages:
      final:
        - from:
            image: kstaken/apache2
        - run:
            runForm: shell
            params:
              - apt-get update &&
              - apt-get clean &&
              - rm -rf /var/lib/apt/lists/*

Use dfg as binary:

bash
dfg generate -i ./example-input-files/test-input-with-target-key-6.yaml --target-field ".serverConfig.dockerfile" --stdout

Or as a library

go
data, err := dfg.NewDockerFileDataFromYamlField("./example-input-files/test-input-with-target-key-6.yaml", ".serverConfig.dockerfile")
tmpl := dfg.NewDockerfileTemplate(data)
err = tmpl.Render(output)

Output

dockerfile
FROM kstaken/apache2
RUN apt-get update && apt-get clean && rm -rf /var/lib/apt/lists/*

Library Usage Example

…

Output

dockerfile
FROM golang:1.7.3 as builder
USER ozan
WORKDIR /go/src/github.com/ozankasikci/dockerfile-generator/
RUN go get -d -v golang.org/x/net/html
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest as final
RUN apk --no-cache add ca-certificates
USER root:admin
WORKDIR /root/
COPY --from=builder /go/src/github.com/ozankasikci/dockerfile-generator/app .
CMD ["./app"]

TODO

  • Add reading Dockerfile data from an existing yaml file support
  • Implement json file input channel
  • Implement stdin input channel
  • Implement toml file input channel

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Godfgdockerdockerfiledockerfile-generator

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理