Dockerize a Facebook bot (Botkit) and deploy it to Heroku

In this article, we will dockerize our Facebook bot built with Botkit Framework and deploy to Heroku using Heroku Container Registry.

First, build the bot project from Botkit

  • Install Botkit on your computer :
1
npm install -g botkit
  • Use Botkit CLI to build a facebook bot project :
1
new --platform facebook

The bot project I named : dockerized-bot is created. You can get into it :

1
cd dockerized-bot

The bot project is a Node.js application based on an express.js server listening on port : 3000.

This information allows us to chose our Docker Image.

How docker works ?

It allows you to bundle your app with all its dependencies into a container. “A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.” ~Docker.

Indeed, after building our container, it will be our packaged application.

To do so, our app should include a Dockerfile which will describe how will the container be build. Our image will be based on Docker Image for Nodejs (carbon)

1
touch Dockerfile

Open the file using a Text Editor then copy these line inside:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM node:carbon
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json .
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]

You must be asking yourself what these lines reffer to. If not. Well, you should have !

These are instructions. Each calls an action :

  • FROM is used to specify the Base Image from which yours will be built. the Base Image is stored in a DockerHub.
  • WORKDIR sets the working directory for other instructions.
  • COPY is used to copy files and directories from a source to a destinaiton.
  • RUN runs a shell command
  • EXPOSE tells Docker that the app is listening on a specific port at the runtime.
  • CMD specifies the command to execute when running the container.

To prevet copying node_modules and log files to your image, a dockerignore file can be created.

1
touch .dockerignore

It contains :

1
2
node_modules
npm-debug.log

Build the image :

1
docker build -t <your username>/dockerized-bot

The image is succefuly built. We can see it docker images :

1
docker images

Now we should run the image to package our app and generate our container. This will happen in detach mode using -dflag. It means that the container will run in background. Also, we’ll use -p to match a public port to the private port of the container. Here we will use the same port 3000 in both cases.

1
docker run -p 3000:3000 -d <your_username>/dockerized-bot

Note: that you can use --name flag to name your container. In my case, Docker generated a name for my container.

As you can see, a container is running (docker pslists the containers).

The app is running too. You can check it in your navigator :

At this point, your bot project is dockerized. But since Facebook webhooks require a public adress. We will deploy our container to Heroku.

First you should login to heroku using Heroku CLI. (To install the CLI: curl https://cli-assets.heroku.com/install-standalone.sh | sh)

1
heroku login

Then install heroku-container-plugin which is a plugin that allows you to deploy Docker-based apps to heroku.

1
heroku plugins:install heroku-container-registry

You should login to the registry too :

1
heroku container:login

Create a Heroku app :

1
heroku create dockerized-bot

Set configuration vars to Heroku :

1
2
heroku config:set page_token=$PAGE_TOKEN
heroku config:set verify_token=$VERIFY_TOKEN

Your bot will need them to match your code to your Facebook App :

You can check that you app is well created on Heroku Dashboard :

Remember that the commands are executed inside of your working directory :

All we have to do now is to push our container to the heroku app :

1
heroku container:push web --app dockerized-bot

Notice that we didn’t need a Procfile for our web app. This option is Container Registry and it can be an alternative for the Github/Dropbox/Heroku Git options.

On Heroku :

Once the app is pushed to Heroku, you can open the app :

1
heroku open --app dockerized-bot

The URL of your app on Heroku will be your base Webhook URL in Facebook.

You can push your image to Docker Hub which the Native Registry of Docker Cloud. Once you push your image to Docker Hub, it is available on Docker Cloud.

1
2
docker login
docker tag <IMAGE_ID> <USERNAME>/dockerized-bot:firsttry

dockerized-bot here is the name of your registry on Docker Hub.

This process can be automated using a CI/CD pipeline.
I’ll be writing another article for that purpose.

Thanks for reading ❤


Comments: