How to Install LocalAI with Docker: An OpenAI-Compatible Server for Local Models

 


Here is a familiar situation: an application already talks to an OpenAI-style API, and everything about its prompts, message format, and response handling is built around that shape. Then a reason appears to move some of that work in-house. Maybe certain data should never leave your own machine. Maybe the monthly API bill has started to sting. Maybe you simply want to decide for yourself which model answers, and when it changes. Rewriting the whole application just to switch providers would be a miserable way to get there.

LocalAI offers a cleaner route. It is a local inference server that speaks an OpenAI-compatible API, so software written for that format can be pointed at a local address instead. Its official documentation recommends Docker as the installation method for most users, because it works across platforms and spares you from assembling dependencies by hand. This guide follows that route: one container, one port, and a first model you can actually talk to through the API.

Before you start, make sure Docker is installed and its daemon is running on the target machine, whether that is a laptop or a VPS. You will also want generous free disk space, because model files are large and they accumulate every time you add another one. No GPU is required for the steps here; the CPU image is enough to learn the whole flow, and GPU options are covered separately below.

Step 1: Pick the Docker image that matches your hardware

The LocalAI Quickstart documentation separates images by hardware. For CPU only, the image is localai/localai:latest. For NVIDIA GPUs with CUDA, it is localai/localai:latest-gpu-nvidia-cuda-12, plus the --gpus all flag. For AMD (ROCm), the image is localai/localai:latest-gpu-hipblas. For Intel GPUs, it is localai/localai:latest-gpu-intel. For Vulkan, it is localai/localai:latest-gpu-vulkan, with the appropriate device flags for AMD, Intel, and Vulkan setups.

This guide uses the CPU image; the workflow is the same across variants, and only the image name and GPU flags change. If you are following along on a VPS without a GPU, stay on the CPU image and do not force GPU flags — the container will fail to start or behave oddly when the hardware it references does not exist.

Step 2: Start your first container and confirm port 8080 answers

The shortest command from the official documentation looks like this:

docker run -p 8080:8080 --name local-ai -ti localai/localai:latest

Each part earns its place. The -p 8080:8080 option maps port 8080 inside the container to port 8080 on the host, which is how both the web interface and the API become reachable. The --name local-ai option gives the container a stable name, so later commands are easy to type. The -ti option attaches an interactive terminal, so the server logs appear right in front of you.

What you should see: LocalAI logs streaming in the terminal, without errors repeating in a loop. Open a browser and visit http://localhost:8080. If the LocalAI web interface appears, the server is alive. If LocalAI runs on a VPS and you are browsing from another computer, replace localhost with the VPS address — and read the security section near the end before leaving that port reachable.

Two failures show up often at this stage. The first is a port conflict: if Docker reports that port 8080 is already allocated, another service on the host owns it. Do not fight over the same port; change the host side of the mapping instead, for example -p 8081:8080, then open http://localhost:8081. The second is a name clash: if a container called local-ai already exists from an earlier attempt, docker run will refuse. Check with docker ps -a, remove the old container you no longer need, and run the command again.

Step 3: Install your first model through the web interface

An empty container cannot answer anything yet, because no model is installed. The Quickstart documentation suggests qwen3-4b as a starting point, describing it as small, CPU-friendly, and capable of tool calling, which makes it reusable later for agent-style work.

Through the web interface, the flow is:

  1. Open http://localhost:8080 in your browser.
  2. Go to Models → Explore.
  3. Search for qwen3-4b and click Install on that entry.
  4. Wait for the download to finish. Model files are large, so this takes a while depending on your connection.

What you should see: qwen3-4b moves into the installed-models list, and the Chat page lets you pick qwen3-4b from the model dropdown, type a message, and receive a reply. If the first reply feels slow, give it a moment — a freshly loaded model needs a short warm-up before it serves its first request. If a download stalls halfway, check free disk space and the network connection before retrying.

Step 4: Test the API with curl, exactly as you would call an OpenAI-style API

This is the heart of the whole guide. LocalAI exposes an OpenAI-compatible API, so your application only needs to point at http://localhost:8080. You can prove that without writing any application code, using curl.

First, list the models the server knows about:

curl http://localhost:8080/v1/models

An empty list usually means the installation has not finished, or you are inspecting a different container than the one that installed the model.

Next, send a chat request, following the example from the Quickstart documentation:

curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3-4b",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

What you should see: a JSON response carrying the model's reply to your message. Notice the pattern — base address http://localhost:8080, path /v1/chat/completions, and a JSON body with model and messages. That is the same shape OpenAI clients already use. In a real application, the change is therefore usually limited to the base address and the model name, not a rewrite of the conversation logic.




The diagram sums up the flow: a client or application sends a request to the API endpoint, LocalAI passes it to the installed model, and the answer returns along the same path. No cloud service sits in the middle, because the model runs on your own hardware.

