Orchestrating Docker Containers with Slack

Guest post by Jussi Nummelin, senior architect at Digia. Jussi is currently Dockerizing both customers and internal projects. You can follow him on Twitter(@JNummelin).


There's been recent trend to move team collaboration to online messaging apps such as Slack, Flowdock or similar. Some organisations have taken this even further and have integrated some of their operations tools to these messaging apps. This is actually a lot simpler than it sounds at first as these tools provide really straightforward APIs and client libraries to integrate to. When I discovered this I just had to try it out. :)

Control Kontena through Slack messages

As a current user of both Kontena and Slack I thought it would be really cool to be able to control my Kontena grid through Slack. The benefits are that the operations are now transparent for the whole team as the communication with Kontena is captured as a conversation with a control bot on a common team channel. It also makes debugging sessions with a colleague much simpler as you can create a private group and invite the bot to it. This way you both see in real-time the control flow with Kontena grid.

The integration

The bot is written using node.js and the Slack node client. The slack client makes things really simple as it provides few easy callbacks to react to incoming messages. Next step was to figure out how to integrate to Kontena. As Kontena already has a fantastic cli tool I really didn't want to create another option parsing and other such "boilerplate" stuff. So the node.js app integrates to Kontena grid using the Kontena cli tool. As the Slack client uses websockets and the bot opens the connection (same model as with Kontena) you can really run it anywhere. We're currently running it in our Kontena grid. ;-)

Interacting with the bot

On Slack side the bot control is like a normal conversation. Just invite the bot to your channel or send 1-to-1 message to it.

kontenabot: service list  

After that the bot responds with the results of the command. As the integration is done using the cli tool the bot commands are exactly the same as with the cli tool. Here's couple of examples:

The bot in a container

The bot is provided as ready made Docker image so it's a breeze to setup your own. On Slack create new bot integration to get needed API token. Just give the needed security tokens (for both Slack and Kontena) and other config as env vars and you're good to go.

docker run -d -e SLACK_TOKEN=xoxb-1243576874-XXXXXXXXXXXXXXXXXXX \  
  -e KONTENA_TOKEN=kontena-token -e KONTENA_URL= \
  -e KONTENA_GRID=my-grid -e SSL_IGNORE_ERRORS=true -e BOT_NAME=kontenabot \ 
  -e ALLOWED_USERS=user1,user2 --name kontenabot jnummelin/kontena-slack-bot:latest

In the above example we're specifying the SSL_IGNORE_ERRORS flag as the test environment has self-signed certificates. One thing to note is that you must name the bot exactly the same way in the container and in the Slack configuration. This way the bot knows that you are sending the messages to that specific bot. The ALLOWED_USERS list configures which of your organisations users are allowed to control the bot. If you omit the list the bot will allow any user to control it.

For details check out the github repo. And if you have ideas or find bugs please create some issues and PR's. :)

Image credits Dave Mathis, https://flic.kr/p/nCtH3g

Jussi Nummelin

Read more posts by this author.

Tampere, Finland
comments powered by Disqus