In this article, we will dockerize our Facebook bot built with Botkit Framework and deploy to Heroku using Heroku Container Registry.
- Install Botkit on your computer :
npm install -g botkit
- Use Botkit CLI to build a facebook bot project :
new --platform facebook
The bot project I named : dockerized-bot is created. You can get into it :
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.
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)
Open the file using a Text Editor then copy these line inside:
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.
It contains :
Build the image :
docker build -t <your username>/dockerized-bot
The image is succefuly built. We can see it 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.
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)
Then install heroku-container-plugin which is a plugin that allows you to deploy Docker-based apps to heroku.
heroku plugins:install heroku-container-registry
You should login to the registry too :
Create a Heroku app :
heroku create dockerized-bot
Set configuration vars to Heroku :
heroku config:set page_token=$PAGE_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 :
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 :
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.
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 ❤