[GH-ISSUE #7184] create minimal cpu-only smaller docker image #51075

Closed
opened 2026-04-28 18:15:41 -05:00 by GiteaMirror · 15 comments
Owner

Originally created by @ozbillwang on GitHub (Oct 12, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/7184

Originally assigned to: @dhiltgen on GitHub.

The image ollama/ollama is 4.87GB already, which I plan to run it on my macbook or Ubuntu (linux) without any GPUs.

ollama/ollama    latest    e458178cf2c1   2 weeks ago         4.87GB

Are there any ways to reduce the size to as small as possible, since I don't need care of GPU, CUBA driver, etc

Originally created by @ozbillwang on GitHub (Oct 12, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/7184 Originally assigned to: @dhiltgen on GitHub. The image `ollama/ollama` is 4.87GB already, which I plan to run it on my macbook or Ubuntu (linux) without any GPUs. ``` ollama/ollama latest e458178cf2c1 2 weeks ago 4.87GB ``` Are there any ways to reduce the size to as small as possible, since I don't need care of GPU, CUBA driver, etc
GiteaMirror added the dockerfeature request labels 2026-04-28 18:15:42 -05:00
Author
Owner

@rick-github commented on GitHub (Oct 12, 2024):

You can build a custom version with scripts/build_docker.sh, controlled by environment variables, see scripts/env.sh. For example:

$ VERSION=cpu PLATFORM=linux/amd64 OLLAMA_SKIP_CUDA_GENERATE=1 scripts/build_docker.sh
<!-- gh-comment-id:2408566571 --> @rick-github commented on GitHub (Oct 12, 2024): You can build a custom version with [`scripts/build_docker.sh`](https://github.com/ollama/ollama/blob/main/scripts/build_docker.sh), controlled by environment variables, see [`scripts/env.sh`](https://github.com/ollama/ollama/blob/main/scripts/env.sh). For example: ``` $ VERSION=cpu PLATFORM=linux/amd64 OLLAMA_SKIP_CUDA_GENERATE=1 scripts/build_docker.sh ```
Author
Owner

@ozbillwang commented on GitHub (Oct 13, 2024):

Thanks for your suggestion.

You can build a custom version with scripts/build_docker.sh, controlled by environment variables, see scripts/env.sh. For example:

$ VERSION=cpu PLATFORM=linux/amd64 OLLAMA_SKIP_CUDA_GENERATE=1 scripts/build_docker.sh

I finally built the image successfully with your command, but it took about 8 hours on my MacBook. The size isn't significantly different, still around 4.87 GB.

docker images
REPOSITORY      TAG               IMAGE ID       CREATED             SIZE
ollama/ollama   cpu-rocm          86087abfe5b8   3 minutes ago       9.71GB
ollama/ollama   cpu               b450966abbae   About an hour ago   4.87GB
<!-- gh-comment-id:2408980932 --> @ozbillwang commented on GitHub (Oct 13, 2024): Thanks for your suggestion. > You can build a custom version with [`scripts/build_docker.sh`](https://github.com/ollama/ollama/blob/main/scripts/build_docker.sh), controlled by environment variables, see [`scripts/env.sh`](https://github.com/ollama/ollama/blob/main/scripts/env.sh). For example: > > ``` > $ VERSION=cpu PLATFORM=linux/amd64 OLLAMA_SKIP_CUDA_GENERATE=1 scripts/build_docker.sh > ``` I finally built the image successfully with your command, but it took about 8 hours on my MacBook. The size isn't significantly different, still around 4.87 GB. ``` docker images REPOSITORY TAG IMAGE ID CREATED SIZE ollama/ollama cpu-rocm 86087abfe5b8 3 minutes ago 9.71GB ollama/ollama cpu b450966abbae About an hour ago 4.87GB ```
Author
Owner

@rick-github commented on GitHub (Oct 13, 2024):

What command did you use?

<!-- gh-comment-id:2408981550 --> @rick-github commented on GitHub (Oct 13, 2024): What command did you use?
Author
Owner

@dhiltgen commented on GitHub (Oct 13, 2024):

After we finish the transition to the new Go server, we may be able to tweak the Dockerfile to make this a bit easier to generate locally with something like docker build --target cpu-only -t ollama .

<!-- gh-comment-id:2409060595 --> @dhiltgen commented on GitHub (Oct 13, 2024): After we finish the transition to the new Go server, we may be able to tweak the Dockerfile to make this a bit easier to generate locally with something like `docker build --target cpu-only -t ollama .`
Author
Owner

@ozbillwang commented on GitHub (Oct 13, 2024):

What command did you use?

Same command you provide:

VERSION=cpu PLATFORM=linux/amd64 OLLAMA_SKIP_CUDA_GENERATE=1 scripts/build_docker.sh

And can I skip to build the image with tag cpu-rocm, seems it takes another hour get all jobs done.

from the logs:

ollama/ollama   cpu-rocm          86087abfe5b8   3 minutes ago       9.71GB

The local ollama/ollama:cpu has been built one hour ago

ollama/ollama   cpu               b450966abbae   About an hour ago   4.87GB
<!-- gh-comment-id:2409295023 --> @ozbillwang commented on GitHub (Oct 13, 2024): > What command did you use? Same command you provide: ``` VERSION=cpu PLATFORM=linux/amd64 OLLAMA_SKIP_CUDA_GENERATE=1 scripts/build_docker.sh ``` And can I skip to build the image with tag `cpu-rocm`, seems it takes another hour get all jobs done. from the logs: ``` ollama/ollama cpu-rocm 86087abfe5b8 3 minutes ago 9.71GB ``` The local `ollama/ollama:cpu` has been built one hour ago ``` ollama/ollama cpu b450966abbae About an hour ago 4.87GB ```
Author
Owner

@ozbillwang commented on GitHub (Oct 13, 2024):

After we finish the transition to the new Go server, we may be able to tweak the Dockerfile to make this a bit easier to generate locally with something like docker build --target cpu-only -t ollama .

That would be great, and helpful

What I need is a lightweight Ollama Docker image that can run easily and quickly on Mac or Linux. My goal is to be able to deploy it anywhere, if possible, for use in my projects.

Target size would be less than 500MB if possible (Would build in Alpine , but that's second stage)

<!-- gh-comment-id:2409312973 --> @ozbillwang commented on GitHub (Oct 13, 2024): > After we finish the transition to the new Go server, we may be able to tweak the Dockerfile to make this a bit easier to generate locally with something like `docker build --target cpu-only -t ollama .` That would be great, and helpful What I need is a lightweight Ollama Docker image that can run easily and quickly on Mac or Linux. My goal is to be able to deploy it anywhere, if possible, for use in my projects. Target size would be less than 500MB if possible (Would build in Alpine , but that's second stage)
Author
Owner

@ozbillwang commented on GitHub (Oct 15, 2024):

@rick-github @dhiltgen

Should I spend time on this now or waiting for new Go server ready?

Are there any ways to get a lightweight version now?

<!-- gh-comment-id:2412691880 --> @ozbillwang commented on GitHub (Oct 15, 2024): @rick-github @dhiltgen Should I spend time on this now or waiting for new Go server ready? Are there any ways to get a lightweight version now?
Author
Owner

@kth8 commented on GitHub (Oct 15, 2024):

Here is a minimal Dockerfile that uses a 15MB base image then just add the Ollama binary.

FROM cgr.dev/chainguard/wolfi-base
RUN apk add --no-cache libstdc++ wget
RUN sh -c 'wget -O - https://ollama.com/download/ollama-linux-amd64.tgz | tar -xz --strip 2 ./bin/ollama'
ENV OLLAMA_HOST=0.0.0.0
EXPOSE 11434
CMD ["/ollama", "serve"]
<!-- gh-comment-id:2413506472 --> @kth8 commented on GitHub (Oct 15, 2024): Here is a minimal Dockerfile that uses a 15MB base image then just add the Ollama binary. ``` FROM cgr.dev/chainguard/wolfi-base RUN apk add --no-cache libstdc++ wget RUN sh -c 'wget -O - https://ollama.com/download/ollama-linux-amd64.tgz | tar -xz --strip 2 ./bin/ollama' ENV OLLAMA_HOST=0.0.0.0 EXPOSE 11434 CMD ["/ollama", "serve"] ```
Author
Owner

@ozbillwang commented on GitHub (Oct 15, 2024):

Here is a minimal Dockerfile that uses a 15MB base image then just add the Ollama binary.

FROM cgr.dev/chainguard/wolfi-base
RUN apk add --no-cache libstdc++ wget
RUN sh -c 'wget -O - https://ollama.com/download/ollama-linux-amd64.tgz | tar -xz --strip 2 ./bin/ollama'
ENV OLLAMA_HOST=0.0.0.0
EXPOSE 11434
CMD ["/ollama", "serve"]

This is now smaller. Let’s consider if there’s any room to make it even smaller. Example, no need build with GPU drivers.

ollama   latest 7b8b0a787d6d   About a minute ago   2.15GB
<!-- gh-comment-id:2413766364 --> @ozbillwang commented on GitHub (Oct 15, 2024): > Here is a minimal Dockerfile that uses a 15MB base image then just add the Ollama binary. > > ``` > FROM cgr.dev/chainguard/wolfi-base > RUN apk add --no-cache libstdc++ wget > RUN sh -c 'wget -O - https://ollama.com/download/ollama-linux-amd64.tgz | tar -xz --strip 2 ./bin/ollama' > ENV OLLAMA_HOST=0.0.0.0 > EXPOSE 11434 > CMD ["/ollama", "serve"] > ``` This is now smaller. Let’s consider if there’s any room to make it even smaller. Example, no need build with GPU drivers. ``` ollama latest 7b8b0a787d6d About a minute ago 2.15GB ```
Author
Owner

@kth8 commented on GitHub (Oct 15, 2024):

FROM ollama/ollama AS ollama
FROM cgr.dev/chainguard/wolfi-base
RUN apk add --no-cache libstdc++
COPY --from=ollama /usr/bin/ollama /usr/bin/ollama
COPY --from=ollama /usr/lib/ollama/runners/cpu /usr/lib/ollama/runners/cpu
COPY --from=ollama /usr/lib/ollama/runners/cpu_avx /usr/lib/ollama/runners/cpu_avx
COPY --from=ollama /usr/lib/ollama/runners/cpu_avx2 /usr/lib/ollama/runners/cpu_avx2
ENV OLLAMA_HOST=0.0.0.0
EXPOSE 11434/tcp
CMD ["ollama", "serve"]
REPOSITORY                              TAG           IMAGE ID       CREATED              SIZE
ollama-cpu                              latest        af01a2130bf0   25 seconds ago       58.8MB
<!-- gh-comment-id:2414823298 --> @kth8 commented on GitHub (Oct 15, 2024): ``` FROM ollama/ollama AS ollama FROM cgr.dev/chainguard/wolfi-base RUN apk add --no-cache libstdc++ COPY --from=ollama /usr/bin/ollama /usr/bin/ollama COPY --from=ollama /usr/lib/ollama/runners/cpu /usr/lib/ollama/runners/cpu COPY --from=ollama /usr/lib/ollama/runners/cpu_avx /usr/lib/ollama/runners/cpu_avx COPY --from=ollama /usr/lib/ollama/runners/cpu_avx2 /usr/lib/ollama/runners/cpu_avx2 ENV OLLAMA_HOST=0.0.0.0 EXPOSE 11434/tcp CMD ["ollama", "serve"] ``` ``` REPOSITORY TAG IMAGE ID CREATED SIZE ollama-cpu latest af01a2130bf0 25 seconds ago 58.8MB ```
Author
Owner

@ozbillwang commented on GitHub (Oct 15, 2024):

That's unbelievable, thank you! I'll run some tests to confirm everything is working properly.

<!-- gh-comment-id:2415220935 --> @ozbillwang commented on GitHub (Oct 15, 2024): That's unbelievable, thank you! I'll run some tests to confirm everything is working properly.
Author
Owner

@ozbillwang commented on GitHub (Oct 16, 2024):

arch amd64, it works fine

One issue found. Below folders doesn't exist in ollama/ollama ARM64 image

COPY --from=ollama /usr/lib/ollama/runners/cpu_avx /usr/lib/ollama/runners/cpu_avx
COPY --from=ollama /usr/lib/ollama/runners/cpu_avx2 /usr/lib/ollama/runners/cpu_avx2

But I can't replace it with

COPY --from=ollama /usr/lib/ollama/runners/cpu* /usr/lib/ollama/runners/

this code doesn't work, I don't get folders, but files are copied into /usr/lib/ollama/runners/ directly

/usr/lib/ollama/runners # pwd
/usr/lib/ollama/runners
/usr/lib/ollama/runners # ls -l
total 4620
-rwxr-xr-x    1 root     root        837952 Oct 10 22:22 libggml.so
-rwxr-xr-x    1 root     root       2089576 Oct 10 22:22 libllama.so
-rwxr-xr-x    1 root     root       1794136 Oct 10 22:22 ollama_llama_server
/usr/lib/ollama/runners #

then I can't build with buildx to support multiple arch image

So what's cpu_avx and cpu_avx2 in amd64?

<!-- gh-comment-id:2415483343 --> @ozbillwang commented on GitHub (Oct 16, 2024): arch amd64, it works fine One issue found. Below folders doesn't exist in `ollama/ollama` ARM64 image ``` COPY --from=ollama /usr/lib/ollama/runners/cpu_avx /usr/lib/ollama/runners/cpu_avx COPY --from=ollama /usr/lib/ollama/runners/cpu_avx2 /usr/lib/ollama/runners/cpu_avx2 ``` But I can't replace it with ``` COPY --from=ollama /usr/lib/ollama/runners/cpu* /usr/lib/ollama/runners/ ``` this code doesn't work, I don't get folders, but files are copied into /usr/lib/ollama/runners/ directly ``` /usr/lib/ollama/runners # pwd /usr/lib/ollama/runners /usr/lib/ollama/runners # ls -l total 4620 -rwxr-xr-x 1 root root 837952 Oct 10 22:22 libggml.so -rwxr-xr-x 1 root root 2089576 Oct 10 22:22 libllama.so -rwxr-xr-x 1 root root 1794136 Oct 10 22:22 ollama_llama_server /usr/lib/ollama/runners # ``` then I can't build with `buildx` to support multiple arch image So what's `cpu_avx` and `cpu_avx2` in amd64?
Author
Owner

@ozbillwang commented on GitHub (Oct 16, 2024):

seems avx/avx2 are not must-have libraries.

https://github.com/ollama/ollama/issues/2903

https://github.com/ollama/ollama/issues/3891

<!-- gh-comment-id:2415518114 --> @ozbillwang commented on GitHub (Oct 16, 2024): seems avx/avx2 are not must-have libraries. https://github.com/ollama/ollama/issues/2903 https://github.com/ollama/ollama/issues/3891
Author
Owner

@ozbillwang commented on GitHub (Oct 16, 2024):

I made it, thanks to all

https://hub.docker.com/repository/docker/alpine/ollama/general

<!-- gh-comment-id:2415607832 --> @ozbillwang commented on GitHub (Oct 16, 2024): I made it, thanks to all https://hub.docker.com/repository/docker/alpine/ollama/general
Author
Owner

@zeeskore commented on GitHub (Jan 30, 2025):

I made it, thanks to all

https://hub.docker.com/repository/docker/alpine/ollama/general

This is very nice, but would be better if the image on hub.docker.com is updated with the current version of ollama 0.5.7 vs 0.3.13

<!-- gh-comment-id:2625633020 --> @zeeskore commented on GitHub (Jan 30, 2025): > I made it, thanks to all > > https://hub.docker.com/repository/docker/alpine/ollama/general This is very nice, but would be better if the image on hub.docker.com is updated with the current version of ollama 0.5.7 vs 0.3.13
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#51075