Using docker to run tweepy (Python library) to collect tweets cause 403 Error forbidden

Hello everyone,
I’m new to docker but I have created a Python script that gather tweets from twitter based on the Hashtag name.
I have created a Docker file and run it as follows:
This is my Dockerfile:

FROM python:3.6

RUN pip install tweepy

RUN pip install pymongo
RUN pip install asyncio

ADD tweepy_twitter_stream_v0.6.py /


CMD [ "python", "./tweepy_twitter_stream_v0.6.py" ]

tweepy_twitter_stream_v0.6.py is my python script that I’m trying to run on docker image. the file searches for hashtags and insert the tweet into mongoDB:

Send a request to Twitter’s server and save the data on tweepy iterator using hashtag_name.

for tweet in tweepy.Cursor(api.search,q=hashtag_name,count=100,wait_on_rate_limit=True ,wait_on_rate_limit_notify= True).items():
        # Convert tweepy object into json format 
        tweet_as_json = tweet._json  
        # Insert tweet to mongoDB 
        tweets.insert_one(tweet_as_json)

It works perfectly fine when I run it on Jupyter notebook how ever, when I try to run it using dockerfile I get the following error message:

    for tweet in tweepy.Cursor(api.search,q=hashtag_name,count=100,wait_on_rate_limit=True ,wait_on_rate_limit_notify= True).items():
  File "/usr/local/lib/python3.6/site-packages/tweepy/cursor.py", line 49, in __next__
    return self.next()
  File "/usr/local/lib/python3.6/site-packages/tweepy/cursor.py", line 197, in next
    self.current_page = self.page_iterator.next()
  File "/usr/local/lib/python3.6/site-packages/tweepy/cursor.py", line 108, in next
    data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
  File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 250, in _call
    return method.execute()
  File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 234, in execute
    raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: Twitter error response: status code = 403

This is how I use docker:

  • build the docker file
  • run the docker image

both went well and I got the image name. then I ran this command in the Docker toolbox command line:

docker run f384c515d331 

f384c515d331 is the image name. I still get the same error message.

Please help this is really frustrating ;(