This project automates the generation of optimized Dockerfiles based on the selected programming language. It leverages the Ollama API to provide best-practice Docker configurations with explanatory comments.
Ollama is required for generating Dockerfiles using an AI model. It must be installed before running the script.
curl -fsSL https://ollama.com/install.sh | shbrew install ollamaBefore using Ollama, you need to download the required AI model to generate optimized Dockerfiles.
ollama pull llama3.2:1bA virtual environment ensures that dependencies are installed in an isolated environment, preventing conflicts with system-wide packages.
python3 -m venv venv
source venv/bin/activatepython3 -m venv venv
.\venv\Scripts\activateTo ensure all necessary Python packages are available, install them using the requirements.txt file.
pip3 install -r requirements.txtRun the script to generate a Dockerfile based on the programming language you input.
python3 generate_dockerfile.py- The script prompts the user to enter a programming language (e.g., Python, Node.js, Java).
- It connects to the locally running Ollama API to generate an optimized Dockerfile.
- The API analyzes best practices and outputs a structured Dockerfile.
- The generated Dockerfile includes explanatory comments to help understand its structure.
python3 generate_dockerfile.pyEnter programming language: python
# Generated Dockerfile will be displayed...
- Ensure the Ollama service is running before executing the script.
- Verify that the correct model (llama3.2:1b) is downloaded and available.
- Modify the generated Dockerfile as needed for other programming languages.
This project is open-source. Feel free to modify and use it as needed.
Contributions are welcome! Feel free to submit issues or pull requests to improve the script.
This script takes user input for a programming language and generates an optimized Dockerfile using the Ollama API.
import ollama # Import the Ollama library to interact with the API
# Define a prompt template that tells the AI to generate a Dockerfile with best practices
PROMPT = """
ONLY Generate an ideal Dockerfile for {language} with best practices. Do not provide any description
Include:
- Base image
- Installing dependencies
- Setting working directory
- Adding source code
- Running the application
"""
def generate_dockerfile(language):
"""Generates an optimized Dockerfile for the given programming language."""
response = ollama.chat(
model='llama3.1:8b', # Specifies the AI model to use
messages=[{'role': 'user', 'content': PROMPT.format(language=language)}] # Sends user input to the AI
)
return response['message']['content'] # Returns the generated Dockerfile content
if __name__ == '__main__':
language = input("Enter the programming language: ") # Ask user for the programming language
dockerfile = generate_dockerfile(language) # Generate the Dockerfile
print("\nGenerated Dockerfile:\n")
print(dockerfile) # Print the output- Import Ollama: Loads the API to process user input.
- Define the Prompt: Instructs the AI to generate an optimized Dockerfile.
- Function
generate_dockerfile(language):- Uses Ollama to process the request and return a best-practice Dockerfile.
- Main Execution Block:
- Takes user input for the programming language.
- Calls the function to generate a Dockerfile.
- Displays the generated Dockerfile to the user.
This ensures the script remains simple, effective, and follows best practices.