﻿# Install BRIX AI using Docker Swarm

> [HTML Version](cortex-docker-swarm.html)

This article describes installation using Docker Swarm with internet access. To learn about installing **BRIX AI** offline, see [Offline installation](cortex-harbor.md).

This deployment option is suitable for production environments because it allows you to scale microservices as load increases.

Before you begin, make sure that:

1. The target server is initialized as a Docker Swarm manager node. To do this, run the command `docker swarm init` when creating the cluster.

2. You have [downloaded the installation files](cortex-download-installer.md) and [configured environment variables](cortex-environment-variables.md).

Please note:

- MongoDB replication requires an internal authentication key for database nodes. It is [created automatically during installation](#mongodb-key).

- If you use self-signed certificates, [export the root certificate](#caddy) after installation to add it to your list of trusted certificates.

## Install BRIX AI

To install **BRIX AI** using Docker Swarm:

1. Run the installation script:

````
chmod +x install.sh  
./install.sh

````
If you use multiple instances of **BRIX AI** and need to specify a [project name](cortex-environment-variables.md#project-name), use the following parameter:

````
./install.sh --project-name my-cortex

2. ````
After running the `install.sh` script, some services may take a few minutes to start up completely. To check if the services are ready, run the following command:

````
docker service logs -f elma-cortex\_gamma-apps

3. ````
Wait until the message “Application startup complete” appears in the logs.

### Create a key for MongoDB

During the installation of **BRIX AI**, the `install.sh` script automatically generates an authentication key file used for MongoDB replication, along with the required access permissions. It is passed to Docker Swarm via the `mongodb-keyfile` secret.

If you need to generate a new key manually, run the following commands:

````
openssl rand -base64 756 > mongo.keyfile  
sudo chmod 400 mongo.keyfile  
  
docker secret create mongo\_keyfile mongo.keyfile

### ````
Export the Caddy root certificate

If you selected **Internal** as the [TLS certificate management mode](cortex-tls-settings.md), export the Caddy root certificate to add it to your trusted certificates:

````
docker exec \$(docker ps -q -f name=elma-cortex\_caddy) cat /data/caddy/pki/authorities/local/root.crt > caddy\_root.crt

````
If the command fails, try an alternative method:

````
docker ps -f name=elma-cortex\_caddy # returns <CONTAINER\_ID>  
docker cp <CONTAINER\_ID>:/data/caddy/pki/authorities/local/root.crt ./caddy\_root.crt

## ````
Update BRIX AI

When a new version of **BRIX AI** is released, save your current configuration and perform the update. To learn more, see [Update BRIX AI using Docker Swarm](update-cortex-docker-swarm.md).

## Uninstall BRIX AI

Installing **BRIX AI** in Docker Swarm creates a stack representing your installation instance. You can remove the stack along with all associated data.

### Uninstall the stack while preserving data

To remove all **BRIX AI** services from the Docker Swarm cluster without clearing data, use one of two methods:

- Run the uninstallation script:

````
chmod +x uninstall.sh  
./uninstall.sh

````
If you use multiple instances of **BRIX AI** and [specify a project name in scripts](cortex-environment-variables.md#project-name), use the following parameter:

````
./uninstall.sh --project-name my-cortex

- ````
Remove the stack from Docker Swarm manually:

````
docker stack rm elma-cortex

### ````
Uninstall the stack and delete data

To permanently remove **BRIX AI** and all associated data:

1. Remove the stack using the following command:

````
./uninstall.sh

````
If you use multiple instances of **BRIX AI** and [specify a project name in scripts](cortex-environment-variables.md#project-name), use the following parameter:

````
./uninstall.sh --project-name my-cortex

2. ````
Wait until all services stop.

3. Delete data volumes:

````
docker volume rm \$(docker volume ls -q -f name=elma-cortex)

````
This operation also deletes data from Caddy's internal certificate authority. If you use **Internal** as the [TLS certificate management mode](cortex-tls-settings.md), you must [export the Caddy root certificate](#caddy) again after reinstallation and add it to your trusted certificates.