使不同 docker 容器中的文件可以通过文件路径相互访问

Making files in different docker containers accessible to each other via file path

我已经使用 docker-compose docker 开发了一个依赖于数据库的 python 应用程序,它工作正常。 python 应用程序生成一个 powerpoint 文件,它存储在容器内的 /tmp 中。然后需要将其转换为 pdf 以供 dockerised 前端呈现。我打算使用 dockerised libreoffice 图片 https://hub.docker.com/r/domnulnopcea/libreoffice-headless/

libreoffice容器运行如下

sudo docker run -v /YOUR_HOST_PATH/:/tmp libreoffice-headless libreoffice --headless --convert-to pdf /tmp/MY_PPT_FILE --outdir /tmp

YOUR_HOST_PATH 在我的 python 应用程序容器中

我需要发生什么

我需要python应用程序调用libreoffice容器,将驻留在python应用程序容器中的ppt文件进行转换,然后将转换后的文档路径提供给前端渲染。

基本上如何使用 docker-compose

使不同 docker 容器中的文件相互访问

我的docker-compose.yaml:

version: '3'
services:
  backend:
    image: interrodata_backend
    build: ./backend
    ports:
      - "9090:9090"
    depends_on:
      - db
    environment:
      - DATABASE_HOST=db
  db:
    image: nielsen_db
    restart: always
    build: ./db

How to call commands in another container?

中,@Horgix 解释了调用驻留在另一个容器中的可执行文件的方法。对于您的情况,最干净的方法是使您的 libreoffice 容器成为一项服务,并将 HTTP API 公开到外部。然后从 Python 应用程序容器中调用此 API。

How to share files between different containers?

您可以使用卷或 bind-mounts 来实现此目的。

例如,要使用 bind-mounts:

docker run -v /host/path:/tmp python-app
docker run -v /host/path:/tmp libreoffice-headless

Python 应用程序将文件生成到它自己的 /tmp 目录中。 libreoffice 应用程序会在其自己的 /tmp 目录中找到相同的文件。他们共享同一个目录。

使用卷的想法相同。您可以找到更多信息 here.