docker生产环境中的文件写入失败



在我的生产环境中,我无法写入文件。例如,我用Celery设置了一个测试任务,它每分钟将时间写入一个文件:

@celery_app.task(name='print_time')
def print_time():
now = datetime.datetime.now().strftime('%Y %b %d %a @%H:%M')
cur_time = {"now": now}
print(f'The date and time sent: {cur_time}')
json.dump(cur_time, open(PATH.abspath(PATH.join(APP_DIR, "data", "cur_time.json")), "w"))
t = json.load(open(PATH.abspath(PATH.join(APP_DIR, "data", "cur_time.json"))))
print(f'The date and time received: {t}')

这两份打印声明都将给出预期的结果,截至我写这篇文章时,它们最后一次打印:

The date and time sent: {'now': '2021 May 26 Wed @18:57'}
The date and time received: {'now': '2021 May 26 Wed @18:57'}

然而,当我设置一个视图来显示内容时:

class TimeView(TemplateView):
def get_context_data(self, **kwargs):
time = json.load(open(PATH.abspath(PATH.join(APP_DIR, "data", "cur_time.json"))))
return time

很明显,当我转到url时,文件并没有在开发环境中真正更新,时间仍然与我最初从开发环境中同步文件时保持不变(成功更新了文件内容(

为了进一步验证这一点,我还运行了cat cur_time.jsonstat cur_time.json来验证文件是否未成功写入。

知道这些文件没有更新,我的问题有两个。第一,为什么我在芹菜任务中的打印语句打印结果,就好像文件正在更新一样?第二,这个问题最可能的原因和解决方案是什么?

我认为这与我的Docker容器文件写入权限有关,但我已经通过运行chmod -R 777 data更改了数据目录中的写入权限。此外,我还没有收到任何权限错误消息,这些消息似乎是在权限问题出现时抛出的。我开始达到我的知识极限,想知道是否有人知道问题/解决方案是什么。谢谢

编辑以回应评论:

我正在使用docker compose。这是我的产品.yml文件:

version: '3'
volumes:
production_postgres_data: {}
production_postgres_data_backups: {}
production_traefik: {}
services:
django: &django
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: myapp_production_django
depends_on:
- postgres
- redis
env_file:
...
command: /start
postgres:
...
traefik:
...
redis:
image: redis:5.0
celeryworker:
<<: *django
image: myapp_production_celeryworker
command: /start-celeryworker
celerybeat:
<<: *django
image: myapp_production_celerybeat
command: /start-celerybeat
flower:
<<: *django
image: myapp_production_flower
command: /start-flower

回应评论的第二次编辑:

这是我的local.yml文件的视图

version: '3'
volumes:
local_postgres_data: {}
local_postgres_data_backups: {}
services:
django: &django
build:
context: .
dockerfile: ./compose/local/django/Dockerfile
image: myapp_local_django
container_name: django
depends_on:
- postgres
volumes:
- .:/app:z
env_file:
...
ports:
- "8000:8000"
command: /start
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: myapp_production_postgres
container_name: postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data:Z
- local_postgres_data_backups:/backups:z
env_file:
...
redis:
image: redis:5.0
container_name: redis
celeryworker:
<<: *django
image: myapp_local_celeryworker
container_name: celeryworker
depends_on:
- redis
- postgres
ports: []
command: /start-celeryworker
celerybeat:
<<: *django
image: myapp_local_celerybeat
container_name: celerybeat
depends_on:
- redis
- postgres
ports: []
command: /start-celerybeat
flower:
<<: *django
image: myapp_local_flower
container_name: flower
ports:
- "5555:5555"
command: /start-flower

在到期时给予信贷。@IainShelvington在上面的评论中优雅地提出了这个问题和解决方案。

问题原因:;除非装载卷并写入该卷,否则在docker容器中写入的任何文件都不会写入主机">

问题的解决方案:;将新卷添加到全局";卷:";在您的撰写配置中。将该卷装入";django";服务,所有的芹菜服务都继承自该服务,因此应该共享它。从您安装的位置写入和读取文件(这应该与应用程序安装完全不同,比如"/celecer-logs"或其他什么(;

为了在我的具体示例中演示这个解决方案,我在production.yml文件中添加了以下内容:

volumes:
...
production_celery: {}
services:
django: &django
build:
...
image: myapp_production_django
depends_on:
...
volumes:
- production_celery:/app/celerydata:z
env_file:
...
command: /start

然后,从我的芹菜脚本派生的所有数据文件都被发送到标题为";celerydata";

正如评论中提到的,我的应用程序以前依赖于APScheduler,我已经习惯了快速将数据文件写入主机,并能够轻松地浏览它们。为了再次在主机上查看它们,作为安全预防措施(数据冗余(,我开始使用以下命令序列将文件从celerydata目录复制到我的本地机器中,在那里我可以通过图形界面轻松地查看它们:

docker ps # note container_id == ${CID} below
export CID=foobarbaz123
docker cp ${CID}:/app/celerydata ./celery_storage

在将来的某个时候,我可能会将其制作成一个脚本,在启动容器时运行,并相应地更新答案。

最新更新