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

con*_*449 5 python docker dockerfile

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

我的 Dockerfile 如下所示:

FROM python:3
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
CMD [ "python", "process.py" ]
Run Code Online (Sandbox Code Playgroud)

理想情况下,终端命令应该是这样的:

docker run -p 5000:5000 [name of docker] [local path to input file] [local path to save output file]
Run Code Online (Sandbox Code Playgroud)

当我运行时,我收到此错误:

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.
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Neo*_*son 3

一般情况下,docker容器是无法突围到宿主机的。

\n

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

\n

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

\n
// The initial wiorking directory content\n.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 input-file\n\n// Run my dummy container and ask it to cat the content of the input file into the output file\ndocker run -v $(pwd):/root/some-path ubuntu /bin/bash -c "cat /root/some-path/input-file >> /root/some-path/output-file"\n\n// The outcome\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 input-file\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 output-file\n\n
Run Code Online (Sandbox Code Playgroud)\n