Docker输入文件并保存在输出中

2024-05-14 06:06:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我构建了一个docker映像,它输入一个本地文件,对它执行一些操作,然后返回一个本地保存的输出文件,但它不起作用。如何允许本地用户输入文件,然后将输出保存到本地计算机上

我的Dockerfile如下所示:

FROM python:3
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
CMD [ "python", "process.py" ]

理想情况下,终端命令如下所示:

docker run -p 5000:5000 [name of docker] [local path to input file] [local path to save output file]

当我运行时,会出现以下错误:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"../test.flac\": stat ../test.flac: no such file or directory": unknown.

我该怎么做


Tags: 文件topathdockertesttxtapplocal
1条回答
网友
1楼 · 发布于 2024-05-14 06:06:55

通常,docker容器无法突入主机

但是,您可以将本地目录从主机装载到容器中。在容器内的装载点中创建的文件也将在主机上可见

在下面的示例中,我从容器内的主机装载工作目录。我的当前目录包含一个input-file
容器cats保存input-file的内容并将其附加到output-file

// The initial wiorking directory content
.
└── input-file

// Run my dummy container and ask it to cat the content of the input file into the output file
docker run -v $(pwd):/root/some-path ubuntu /bin/bash -c "cat /root/some-path/input-file >> /root/some-path/output-file"

// The outcome
.
├── input-file
└── output-file

相关问题 更多 >