Rasa and Docker Containers
Let's discuss how you can use Docker together with Rasa.
Video
Code Used
Let's start a new Rasa project locally via;
python -m pip install rasa
rasa init
rasa train
You can now run the assistant locally via;
rasa run --enable-api
This should allow you to call the trained model via a web API. You
can point traffic to http://localhost:5000/webhook
and you should
be able to get responses. You can do this with a tool like
postman or insomnia
but you can also use curl
from the command line.
curl --request POST \
--url http://localhost:5005/model/parse \
--header 'Content-Type: application/json' \
--data '{"text": "hi there"}'
Dockerfile
Let's see if we can build a Docker image for Rasa. That way, we might
more easily share our code for deployment. To do this we'll need to
write a Dockerfile
first.
# use a python container as a starting pointFROM python:3.7-slim
# install dependencies of interestRUN python -m pip install rasa
# set workdir and copy data files from disk# note the latter command uses .dockerignoreWORKDIR /appENV HOME=/appCOPY . .
# train a new rasa modelRUN rasa train nlu
# set the user to run, don't run as rootUSER 1001
# set entrypoint for interactive shellsENTRYPOINT ["rasa"]
# command to run when container is called to runCMD ["run", "--enable-api", "--port", "8080"]
To make sure that we don't copy files into the container
that we won't need, we'll add a .dockerignore
file.
tests/*
models/*
actions/*
**/*.md
venv
This way, docker won't copy all the files in the container
run the COPY . .
command.
Building and Running
To build the image, we'll need to run the following command;
docker build -t koaning/rasa-demo .
Note that we're building the image with the tag koaning/rasa-demo
.
This is a convention that we'll use to identify the image later.
Once the image is built, we can run the container with the following command;
docker run -it -p 8080:8080 koaning/rasa-demo
This command does a few things.
- The
koaning/rasa-demo
is a reference to the container that we built
earlier.
- The
-it
flag means that we want to run the container interactively. - The
-p 8080:8080
means that we want to route the port 8080 from the
container to port 8080 on the host.
When the container spins up you should be able to once again communicate with the Rasa model via the web API.
curl --request POST \
--url http://localhost:5005/model/parse \
--header 'Content-Type: application/json' \
--data '{"text": "hi there"}'
The difference now is that the model is running inside of a docker container. You can imagine that we're able to share this container and as long as you're able to run docker ... you're also able to run Rasa!
Entrypoint
You can do even more with the container that we have right now. For example, you could run the rasa shell via our entrypoint.
docker run -it koaning/rasa-demo shell
Note that this command is equivalent to running rasa shell
from the
command line.
Push
You can also push the container to a repository so it's available to other people.
docker push koaning/rasa-demo
Next steps.
In the next video we'll use the docker containers that Rasa provides on dockerhub instead of writing our own from scratch.
Exercises
Try to answer the following questions to test your knowledge.
- What might go wrong if you don't have a
.dockerignore
file? - How big might a docker image be with Rasa in it? What are the main contributing factors in the size?