Twitter Bot Docker

Twitter Bot Docker

Python and Twitter

So far we have created a little Python app that will be our bot for Twitter. We added in all the core functionality. Then added in the ability to schedule it. This app is far from perfect but it got us started. Next, we are going to wrap this up in a Docker container.

This is quick and easy. We need to create two files for doing this. But I am going to assume you have Docker installed. If you don’t You will need to do so before you can continue. The first is the Dockerfile you can create this file in the root directory of your app. If you don’t know the Dockerfile has no file extension. When I first started with Docker this was one of those little things that got me. The next file we need is the .dockerignore this acts similar to what a .gitignore file acts. One thing to remember for both of these file types they are case sensitive and .dockerignore does needs to start with ..

Let’s look at the Dockerfile first it should look something like this:

FROM python:3.7

RUN pip install request
RUN pip install tweepy
RUN pip install schedule

RUN mkdir -p /var/RobertSircBot

WORKDIR /var/RobertSircBot

COPY ./ /var/RobertSircBot

ENTRYPOINT python /var/RobertSircBot/bot.py

If we break this down the first line:

FROM python:3.7

This set’s the specific version of Python we are using, yours might be different than mine. Next we have to get the modules we used in our app and that is with these commands:

RUN pip install request
RUN pip install tweepy
RUN pip install schedule

Then we want to create a working directory and copy what we need to it:

WORKDIR /var/RobertSircBot

COPY ./ /var/RobertSircBot

Lastly we need to set the entry point of the application:

ENTRYPOINT python /var/RobertSircBot/bot.py

This is in no way optimized. Again we are going for MVP and working our getting to that point then we can start optimizing. We will now move on to the .dockerignore this file will have a few entries to block files we don’t want to package up:

# Virtual Environment

venv/

# Project Settings

.idea/

# git

.git/

And if we run the following command to build:

docker build . -t robertsircbot

Then to run it:

docker run robertsircbot

This post is a continuation of the Twitter-Bot series. Not a terribly long post but an important one none the less.