How to Create Dockerfiles with GenAI
December 8, 2025 · 748 words · 4 min
As we learn how to work more effectively with generative AI, one of the tasks we return to is Docker
As we learn how to work more effectively with generative AI, one of the tasks we return to is Dockerfile generation. Because we can equip our AI assistants with tools, does this allow us to reframe the problem of content generation as an interaction between LLMs and tools? If you ask ChatGPT to write a for your project, it will initiate a conversation with you and ask you to extract some details from your project (Figure 1). However, we can also equip our assistant with a tool to extract this information from the project directly. Instead of asking the user, an assistant can ask for a tool instead. Here’s an alternative framing. Let’s prompt the assistant with two high-level instructions whenever it is asked to create a Dockerfile. In addition to these new prompts, we will also supply the LLM with two function definitions. With these two functions, the assistant will have enough agency that it can write a Dockerfile directly to a user’s project. Unfortunately, without best practices, the actual Dockerfile content is never very good. Here’s an example of the kind of Dockerfile that gets generated when the assistant knows the kind of project but not much else. This example shows several standard Dockerfile authoring problems. So, what can we do about this? Let’s extract some best practices for authoring NPM project Dockerfiles. We’ll include this content in any conversation occurring in the context of an NPM project. We’ll also add a third function. At the time of writing, here’s the output from our assistant. In this example, the assistant is using the GPT-4 model, but we are seeing the same results with other local models. This is a big improvement. The three inputs that underlie this improvement are listed below. Distilling best practices is certainly not easy, but a little can go a long way. Still, it is still hard to know whether this can be considered an improvement over just using code templates. Our continuing investigation centers around whether generative AI will help us to more easily adapt these models to new knowledge, new features, and new project types. It’s instructive to go through the actual messages passed between agents here. It’s particularly interesting to look at the interplay between the Assistant and the Tool agents. Near the end of the exchange, there is an unexpected twist where the tool complains that it was passed an invalid JSON file. In this case, the assistant actually did generate invalid JSON and the tool was right to complain. However, the assistant also responded to this error by fixing the JSON and calling the tool a second time! That was . Another interesting example of an unscripted moment is the last message from the assistant. The assistant correctly determines that the task is now complete and generates a summary of the work that was done and why. You are an assistant who specializes in authoring Dockerfiles for projects. Since you are an expert and know about their project, be definitive about recommendations. This is an NPM project. Write Dockerfiles for NPM projects using three stages. Do these three steps sequentially … … … (system prompts excluded for brevity) .. … 22-slim function call failed call exited with non-zero code (1): call exited with non-zero code (1): #error {\r\n :cause Unexpected character (‘`’) I apologize for the error. Let me try that again. wrote 1 files: Dockerfile I have written the Dockerfile for your NPM project. You can find it in your project folder. This Dockerfile uses a multi-stage build process to efficiently build and run your application within a Docker container. The first stage fetches the runtime dependencies, the second stage builds the application, and the final stage runs the application. Please let me know if you need further assistance. For a generative AI assistant to improve its ability to generate Dockerfile content, we have identified both tools and stack-specific best practice content as key enablers. Incorporating these elements into a generative AI based assistant can serve as an alternative to code templates. We continue to evaluate whether this approach allows us to more easily adapt to new project types. As always, feel free to follow along in our and reach out. Everything we’ve discussed in this blog post is available for you to try on your own projects. For more on what we’re doing at Docker,