cd..blog

Scalable AI: Sails.js, Docker Cloud, & Cohere for Modern Apps

const published = "Mar 10, 2026, 10:36 PM";const readTime = 4 min;
sailsjsdockercoheretypescriptai
Build intelligent, scalable web applications by integrating Sails.js, Cohere's powerful AI, and Docker Cloud for seamless deployment.

Modern web applications demand both robust backend capabilities and intelligent features, all while maintaining scalability and ease of deployment. This post demonstrates how to integrate Sails.js, a powerful Node.js framework, with the Cohere API for advanced AI, and deploy it seamlessly using Docker Cloud's cloud-native features.

Setting Up Sails.js with TypeScript

Sails.js is a real-time MVC framework for Node.js, designed to build custom, enterprise-grade Node.js applications and APIs efficiently. It provides an opinionated structure and conventions to streamline development. We'll start by scaffolding a new Sails.js project with TypeScript support.

First, ensure you have Sails.js globally installed:

npm install -g sails
sails new ai-sails-app --no-frontend --template=typescript
cd ai-sails-app

Next, install necessary dependencies, including the Cohere client library.

Integrating the Cohere API for AI Capabilities

Cohere provides powerful large language models (LLMs) for a wide range of natural language processing tasks, including text generation, embeddings, and summarization. Its API enables developers to integrate advanced AI capabilities into their applications with ease.

Install the Cohere SDK:

npm install cohere-ai
npm install --save-dev @types/cohere-ai # If types are not bundled

Create a new Sails.js service to encapsulate Cohere interactions. This promotes modularity and reusability.

api/services/CohereService.ts:

import cohere from 'cohere-ai';

declare const sails: any;

export default {
  init() {
    if (!process.env.COHERE_API_KEY) {
      sails.log.error('COHERE_API_KEY environment variable is not set.');
      return;
    }
    cohere.init(process.env.COHERE_API_KEY);
    sails.log.info('Cohere service initialized.');
  },

  async generateText(prompt: string, maxTokens: number = 100): Promise<string | undefined> {
    try {
      const response = await cohere.generate({
        model: 'command-light',
        prompt: prompt,
        max_tokens: maxTokens,
        temperature: 0.7,
        num_generations: 1,
      });
      return response.body.generations[0].text.trim();
    } catch (error) {
      sails.log.error('Cohere text generation error:', error);
      return undefined;
    }
  },

  // Add other Cohere functions like embed, summarize, etc.
};

Remember to call sails.services.cohereservice.init() during app bootstrap, e.g., in config/bootstrap.ts.

Dockerizing Your Sails.js Application

Docker provides a platform for building, shipping, and running applications in isolated environments called containers, ensuring consistency across development, testing, and production. Dockerizing our Sails.js app makes it portable and scalable.

Create a Dockerfile in your project root:

# Use a Node.js base image
FROM node:18-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to install dependencies
COPY package*.json ./

# Install dependencies, including dev dependencies for TypeScript compilation
RUN npm install --production=false

# Copy the rest of the application code
COPY . .

# Compile TypeScript to JavaScript
RUN npm run build

# Remove dev dependencies and prune for production readiness
RUN npm prune --production

# Expose the port your Sails.js app runs on (default is 1337)
EXPOSE 1337

# Define the command to run your application
CMD ["node", "app.js"]

Build your Docker image locally:

docker build -t ai-sails-app .

Deploying with Docker Cloud

Docker Cloud (now largely integrated with Docker Hub and Docker Desktop with cloud-native features) provides a platform for building, deploying, and managing containerized applications across various cloud providers. It simplifies CI/CD workflows and orchestration. After building your image, push it to Docker Hub and then configure deployment via Docker's cloud services.

First, tag and push your image to Docker Hub:

docker tag ai-sails-app your-dockerhub-username/ai-sails-app:latest
docker push your-dockerhub-username/ai-sails-app:latest

From Docker Hub, you can then configure automated deployments to your cloud provider of choice (e.g., AWS, Azure, GCP) using Docker's integrated services, defining environment variables like COHERE_API_KEY securely.

Bringing It All Together: An AI-Enhanced Endpoint

Let's create a Sails.js controller endpoint that leverages our CohereService to generate text based on a user prompt.

api/controllers/AIController.ts:

import { Request, Response } from 'express';

declare const sails: any;

export default {
  async generate(req: Request, res: Response) {
    const { prompt } = req.body;

    if (!prompt) {
      return res.badRequest({ message: 'Prompt is required.' });
    }

    try {
      const generatedText = await sails.services.cohereservice.generateText(prompt);

      if (generatedText) {
        return res.ok({ text: generatedText });
      } else {
        return res.serverError({ message: 'Failed to generate text from Cohere.' });
      }
    } catch (error) {
      sails.log.error('Error in AIController.generate:', error);
      return res.serverError({ message: 'An unexpected error occurred.' });
    }
  },
};

Add a route in config/routes.ts:

module.exports.routes = {
  'POST /ai/generate': 'AIController.generate',
};

Conclusion

By integrating Sails.js for robust backend development, Cohere API for intelligent text generation, and Docker Cloud for streamlined deployment, you can build powerful, scalable, and AI-driven web applications. This architecture provides a solid foundation for modern, feature-rich services, enabling developers to focus on innovation rather than infrastructure complexities.