October 26, 2026
“DSL” Lua for “Dockerfiles” with declarative logic and pre-runtime focus
Aline Diani de Oliveira Batista; Denis Bruno Viríssimo
DOI: 10.22167/2675-6528-202600504
Article derived from a Course Conclusion Work (TCC), with content based on the student’s original work and adapted to the editorial format of the E&S Magazine with the support of the ResumeAI tool, an artificial intelligence solution developed by Instituto Pecege for textual synthesis and organization.
Summary
Dockerfile management presents maintenance challenges and increased image size due to the inclusion of runtime configuration tools. This work investigated the potential of a Lua-based domain-specific language (DSL) for generating Dockerfiles with declarative logic applied in the pre-build stage. The research, of an applied and qualitative nature, compared four distinct approaches: static, parameterized, Lua logic (pre-build), and embedded Python script (post-build), using backend service selection by latency as a test scenario. The results indicated that the Lua approach allowed for the creation of leaner and more deterministic artifacts by removing the need for interpreters within the container. However, it was observed that this strategy transferred configuration intelligence to the build environment, requiring the host system to possess specific connectivity and resources at build time. In contrast to the Python model, which offered flexibility after deployment, the Lua solution proved to be more rigid after image consolidation. It was concluded that anticipating logic to the pre-build stage is a viable alternative for reducing the attack surface and optimizing image size, although its implementation demands an additional learning curve for DevOps teams and depends on the stability of external resources during the build process.
Keywords: Automation; Containers; Image generation; Infrastructure as Code; Declarative logic.
1. Introduction
The use of “containers” has consolidated itself as a fundamental pillar in modern software engineering, with Docker emerging as a widely adopted solution. The relevance of this technology is evidenced by data from the annual Stack Overflow survey (2025), which recorded a significant growth of seventeen percentage points in its utilization between the years 2024 and 2025. This expansion led to an adoption rate of 71.1% among developers, representing the largest increase among all technologies analyzed in the report.
Despite widespread adoption, the creation and maintenance of “Dockerfile” files and associated “Bash” commands still present considerable challenges. This task requires specific technical knowledge, which often increases the complexity of the process and the likelihood of introducing errors (Henkel et al., 2020; Zerouali et al., 2021). Such difficulties can impact the efficiency and security of software development, demanding solutions that simplify and automate these operations.
Additionally, conventional “build” management methods often rely on code that is executed within “containers” in the “post-build” stage. This practice, while functional, leads to an unnecessary increase in image size, as it requires the inclusion of configuration tools in the environment, such as language interpreters, like Python. The presence of these additional components increases the attack surface and compromises the lightness of the artifacts, which is counterproductive in environments where resource optimization is crucial.
In the context of Infrastructure as Code (IaC), tools like Terraform have become established for the declarative provisioning of resources, using languages such as HashiCorp Configuration Language (HCL) (HashiCorp, 2024; Brikman, 2022). However, HCL, being a static declarative language, does not support the arbitrary procedural logic required in more complex Docker image “build” cycles. This includes the inability to perform external API queries, evaluate network conditions, or dynamically select configurations based on environment metrics (Brikman, 2022), limiting the adaptability of these solutions to dynamic scenarios.
To address these limitations, the concept of Domain-Specific Language (DSL) becomes relevant. Fowler (2010) distinguishes two categories: the external DSL, which has its own grammar and parser, and the internal DSL, or “embedded DSL”, which relies on the syntax of a host language to define a restricted vocabulary oriented to a specific domain. The proposal of this work fits into the internal DSL model, seeking to leverage the flexibility of a general-purpose language to solve specific problems in the domain of Docker image construction.
This study proposes the development of a “Dockerfiles” generator engine structured as an internal DSL in Lua. Operating on the language’s “runtime”, this DSL defines a set of domain primitives, such as engine:INIT() and engine:RUN(), which map directly to Docker instructions and compose the generator’s specific vocabulary. The choice of Lua is justified by its embeddable nature, low memory consumption, and ability to execute procedural logic at the “pre-build” stage (Ierusalimschy, 2022; Meng et al., 2021). This allows, for example, dynamic selection of “backend” by latency via ICMP even before the image is generated, eliminating the need for interpreters within the final “container” and ensuring a leaner artifact.
Anticipating the configuration logic to the “pre-build” stage represents an effective strategy to mitigate the mentioned challenges. This approach allows for the creation of leaner and more deterministic artifacts, reducing the attack surface and optimizing image size. By transferring configuration intelligence to the build environment, the solution eliminates unnecessary dependencies from the final image and offers superior flexibility compared to existing static declarative solutions, although it demands a learning curve for DevOps teams and depends on the stability of external resources during the build process.
Given this scenario, the present work aims to demonstrate that anticipating the configuration logic for the “pre-build” stage reduces the complexity of creating Docker artifacts, eliminates unnecessary dependencies from the final image, and offers superior flexibility to existing static declarative solutions.
2. Material and Methods
The research adopted an applied and qualitative approach, focused on the development of a software solution. The method was structured in sequential steps, aiming to evaluate the potential of a Lua-based Domain-Specific Language (DSL) for generating Dockerfiles with declarative logic applied in the pre-build phase. This design allowed for the investigation of the complexity, flexibility, and reliability of the proposed solution compared to traditional approaches.
Initially, a bibliographic survey was conducted on “containers”, “Docker”, “script” languages, and “transpilers”, to theoretically ground the project. Subsequently, a detailed analysis of Lua and Bash languages, Dockerfiles, and pipeline and CI/CD processes was carried out, identifying relevant syntactic and semantic constructs for the conversion and generation of artifacts.
The experimental environment was set up on a local machine with a Windows 11 Operating System. For test execution and solution development, Docker Desktop version 4.65.0, the .NET 9.0 framework (C#), and the NLua version 1.7.8 library were used. The choice of these components aimed to replicate a Docker development and image building environment.
For the solution evaluation, four distinct prototypes were developed. The first consisted of a static Dockerfile, with manual configuration. The second was a parameterized Dockerfile, allowing customization via build and environment variables. The third prototype incorporated an embedded Python script, executed at runtime (post-build), for conditional logic. The fourth prototype was the Dockerfile generator via an internal DSL in Lua, with logic applied in the pre-build phase.
The solution validation was conducted through case studies, in which Lua scripts were used to generate Docker files. These files were tested for their functionality and compliance with Docker tool best practices. The established compliance criterion was the error-free execution of the “docker build” command and the correspondence between the environment variable defined in the generated Dockerfile and the endpoint selected by the Lua logic.
The evaluation of the complexity and maintainability of the artifacts was performed quantitatively, by counting the lines of code (LOC) of the main artifact and the final Dockerfile, in addition to the number of manually configured environment variables. The effort to keep the image compliant and the presence of auxiliary scripts or unnecessary interpreters, which could impact the attack surface and the size of the final image, were verified.
The flexibility and adaptability of the solution were tested for its ability to react to infrastructure changes without altering the application’s source code or the base Dockerfile. The ease of endpoint redirection and the programmatic adaptation of the build environment were analyzed. It was also verified whether the build process resulted in a deterministic artifact, meaning whether the generated Dockerfile would be identical and reproducible under the same input conditions of the Lua logic.
To validate the robustness of the pre-build logic, critical operational scenarios were simulated, using latency as the decision variable. Situations of primary backend unavailability, performance variations between multiple instances, and changes in endpoint priority were tested. The capability of the Lua DSL to inspect the external environment and make logical decisions, such as choosing a fallback or interrupting the build, before consolidating the Docker image, was observed.
The investigation was guided by four research hypotheses. The first hypothesis (H1) proposed that pre-build generation via Lua would reduce operational complexity compared to static Dockerfile, with validation by the reduction of manual steps and auxiliary artifacts. The second (H2) postulated that pre-runtime logic would eliminate the need for auxiliary scripts within the container, with validation by the absence of initialization or configuration scripts in the final image.
The third hypothesis (H3) suggested that the automatic pre-build backend selection would reduce manual configuration failures, with validation by the automated model’s ability to select the backend based on defined criteria, such as latency. The fourth hypothesis (H4) indicated that the approach would improve execution predictability compared to dynamic runtime configurations, with validation if the generated Dockerfile was identical in repeated executions under the same input conditions.
3. Results and Discussion
The analysis of the research results revealed a set of significant advantages associated with the proposed approach, which is based on executing the configuration logic in the pre-build stage. This strategy promoted a clear distinction between infrastructure intelligence and the final image artifact, resulting in leaner components with a reduced attack surface. The elimination of startup scripts within the container directly contributed to decreasing the coupling between the application and the execution environment, conferring greater portability and determinism to the image. Furthermore, the approach demonstrated potential for extension to integration with orchestrators like Kubernetes and for incorporation into CI/CD pipelines, broadening its applicability in continuous delivery contexts.
Operational Complexity and Automation
The assessment of operational complexity and automation was carried out through various indicators, including the number of lines of code (LOC) of the main artifact and the final Dockerfile, cyclomatic complexity, the quantity of dependencies in the final image, the number of manually configured variables, manual interventions per build, the presence of auxiliary scripts and conditional logic, build time, and the final image size. The results obtained corroborated hypothesis H1, which predicted a reduction in operational complexity with the pre-build generation approach. This reduction was measurable, evidenced by the decrease in lines in the final Dockerfile, the elimination of additional dependencies, and the absence of manual interventions in the build process.
Regarding the number of lines of code of the main artifact, the static model presented 16 lines, the parameterized 28 lines, while the Python model with embedded script totaled 52 lines (considering Dockerfile and inline Python). The proposed solution, based on Lua, resulted in a 106-line script, which generated a final Dockerfile of 16 lines. This distribution demonstrates the transfer of logic complexity to the pre-build script, keeping the final artifact lean. In relation to the final delivered Dockerfile, the static model and the Lua generator presented 16 and 17 lines, respectively, contrasting with the 28 lines of the parameterized model and the 93 lines of the Python model, which included the embedded script.
The average cyclomatic complexity also varied significantly between the approaches. The static Dockerfile registered 1.00, the parameterized 2.00, the Python model 2.50 (with an average of two functions), and the Lua generator 4.67 (with an average of three functions). Regarding dependencies in the final image, the static, parameterized, and Lua models included four apt packages, while the Python model required python3, python3-pip, requests, and four apt packages, in addition to an extra interpreter. This reinforces the advantage of the Lua approach in keeping the final image lighter and with a smaller attack surface.
The number of variables manually configured by the operator was a crucial indicator. The static model required one variable (hardcoded BACKEND_URL), the parameterized one required two (ARG and ENV), and the Python one required one (manually defined backends). In contrast, the Lua generator eliminated the need for manual variable configuration, performing it automatically via Lua. Manual interventions per build were also reduced: the static model required no interventions (fixed), the parameterized one required one (mandatory build-arg), and the Python and Lua models demanded no manual interventions, as the decision occurred automatically at runtime or pre-build, respectively.
The presence of auxiliary scripts in the final image was a differential. While the static and parameterized models could have auxiliary scripts, the Python model made them necessary (embedded Python script), and the Lua generator eliminated them completely. Conditional logic, nonexistent in the static model and limited in the parameterized one (via parameters), was complete in both the Python model (if/else, loops at runtime) and the Lua generator (if/else, loops). This data supports hypothesis H2, confirming that the elimination of auxiliary scripts in the final image was consistently observed in the Lua-based model, which reduces coupling between the application and the execution environment.
Regarding build time, the average of five runs indicated 57.32 seconds for the static Dockerfile and for the Lua generator, 50.57 seconds for the parameterized one, and 108.69 seconds for the Python model. The final image size, also averaged over five runs, was 70.83 MB for the static, parameterized, and Lua models, while the Python model resulted in an image of 171.48 MB. This sharp difference in the final image size of the Python model is attributed to the inclusion of the interpreter and additional dependencies, which does not occur in the Lua approach, which executes the logic in the build environment, not in the final container.
Effectiveness of Conditional Logic (Use Case: “Backend” Selection)
In the scenario of latency-based backend selection, the adaptability of different models was crucial. The static model demonstrated no adaptability, always maintaining the same endpoint regardless of external conditions. The parameterized model, although allowing backend switching, required manual intervention at build time, which limited its automation and adaptability to dynamic scenarios. This dependence on human intervention for parameter definition compromises the system’s agility and resilience in infrastructure environments that demand rapid responses to changes.
The Python-based model, with an embedded script for post-build execution, demonstrated automatic decision-making capability, dynamically selecting the backend with lower latency during container runtime. This behavior confirmed the effectiveness of the conditional logic applied at runtime, allowing adaptation to the environment after deployment, without the need for human intervention. However, this flexibility is achieved at the cost of transferring the responsibility of choice to the execution moment, which can introduce variability and unpredictability in the container’s behavior after deployment.
In contrast, the Lua-based solution demonstrated the capacity for automatic and proactive decision-making, dynamically selecting the backend with the lowest latency even before the Dockerfile generation. This behavior confirmed the effectiveness of the conditional logic implemented in the pre-build stage, allowing the final artifact to be already configured for the target environment, without the need for human intervention or runtime adaptation logic. This anticipation of the decision contributes to the predictability and consistency of the generated image, a fundamental aspect in production environments.
The analysis of the effectiveness of the backend selection logic in different scenarios reinforces these observations. When all backends were available, the static model used a fixed backend, the parameterized depended on a manual build-arg, Python automatically selected the one with the lowest latency at runtime, and the Lua generator did the same, but at the pre-build moment. In a scenario of primary backend failure, the static model attempted to use an inactive endpoint and failed at runtime. The parameterized required the operator to manually change the build-arg, while Python and Lua offered automatic fallback, Python at runtime and Lua at pre-build, without the need for a rebuild.
For latency variations between instances, the static model showed no adaptation, and the parameterized one lacked automatic adaptation. Python performed dynamic selection of the lowest latency instance at each runtime, while Lua dynamically selected the lowest latency instance at the pre-build moment. In terms of avoided incorrect configurations, the static (fixed) model was not measurable, the parameterized one depended on the operator, but Python and Lua achieved 100% of objective decisions based on latency, Python at runtime and Lua at pre-build. The human intervention required was “yes” for static (file editing) and parameterized (–build-arg), and “no” for Python and Lua. The results obtained confirm hypothesis H3, demonstrating the proposed approach’s capability for automated decision-making without human intervention, autonomously selecting the backend still in the build phase.
Predictability and Determinism
To evaluate the reproducibility of the artifacts, an automated experimental procedure was employed, consisting of multiple controlled executions of the build process. The experiment involved performing five independent runs for each evaluated model, keeping all input conditions constant, including build parameters and the execution environment. In each iteration, the Docker build engine’s cache was explicitly invalidated, ensuring that subsequent runs were not influenced by residual states and that each build was independent of previously constructed layers.
After the five executions, the results were aggregated and analyzed for variability among the generated artifacts, allowing inference of the degree of determinism of each approach. Reproducibility was evaluated by the consistency of observed results under controlled conditions. In terms of artifact determinism, the static Dockerfile was 100% identical (trivial, by fixed definition). The parameterized one was not deterministic. The Python model resulted in 100% identical artifacts, but with non-deterministic behavior at runtime. In contrast, the Lua generator produced 100% identical artifacts and with deterministic logical behavior under fixed input conditions, which confirmed hypothesis H4.
The results presented support hypothesis H4 under the experimental conditions of this study, as the Lua Generator produced structurally identical artifacts when subjected to the same input conditions. This behavior highlights the deterministic nature of the pre-build approach, in contrast to the post-build model, where variability is shifted to runtime. The predictability of the image generated by the Lua engine is a significant advantage, as it ensures that, given the same inputs, the output will always be the same, facilitating debugging and maintenance in production environments.
Consolidation
The consolidated comparison between the four experimental models developed, considering aspects related to automation, flexibility, operational complexity, and environmental adaptability, revealed a progressive evolution. A transition was observed from the static model to the solution based on declarative logic in Lua, characterized by reduced dependence on human intervention and increased automated decision-making capacity in the build process. This progression demonstrates the effectiveness of anticipating configuration logic for the pre-build stage as a means of optimizing Docker image creation.
In terms of LOC of the main artifact, the static model had 17 lines, the parameterized 31, the Python 93 (Dockerfile plus embedded script) and the Lua 106 (script). For the final delivered Dockerfile, static and Lua presented 17 lines, while the parameterized had 31 and the Python 93. The number of manually configured variables was 1 for static and Python, 2 for parameterized and 0 for Lua. Manual interventions per build were 0 for static, Python and Lua, and 1 (mandatory) for parameterized. Auxiliary scripts in the image were possible for static and parameterized, necessary for Python and eliminated for Lua. Adaptation to backend failures was nonexistent (runtime failure) for static, manual for parameterized, automatic at runtime for Python and automatic (with fallback) for Lua. The determinism of the artifact was 100% trivial for static, non-deterministic for parameterized, 100% in the artifact but non-deterministic in behavior for Python, and 100% logical for Lua.
Critical Discussion and Limitations
The results obtained indicate that the approach based on generating Dockerfiles through Lua scripts in the pre-build stage provided significant improvements in process automation, reduction of operational complexity, and elimination of the need for auxiliary scripts in the execution environment. Compared to the static and parameterized models, it was found that the configuration logic could be externalized and centralized, allowing greater control over the image building process and reducing manual intervention. This externalization of logic is a key point for system maintenance and scalability.
Compared to the post-build Python model, the Lua approach demonstrated a specific advantage in eliminating dependencies within the container. While Python requires the inclusion of the interpreter in the image, Lua operates exclusively on the build host, resulting in leaner artifacts with a smaller attack surface for vulnerabilities. Additionally, the use of conditional logic enabled the adaptation of the artifact during the build phase, as exemplified by the selection of the backend based on predefined criteria. The evidence observed throughout the experiments indicates that hypotheses H1, H2, and H3 were consistently supported by the empirical data.
The H4 hypothesis was validated within the adopted experimental scope, even without formal verification by hash mechanisms. However, some critical points deserve attention. The dependency on the build environment implies that the execution of pre-build logic is conditioned on the availability of the resources used, such as connectivity to endpoints in the case of latency evaluation. This dependency can be a limitation in environments with restricted or unstable connectivity. Nevertheless, the flexibility of the DSL in Lua allows this logic to be adapted to more restrictive contexts, such as CI/CD pipelines, where validation can be based on local criteria, such as file integrity or dependency analysis, partially mitigating this dependency.
Furthermore, the introduction of an additional scripting layer prior to Docker may represent an initial learning curve for DevOps teams. This entry barrier, although surmountable, should be considered in adoption planning. Finally, the limitations of the experiments must be considered: the use of ICMP (ping) as a latency metric simplifies the problem, while real-world scenarios may require more complex criteria, such as API availability via HTTP or server load. The impacts related to the storage of multiple images generated for different execution contexts were also not evaluated, which can be a relevant factor in large-scale environments.
In summary, transferring the configuration logic from runtime to build time represents an effective strategy for obtaining leaner, more predictable images with less coupling to the execution environment. In this context, the use of a higher-level language in the pre-build stage, such as the internal DSL in Lua, has proven to be a viable alternative within Infrastructure as Code practices. The proposed approach, by anticipating infrastructure decisions, preserves the simplicity and predictability of the resulting Dockerfile, responding to the objective of reducing complexity, eliminating unnecessary dependencies, and offering superior flexibility to existing static declarative solutions.
4. Conclusion
This study investigated the potential of anticipating configuration logic for the pre-build stage, aiming to reduce complexity in Docker artifact creation, eliminate unnecessary dependencies from the final image, and offer superior flexibility to static declarative solutions. It was found that the approach based on a domain-specific language (DSL) in Lua, executed in the pre-build step, allowed for the generation of leaner and more deterministic Dockerfiles. This strategy eliminated the need for interpreters and auxiliary scripts within the container, which contributed to reducing the attack surface and optimizing image size. Additionally, the capability for automated decision-making, such as dynamic selection of backend services by latency, was observed during the build phase itself, without human intervention. Transferring configuration intelligence to the build environment proved to be a viable and effective alternative for obtaining more predictable artifacts with less coupling to the execution environment, consolidating the Lua DSL as a relevant technical contribution to Infrastructure as Code practices.
However, the implementation of this strategy revealed some limitations. The dependency on the build environment for the execution of pre-build logic requires the host system to have specific connectivity and resources at the time of construction, which can be a challenge in restricted or unstable environments. Furthermore, the introduction of a new scripting layer may demand a learning curve for DevOps teams. For future studies, formal validation of artifact reproducibility through hash comparison, the adoption of more robust metrics for backend evaluation, and the analysis of the scalability impacts of the approach in orchestration and cloud environments are recommended.
Bibliographic References
Brikman, Y. 2022. Terraform: Up & Running. 3ed. O’Reilly Media, Sebastopol, CA, EUA.
Fowler, M. 2010. Domain-Specific Languages. 1ed. Addison-Wesley, Boston, MA, EUA.
HashiCorp. 2024. Terraform Documentation. Disponível em: <https://developer.hashicorp.com/terraform>. Acesso em: [data].
Henkel, J.; Bird, C.; Lahiri, S. K.; Reps, T. 2020 A Dataset of Dockerfile. In: 17th International Conference on Mining Software Repositories, 2020, Seoul, Coréia do Sul. Anais… p. 528-532.
Ierusalimschy, R. 2022. Programming in Lua. 5ed. PUC-Rio, Rio de Janeiro, RJ, Brasil.
Meng et al., 2021 [Referência completa não encontrada no documento original]
Stack Overflow. 2025. Developer Survey: Technology – Cloud development. Disponível em: <https://survey.stackoverflow.co/2025/technology#1-cloud-development>. Acesso em: 15 fev. 2026.
Zerouali, Ahmed, et al. “On the relation between outdated docker containers, severity vulnerabilities, and bugs.” 2019 ieee 26th international conference on software analysis, evolution and reengineering (saner). IEEE, 2019.
Article originating from the Final Course Work of the Specialization in Software Engineering of the MBA USP/Esalq
To learn more about the course, click here and access the MBX Academy platform