DEV Community

Samarth Funde
Samarth Funde

Posted on

AI_Assisted_Devops_Day-3_Project-1_Local_LLM

Generate_Dockerfile_using_Ollama_Local_LLM project-1

Step A) Setup ollama in your local EC2 server

Step 1) Create EC2 instance with enough Storage and Ram instance i have used (t3.medium) and ubuntu OS

Step 2) go to ollama.com website search llama3 and select the version llama3.2

Step 3) you can see in website Download button click on it and you see there Linux so copy following commands and paste in your instace
a) Download & Install Ollama for linux:
curl -fsSL https://ollama.com/install.sh | sh
b) Start ollama service: ollama serve or start &
c) Run the model: ollama run llama3.2:1b
<<.......... now you can write here your query e.g 'Create a Dockerfile based on Java application'

.......................................................................

Step B) Setup Python Virtual Environment in EC2 instance

Step 4) Create Virtual Environment copy & paste the following cmds
sudo apt update
sudo apt install python3 python3-venv python3-pip -y
python3 -m venv venv
source venv/bin/activate # On Linux/MacOS

........................................................................

Step C) Create a Python files for run the Local-LLM

Step 5) echo "ollama" > requirement.py
install dependancies:
pip3 install -r requirement.txt

Step 7) nano generate_dockerfile.py ...... and copy paste code

import ollama
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
  • Multi stage docker file """

def generate_dockerfile(language):
response = ollama.chat(
model='llama3.2:1b',
messages=[{'role': 'user', 'content': PROMPT.format(language=language)}]
)
return response['message']['content']

if name == 'main':
language = input("Enter the programming language: ")
dockerfile = generate_dockerfile(language)
print("\nGenerated Dockerfile:\n")
print(dockerfile)

Run the Application:
python3 generate_dockerfile.py

Step 8) after run above file you can see output 'Enter the programming language: java' so write as per your required language eg, java, groovy, python,nodejs etc.. it will generate the Dockerfile

Step 9) 'Enter the programming language: groovy'

Top comments (0)