Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cog

> DevOps
Open source

Containers for machine learning

9.5K stars0 likes1 views
WebsiteGitHub

About

Containers for machine learning

Cog: Containers for machine learning

Cog is an open-source tool that lets you package machine learning models in a standard, production-ready container.

You can deploy your packaged model to your own infrastructure, or to Replicate.

Highlights

  • Docker containers without the pain. Writing your own Dockerfile can be a bewildering process. With Cog, you define your environment with a simple configuration file and it generates a Docker image with all the best practices: Nvidia base images, efficient caching of dependencies, installing specific Python versions, sensible environment variable defaults, and so on.

  • ️ No more CUDA hell. Cog knows which CUDA/cuDNN/PyTorch/Tensorflow/Python combos are compatible and will set it all up correctly for you.

  • ✅ Define the inputs and outputs for your model with standard Python. Then, Cog generates an OpenAPI schema and validates the inputs and outputs.

  • Automatic HTTP inference server: Your model's types are used to dynamically generate a RESTful HTTP API using a high-performance Rust/Axum server.

  • Ready for production. Deploy your model anywhere that Docker images run. Your own infrastructure, or Replicate.

How it works

Define the Docker environment your model runs in with cog.yaml:

yaml
build:
  gpu: true
  system_packages:
    - "libgl1"
    - "libglib2.0-0"
  python_version: "3.13"
  python_requirements: requirements.txt
run: "run.py:Runner"

Define how your model runs with run.py:

python
from cog import BaseRunner, Input, Path
import torch

class Runner(BaseRunner):
    def setup(self):
        """Load the model into memory to make running multiple inferences efficient"""
        self.model = torch.load("./weights.pth")

    # The arguments and types the model takes as input
    def run(self,
          image: Path = Input(description="Grayscale input image")
    ) -> Path:
        """Run the model"""
        processed_image = preprocess(image)
        output = self.model(processed_image)
        return postprocess(output)

In the above we accept a path to the image as an input, and return a path to our transformed image after running it through our model.

Now, you can run the model:

bash
$ cog run -i [email protected]
--> Building Docker image...
--> Running...
--> Output written to output.jpg

Or, build a Docker image for deployment:

bash
$ cog build -t my-classification-model
--> Building Docker image...
--> Built my-classification-model:latest

$ docker run -d -p 5000:5000 --gpus all my-classification-model

$ curl http://localhost:5000/predictions -X POST \
    -H 'Content-Type: application/json' \
    -d '{"input": {"image": "https://.../input.jpg"}}'

Or, combine build and run via the serve command:

bash
$ cog serve -p 8080

$ curl http://localhost:8080/predictions -X POST \
    -H 'Content-Type: application/json' \
    -d '{"input": {"image": "https://.../input.jpg"}}'

Why are we building this?

It's really hard for researchers to ship machine learning models to production.

Part of the solution is Docker, but it is so complex to get it to work: Dockerfiles, pre-/post-processing, Flask servers, CUDA versions. More often than not the researcher has to sit down with an engineer to get the damn thing deployed.

Andreas and Ben created Cog. Andreas used to work at Spotify, where he built tools for building and deploying ML models with Docker. Ben worked at Docker, where he created Docker Compose.

We realized that, in addition to Spotify, other companies were also using Docker to build and deploy machine learning models. Uber and others have built similar systems. So, we're making an open source version so other people can do this too.

Hit us up if you're interested in using it or want to collaborate with us. We're on Discord or email us at [email protected].

Prerequisites

  • macOS, Linux or Windows 11. Cog works on macOS, Linux and Windows 11 with WSL 2
  • Docker. Cog uses Docker to create a container for your model. You'll need to install Docker before you can run Cog. If you install Docker Engine instead of Docker Desktop, you will need to install Buildx as well.

Install

Choose your platform for installation instructions.

macOS

The easiest way to install Cog on macOS is with Homebrew:

