This setup worked for me:
docker-compose.yml
ollama:
...
entrypoint: ["/entrypoint.sh"]
volumes:
- ...
- ./entrypoint.sh:/entrypoint.sh
...
entrypoint.sh
Make sure to run
sudo chmod +x entrypoint.sh
Adapted from @datawookie's script:
#!/bin/bash
# Start Ollama in the background.
/bin/ollama serve &
# Record Process ID.
pid=$!
# Pause for Ollama to start.
sleep 5
echo "Retrieving model (llama3.1)..."
ollama pull llama3.1
echo "Done."
# Wait for Ollama process to finish.
wait $pid
Why this approach?
By pulling the model in the entrypoint script rather than in the Docker image, you avoid large image sizes in your repository, storing the model in a volume instead for better efficiency.