# Running Rasa with Docker

This is a guide on how to build a Rasa assistant with Docker. If you haven’t used Rasa before, we’d recommend that you start with the [Rasa Tutorial](https://legacy-docs-v1.rasa.com/1.4.6/user-guide/rasa-tutorial/#rasa-tutorial).

## Installing Docker
If you’re not sure if you have Docker installed, you can check by running:

```bash
docker -v && docker-compose -v
# Docker version 18.09.2, build 6247962
# docker-compose version 1.23.2, build 1110ad01
```

If Docker is installed on your machine, the output should show you your installed versions of Docker and Docker Compose. If the command doesn’t work, you’ll have to install Docker. See [Docker Installation](https://docs.docker.com/install/) for details.

## Building an Assistant with Rasa and Docker
This section will cover the following:

- Setting up your Rasa project and training an initial model
- Talking to your AI assistant via Docker
- Choosing a Docker image tag
- Training your Rasa models using Docker
- Talking to your assistant using Docker
- Running a Rasa server with Docker

### Setup
Just like in the [tutorial](https://legacy-docs-v1.rasa.com/1.4.6/user-guide/rasa-tutorial/#rasa-tutorial), you’ll use the `rasa init` command to create a project. The only difference is that you’ll be running Rasa inside a Docker container, using the image `rasa/rasa`. To initialize your project, run:

```bash
docker run -v $(pwd):/app rasa/rasa init --no-prompt
```

### Talking to Your Assistant
To talk to your newly-trained assistant, run this command:

```bash
docker run -it -v $(pwd):/app rasa/rasa shell
```

This will start a shell where you can chat to your assistant. Note that this command includes the flags `-it`, which means that you are running Docker interactively, and you are able to give input via the command line.

## Customizing your Model
### Choosing a Tag
To keep images as small as possible, we publish different tags of the `rasa/rasa` image with different dependencies installed. See [Choosing a Pipeline](https://legacy-docs-v1.rasa.com/1.4.6/nlu/choosing-a-pipeline/#choosing-a-pipeline) for more information about dependencies.

The tags are:

- `{version}`
- `{version}-spacy-en`
- `{version}-spacy-de`
- `{version}-mitie-en`
- `{version}-full`

### Training a Custom Rasa Model with Docker
Edit the `config.yml` file to use the pipeline you want and place your NLU and Core data into the `data/` directory. Now you can train your Rasa model by running:

```bash
docker run \
  -v $(pwd):/app \
  rasa/rasa:latest-full \
  train \
    --domain domain.yml \
    --data data \
    --out models
```

## Running the Rasa Server
To run your AI assistant in production, configure your required [Messaging and Voice Channels](https://legacy-docs-v1.rasa.com/1.4.6/user-guide/messaging-and-voice-channels/#messaging-and-voice-channels) in `credentials.yml`. If this file does not exist, create it using:

```bash
touch credentials.yml
```

Then edit it according to your connected channels. After, run the trained model with:

```bash
docker run \
  -v $(pwd)/models:/app/models \
  rasa/rasa:latest-full \
  run
```

## Using Docker Compose to Run Multiple Services
To run Rasa together with other services, such as a server for custom actions, it is recommended to use [Docker Compose](https://docs.docker.com/compose/).

Start by creating a file called `docker-compose.yml`:

```bash
touch docker-compose.yml
```

Add the following content to the file:

```yaml
version: '3.0'
services:
  rasa:
    image: rasa/rasa:latest-full
    ports:
      - 5005:5005
    volumes:
      - ./:/app
    command:
      - run
```

To run the services configured in your `docker-compose.yml` execute:

```bash
docker-compose up
```

## Adding Custom Actions
To create more sophisticated assistants, you will want to use [Custom Actions](https://legacy-docs-v1.rasa.com/1.4.6/core/actions/#custom-actions).

### Creating a Custom Action
Start by creating the custom actions in a directory `actions`:

```bash
mkdir actions
# Rasa SDK expects a python module.
# Therefore, make sure that you have this file in the directory.
touch actions/__init__.py
touch actions/actions.py
```

```python
import requests
import json
from rasa_sdk import Action

class ActionJoke(Action):
  def name(self):
    return "action_joke"

def run(self, dispatcher, tracker, domain):
    request = requests.get('http://api.icndb.com/jokes/random').json()
    joke = request['value']['joke']
    dispatcher.utter_message(joke)
    return []
```

### Adding the Action Server
The custom actions are run by the action server. To spin it up together with the Rasa instance, add a service `action_server` to the `docker-compose.yml`:

```yaml
version: '3.0'
services:
  rasa:
    image: rasa/rasa:latest-full
    ports:
      - 5005:5005
    volumes:
      - ./:/app
    command:
      - run
  action_server:
    image: rasa/rasa-sdk:latest
    volumes:
      - ./actions:/app/actions
```

### Adding a Custom Tracker Store
By default, all conversations are saved in memory. If you want to persist your conversations, you can use a different [Tracker Store](https://legacy-docs-v1.rasa.com/1.4.6/api/tracker-stores/#tracker-stores).

### Using PostgreSQL as Tracker Store
Start by adding PostgreSQL to your docker-compose file:

```yaml
postgres:
  image: postgres:latest
```

Then add PostgreSQL to the `tracker_store` section of your endpoint configuration `config/endpoints.yml`:

```yaml
tracker_store:
  type: sql
  dialect: "postgresql"
  url: postgres
  db: rasa
```

### Using MongoDB as Tracker Store
Start by adding MongoDB to your docker-compose file:

```yaml
mongo:
  image: mongo
  environment:
    MONGO_INITDB_ROOT_USERNAME: rasa
    MONGO_INITDB_ROOT_PASSWORD: example
```

Then add the MongoDB to the `tracker_store` section of your endpoints configuration `endpoints.yml`:

```yaml
tracker_store:
  type: mongod
  url: mongodb://mongo:27017
  username: rasa
  password: example
```

### Using Redis as Tracker Store
Start by adding Redis to your docker-compose file:

```yaml
redis:
  image: redis:latest
```

Then add Redis to the `tracker_store` section of your endpoint configuration `endpoints.yml`:

```yaml
tracker_store:
  type: redis
  url: redis
```

### Using a Custom Tracker Store Implementation
If you have a custom implementation of a tracker store you have two options to add this store to Rasa:

- extending the Rasa image
- mounting it as volume

Then add the required configuration to your endpoint configuration `endpoints.yml` as described in [Tracker Stores](https://legacy-docs-v1.rasa.com/1.4.6/api/tracker-stores/#tracker-stores).