bash
brew install replicate/tap/cog

You can also use the install script:

bash
# bash, zsh, and other shells
sh <(curl -fsSL https://cog.run/install.sh)

# fish shell
sh (curl -fsSL https://cog.run/install.sh | psub)

# download with wget and run in a separate command
wget -qO- https://cog.run/install.sh
sh ./install.sh

Or install manually:

bash
sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m | sed 's/aarch64/arm64/')"
sudo chmod +x /usr/local/bin/cog
sudo xattr -d com.apple.quarantine /usr/local/bin/cog 2>/dev/null || true

If you see a Gatekeeper warning saying the binary "cannot be opened because the developer cannot be verified", run:

bash
sudo xattr -d com.apple.quarantine /usr/local/bin/cog
Linux

You can install Cog using the install script:

bash
# bash, zsh, and other shells
sh <(curl -fsSL https://cog.run/install.sh)

# fish shell
sh (curl -fsSL https://cog.run/install.sh | psub)

# download with wget and run in a separate command
wget -qO- https://cog.run/install.sh
sh ./install.sh

Or install manually:

bash
sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m | sed 's/aarch64/arm64/')"
sudo chmod +x /usr/local/bin/cog
Windows

Cog does not natively support Windows, but you can run it on Windows 11 using WSL 2. Once WSL 2 is set up, follow the Linux installation instructions above.

Docker

To install Cog inside a Docker image:

dockerfile
RUN sh -c "INSTALL_DIR=\"/usr/local/bin\" SUDO=\"\" $(curl -fsSL https://cog.run/install.sh)"

Upgrade

If you're using macOS and you previously installed Cog with Homebrew, run the following:

bash
brew upgrade replicate/tap/cog

Otherwise, you can upgrade to the latest version by running the same commands you used to install it.

Development

See CONTRIBUTING.md for how to set up a development environment and build from source.

Next steps

  • Get started with an example model
  • Get started with your own model
  • Using Cog with notebooks
  • Using Cog with Windows 11
  • Browse the example models in this repo
  • Deploy models with Cog
  • cog.yaml reference to learn how to define your model's environment
  • Run interface reference to learn how the Runner interface works
  • Training interface reference to learn how to add a fine-tuning API to your model
  • HTTP API reference to learn how to use the HTTP API that models serve

Need help?

Join us in #cog on Discord.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Ben Firshman


Andreas Jansson


Zeke Sikelianos


Rory Byrne

⚠️

Michael Floering


Ben Evans


shashank agarwal


VictorXLR

⚠️

hung anna


Brian Whitman


JimothyJohn


ericguizzo


Dominic Baggott

Issues· 80 open

View all issuesOpen on GitHub
  • #3164

    Fail to push container to r8.im with "error during image push: unknown blob"

    Updated Sep 3, 2026
  • #625

    Warn if nvidia-docker is not installed

    documentationgood first issuegoUpdated Aug 31, 2026
  • #3162

    OpenTelemetry log export

    Updated Aug 26, 2026
  • #842

    `cog login` fails silently if `pass` is uninitialized

    type/bugnextUpdated Jun 26, 2026
  • #258

    Local images should be tagged and the previous image should be removed

    enhancementgoUpdated Jun 26, 2026
  • #2757

    Refactor --separate-weights to use named build contexts and Dockerfile.dockerignore

    enhancementgoUpdated Jun 26, 2026
  • #2742

    Add support for reading from and writing to cloud storage

    enhancementtype/epicUpdated Jun 26, 2026
  • #2709

    Support shared code between multiple models

    enhancementgotype/epicUpdated Jun 26, 2026
  • #623

    Add ordering to output objects

    enhancementgonextUpdated Jun 26, 2026
  • #614

    Support simple TTY control characters to make progress bars work

    enhancementgorustUpdated Jun 26, 2026

> Tags

Goaicontainerscudadocker

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理