Py学习  »  docker

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

user • 3 年前 • 1257 次点击  

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

@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.json stat cur_time.json 验证文件是否未成功写入。

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

我以为这与我的Docker containers文件写入权限有关,但我已经通过运行 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

第二次编辑回复评论:

这是我的本地照片。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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133316
 
1257 次点击  
文章 [ 1 ]  |  最新文章 3 年前