Deploying a Go Slack Bot on Cycle
The Cloud is enabling companies to scale their businesses affordably. But the jump from development to managing containers, deployment, networks and learning all the tools a cloud platform provides can be very challenging. Fortunately, Cycle has come along and made this process incredibly simple. This article will cover how I deployed a Slack bot onto Cycle.
This article would be too long if I tried to cover everything, so I will be assuming the following:
- You have a Slack API created. This will be the bot instance of your application and will have all the tokens you need to connect to it. Slack API
- You have a server set up on Cycle — the steps up to ‘Creating an Environment’, from their getting started guide.
- You have Docker installed and have an account on Docker Hub
### Checkout the repository ~$ git clone https://github.com/kamiyaa/cycle-projects.git ~$ cd cycle-projects/slack-bot ### Building ~$ go build . ### Installing ~$ go install . ### Running ~$ ./slack-bot
To keep our Slack token secret, our container will utilize persistent storage for storing configs. Docker volumes allows us to achieve this and Cycle’s stateful containers allows to easily configure and scale these volumes. More on stateful containers later.
### example bot.toml config file slack_token = "xox...." port = 4000 cycle_api_key = "secret_...." hub_id = "..."
In order to deploy our Docker container to the cloud, we need to write a Dockerfile, build it and push it onto a Docker Registry such as Docker Hub. The Dockerfile for our Slack bot can be found here.
FROM golang:latest ARG SLACKBOT_HOME=/var/slack_bot ARG WORK_DIR=/home/builds ARG BOT_CONFIG=$SLACKBOT_HOME/bot.toml ENV SLACKBOT_CONFIG $BOT_CONFIG VOLUME $SLACKBOT_HOME COPY / $WORK_DIR RUN cd $WORK_DIR \ && go install . ENTRYPOINT [ "./bin/slack-bot" ]
Here, we have a Dockerfile that copies all our source files into
/home/builds , then installs it into
/bin (default GOPATH). We specify a volume at
/var/slack_bot where our config file will reside. The data in this volume will persist even when containers are restarted or reimaged. We specify an environment variable so our bot knows where to find the config file. Finally, we have an entrypoint; the command our container will run when started.
Now we must host our docker image somewhere for Cycle to see. So we will push our image to Docker Hub.
# Build our docker image ~$ docker build -t <dockerhub_username>/<dockerhub_repository> -f Dockerfile . # push our docker image onto docker hub ~$ docker push <dockerhub_username>/<dockerhub_repository>:latest
Head over to your Cycle Dashboard and click the Environment section on the left-hand side to see your Enviroment Dashboard. Create or select the environment for our Slack bot and then deploy a container for it. Environments allow containers inside it to talk to each other through private networks, i.e. a server and a database. So in the future, if we wanted to add more functionality to our Slack bot, we may deploy additional containers to run side-by-side with it.
Now we want to make sure our container can connect to Slack’s APIs (the public internet) and have persistent storage for our configs. To allow our container to connect to the public internet, simply set Public Network to ‘enabled’. For persistent storage, Cycle’s Stateful containers automatically configures the container for persistent storage. To enable it, set the Stateful option for our container.
In order to put our application onto Cycle, we can import our docker image from Docker Hub by pressing Import New Image. We also have the option to provide Docker Hub credentials for private images.
Cycle will import and configure our image automatically. We see Cycle was able to detect volumes specified in our Dockerfile and allows us to configure how much space to allocate for the volumes. For this Slack bot, 16MB will be plenty of space.
Now we must configure our container to behave like a server by opening
port 80. This usually requires delving into Docker’s documentation and passing in the correct ports, protocols, etc. Cycle makes these configuring these options effortless through its interface.
For example, currently our toml file configures our Slack bot to listen on port 4000. Simply create a port configuration on Cycle that forwards incoming
port 80 requests to our container’s
Finally, click Create Container and our container has been created!
Our container has been created, but our Slack bot won’t run without the token stored in our config file. Cycle makes it easy to move files to our volume via SFTP:
- Enable Remote Access to our container’s volume.
- Set a password and save changes.
- Head over to the instances tab and scroll down to ‘Connected Volumes’.
- Now we can get information on how to connect and copy files to our volume.
Set a password and save changes, then head over to the instances tab and scroll down to ‘Connected Volumes’ and now we can get information on how to connect and copy files to our volume.
Finally, we deploy our Slack bot! Simply go back to our Environment and press the start button.
Now we can copy our domain url and use it in our Slack slash commands!
Now we can add a slash command to complete our Slack bot!
Things are constantly changing. We may want to add new features or fix bugs in our Slack bot. Cycle makes re-deployments very straightfoward. To update a container to use our latest Docker images, re-import the image from the Images tab on the sidebar and reimage the container under the settings tab of your container.
We started with our small Slack bot and was able to containerize it, deploy it and easily maintain it thanks to Cycle’s multi-cloud infrastructure management platform. With Cycle’s stateful containers and easy network configurations, its never been simplier to deploy containers to the cloud. I hope this article helped familiarize you more with developing, deploying and maintaining containerized applications on Cycle.
- http://slack.cycle.io/ — Cycle Slack Channel where you can ask more questions
- https://docs.cycle.io/getting-started/quick-start/ — Cycle platform documentation
- https://cycle.io/blog/2019/07/stateful-and-stateless-containers-on-Cycle/ — Article on stateful and stateless containers on Cycle
- https://github.com/kamiyaa/cycle-projects — More projects that can be deployed on Cycle
- https://github.com/nlopes/slack — Slack API used for the Slack bot
- https://api.slack.com — Slack API website for managing tokens and permissions