Step 5: Restart with a models volume so nothing vanishes when the container is removed

The short command in Step 2 is fine for a trial, but it has a weakness: models downloaded inside the container can disappear when the container is removed. For anything you plan to use more than once, bind a models folder from the host into the container. The official model-setup documentation gives this example:

docker run -p 8080:8080 -v $PWD/models:/models -ti --rm localai/localai:latest --models-path /models --context-size 700 --threads 4

Before running it, stop and remove the trial container from Step 2 so the name and port do not collide:

docker stop local-ai
docker rm local-ai

Then create the models folder in your working directory if it does not exist yet:

mkdir -p models

A quick reading of the volume command: -v $PWD/models:/models binds the models folder in your working directory to /models inside the container, while --models-path /models tells LocalAI to read models from that path. The --rm option removes the container automatically when it stops, but your models stay safe because they live in the host folder, not inside the container. The --context-size 700 and --threads 4 values are example parameters from the documentation; adjust them if the model you use needs a longer context or your machine has more CPU cores to spare.

Once the volume-backed container is running, repeat Steps 3 and 4: install the model through Models → Explore, then check with curl http://localhost:8080/v1/models. What you should see: the model is recognized, and if you stop the container and start it again with the same volume command, the model is still there with no fresh download required.

Step 6: Lock down the API before it is reachable from a network

While LocalAI only listens on the same machine through localhost, the exposure is limited. The moment port 8080 becomes reachable from another network — for instance, on a VPS — anyone who can reach that port could potentially use your server. The Quickstart documentation names two protections for this case: a simple API key through the LOCALAI_API_KEY variable, and multi-user authentication through LOCALAI_AUTH=true, which brings admin and user roles, per-user API keys, and usage tracking.

The practical rule: do not expose port 8080 to the internet without one of those protections, and never share an API key through a repository or a screenshot. If you are unsure, keep LocalAI reachable only locally and access it through a secure tunnel or VPN to your VPS.

Common problems and how to fix them

  • Port already in use: change the host side of -p, for example -p 8081:8080, then open the new host port.
  • Container name clash: check with docker ps -a, remove the old container you no longer need, and run the command again.
  • Model missing from /v1/models: confirm the install through Models → Explore fully completed, and confirm you are checking the same container that installed it.
  • Chat error saying the model was not found: match the model value in the JSON body exactly to the installed model name, not to a name you guessed.
  • Container stopping unexpectedly: inspect the logs with docker logs local-ai and check remaining RAM and disk space, since a model larger than the machine can handle will bring the process down.
  • Slow first reply: allow time for the model to load, especially on CPU machines, before concluding something is broken.

Summary table

StepCommand or pathWhat to check
Start the CPU containerdocker run -p 8080:8080 --name local-ai -ti localai/localai:latestWeb interface opens at http://localhost:8080
Install a modelWeb interface Models → Explore, search qwen3-4b, click InstallModel appears in the installed list
List modelscurl http://localhost:8080/v1/modelsJSON response includes the installed model
Test chatcurl http://localhost:8080/v1/chat/completions with model and messagesJSON response contains the model's reply
Run with a volumedocker run -p 8080:8080 -v $PWD/models:/models -ti --rm localai/localai:latest --models-path /models --context-size 700 --threads 4Model survives a container restart
Protect network accessLOCALAI_API_KEY or LOCALAI_AUTH=true as documentedAPI is not freely usable from outside networks

Final checklist

  • [ ] Docker is running and the CPU image command completes without errors.
  • [ ] The LocalAI web interface opens at http://localhost:8080 (or the host port you chose).
  • [ ] The qwen3-4b model finishes installing through Models → Explore.
  • [ ] curl http://localhost:8080/v1/models shows that model.
  • [ ] A curl call to /v1/chat/completions returns a reply from the model.
  • [ ] The container is restarted with the -v $PWD/models:/models volume and the model is still recognized.
  • [ ] If the port is reachable from a network, API protection is active before the address is shared.

Once every box is checked, you have a foundation worth building on: a local AI server that speaks an API shape many applications already understand. From here the directions branch — swapping in a model that fits your workload better, pointing an existing application at the local address, or exploring the LocalAI model gallery in more depth. Which direction appeals to you most: local chat for internal use, automation tasks, or replacing one cloud API call you use constantly? Share your plan in the comments, including which model you want to try next and what tripped you up while following the steps above.

Read also: the related older article titled Using Ollama Models from Your Own App via the OpenAI-Compatible API, and the same-batch companion piece at https://blog.rnevio.com/2026/10/how-to-run-llamacpp-server-on-cpu-vps.html.

Diskusi

Ada pertanyaan atau pengalaman yang ingin dibagikan?

0komentar

Komentar ditinjau sebelum ditampilkan. Mari berdiskusi dengan sopan dan sesuai topik.