Bash Functions To Make Ollama Slightly Easier To Run In Docker

Published:

I'm not gonna get into the LLM/"AI" debate right now. I'm not a fan of it but also not a purist and for a variety of reasons I've had to learn a little about it. Fortunately, I have yet to actually need to use cloud-based models and have been able to use local ones just fine. Running models locally doesn't get around all the ethical implications but it goes a long way, in my estimation. To that end, I've found Ollama to be the easiest way to get up and running. I like the idea of not having this natively installed, especially since the only option is to manually install the binary (or curl | sh it :/). Fortunately, Ollama publishes a container image. This adds a little complexity in interacting with it so that's where this function comes into play. I have this in my ~/.bashrc file.

The gist is that this "adds" two ollama commands: start and stop. All other commands and parameters get passed through to the container's ollama executable.

ollama() {
  case "${1}" in
    start)
      docker run \
        --detach \
        --name ollama \
        --publish 11434:11434 \
        --rm \
        --volume ollama:/root/.ollama \
        ollama/ollama
      ;;
    stop)
      docker stop ollama
      ;;
    *)
      docker exec --interactive --tty ollama ollama ${*}
      ;;
  esac
}

Using

$ ollama start
d4f833e6e842a34e119ab99050288e4cedc315a2e865263f9025f529de9d5aab

$ ollama pull lfm2
pulling manifest
pulling d55ab0bfb1b8: 100% ▕████████████████████████████████████████████▏  14 GB
pulling 2453818a9efb: 100% ▕████████████████████████████████████████████▏  10 KB
pulling 2dd8b8f832ae: 100% ▕████████████████████████████████████████████▏   20 B
pulling 89c7786f56b2: 100% ▕████████████████████████████████████████████▏  472 B
verifying sha256 digest
writing manifest
success

$ ollama run lfm2 "Why is the sky blue? Keep it to one sentence."
The sky appears blue because molecules in Earth's atmosphere scatter shorter
wavelengths of sunlight (like blue) more than longer wavelengths (like red),
a phenomenon called Rayleigh scattering.

$ ollama run lfm2 -- "Please check the following article for grammar and spelling
and make suggestions.

$(cat content/articles/2026-04-15-bash-functions-to-make-ollama-slightly-easier-to-run-in-docker/index.md)
"
Here’s a revised version of your article with grammar, spelling, and clarity
improvements, along with suggestions:
...

$ ollama stop
ollama

Caveats

If you want to pipe text into ollama run, for example, the --tty argument will likely cause you problems.