Docker未在KeyboardInterrupt上保存文件 [英] Docker not saving file on KeyboardInterrupt

查看:77
本文介绍了Docker未在KeyboardInterrupt上保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm storing tweets into a CSV, when I run the following code in Jupyter Notebook it'll save to tweets.csv successfully.

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容器中运行并发出键盘中断时,它会返回

When run in a Docker container and issued a Keyboard Interrupt it returns

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

,但不保存任何内容。

but nothing is saved.

我如何使脚本在容器中写出,这也是在幕后发生的事情?

How do I get the script to write out in the container, also what is happening under the hood here?

编辑:

命令运行:

docker build -t dock .
docker run dock

这里是Dockerfile:

Here's the 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


推荐答案

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

From your docker command , it seems that you are not mounting your local directory to your docker container. Docker containers have a different filesystem from the host and files saved in docker containers are get lost once the container is removed. To fix this, run your command like this:

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

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

This commands binds your local directory that you defined before the : to the directory app inside the docker container. You should check that the local directory exists, and it should be the absolute path. As your docker container runs in /app,it will download the tweets.csv there and you should be able to see it in your /some/local/directory after it is completed. You might also see your whole codebase as you are downloading the file to the same directory with your code.

这是 Docker卷

*出于完整性考虑,这与键盘中断无关。

*Just for completeness, this has nothing to do with keyboard interrupt.

这篇关于Docker未在KeyboardInterrupt上保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