Docker未在KeyboardInterrupt

时间:2018-08-27 02:38:01

标签: python docker

我将推文存储到CSV中,当我在Jupyter Notebook中运行以下代码时,它将成功保存到tweets.csv中。

with open(fName, 'a') as f:
while True:
    try:
        if (max_id <= 0):
            # to the beginning of twitter time
            if (not sinceId):
                results = api.search(q=query, count=tweetCount)
            # go to last tweet we downloaded
            else:
                results = api.search(q=query, since_id=sinceId, count=tweetCount)
        # if max_id > 0 
        else:
            # results from beginning of twitter time to max_id
            if (not sinceId):
                results = api.search(q=query, max_id=str(max_id - 1), count=tweetCount)
            # results from since_id to max_id
            else:
                results = api.search(q=searchQuery, count=tweetCount,
                                     max_id=str(max_id - 1),
                                     since_id=sinceId)
        if not results:
            print("No more tweets found")
            break
        for result in results:
            tweets_DF = pd.DataFrame({"text": [x.text for x in results]}, 
               index =[x.id for x in results])
            tweets_DF.name = 'Tweets'
            tweets_DF.index.name = "ID"
            tweets_DF.to_csv(f, header=False)
        tweetCount += len(results)
        print("Downloaded {0} tweets".format(tweetCount))
        max_id = results[-1].id
    except (KeyboardInterrupt, SystemExit):
        print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, os.path.abspath(fName)))
        quit()
    except tweepy.TweepError as e:
        print("Error : " + str(e))
        break

在Docker容器中运行并发出键盘中断时,它会返回

Downloaded 520 tweets, Saved to /app/tweets.csv

,但不保存任何内容。

如何获取脚本以将其写出到容器中,以及这里发生的事情?

编辑:

命令运行:

docker build -t dock .
docker run dock

这是Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
    enter code here

1 个答案:

答案 0 :(得分:2)

从docker命令中,您似乎没有将本地目录挂载到Docker容器中。 Docker容器的文件系统与主机不同,一旦删除容器,保存在Docker容器中的文件将丢失。要解决此问题,请像这样运行您的命令:

docker run  -v /User/Seth/some/local/directory:/app/ dock

此命令将您在:之前定义的本地目录绑定到docker容器内的目录app。您应该检查本地目录是否存在,并且它应该是绝对路径。当您的Docker容器在/app中运行时,它将在其中下载tweets.csv,完成后您应该可以在/some/local/directory中看到它。将文件下载到与代码相同的目录中时,您可能还会看到整个代码库。

这是Docker volumes

的链接

*为了完整起见,这与键盘中断无关。