使用Docker读取输入文件 [英] Reading input files with docker

查看:315
本文介绍了使用Docker读取输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个按如下方式运行的C ++程序:

I'm writting a C++ program that is run as follows:

./program data_file config_file

我想使用docker。我编写了以下 Dockerfile

And I want to use docker with it. I've written the following Dockerfile:

FROM gcc:7.2.0
ENV MYP /repo
WORKDIR ${MYP}
COPY . ${MYP}

RUN /bin/sh -c 'make'
ENTRYPOINT ["./program"]
CMD ["file1", "file2"]

所以我 docker build -t test。使用 docker run test 并查看默认设置一切正常。

So I docker build -t test . it and use docker run test and see that everything goes fine with the defaults.

但是,如果我修改了<$ c例如,在工作目录(构建后)中的$ c> file1 中,我注意到当我运行 docker运行测试文件1 file2 时, file1 是容器中的一个 ,而作为参数输入的一个将被忽略。

However, if I modify file1 for instance in my working directory (after build), I note that when I run docker run test file1 file2, the file1 called is the one inside the container, and the one entered as argument is being ignored.

类似地,如果我使用重命名的数据和配置文件,并运行 docker run test file3 file4 ,则会收到一条错误消息,指出这些文件不存在(因为它们不存在)

Similarly, If I use renamed data and config files, and run docker run test file3 file4, I get an error saying that these files does not exist (because they are not in the container).

所以,如何使docker识别这些作为参数传递的输入文件,避免使用包含的文件

So, how can I do to make docker recognize these input files passed as arguments, avoiding to use the files that are contained in the image?

Docker版本为18.04.0-ce,构建3d 479c0af6。

Docker version is 18.04.0-ce, build 3d479c0af6.

EDIT

另一种选择是使用 launch.sh 脚本,例如:

Another option is to use a launch.sh script like:

#!/bin/sh
./program "${DATA_FILE}" "${CONFIG_FILE}"

所以 ENTRYPOINT CMD 指令在Dockerfile中替换为以下行:

So ENTRYPOINT and CMD instructions are replaced by the following line in the Dockerfile:

CMD ["./launch.sh"]

但是现在如果我运行:

docker run test -e DATA_FILE=file1 -e CONFIG_FILE=file2

我遇到权限错误...

I get a permission error...


docker :来自守护程序的错误响应:OCI运行时创建失败:container_linux.go:348:启动容器进程导致 exec:\ -e\:在$ PATH中找不到可执行文件:未知。
ERRO [0000]错误,正在等待容器:上下文已取消

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"-e\": executable file not found in $PATH": unknown. ERRO[0000] error waiting for container: context canceled


推荐答案

您可以使用音量机制将主机目录安装到容器。

You can use volume mechanism for mount a host directory to the container.

在以下目录中的某些目录中创建 file1.txt file2.txt 文件主机。例如,我们在 / var / dir1 / 目录中创建文件。

您在docker容器中的工作目录为 / program

Create file1.txt and file2.txt files in some directory in the host machine. For example we are creating files in the /var/dir1/ directory.
Your working dir in the docker container is /program.

因此,当您运行docker时,应使用-v标志

So when you are running docker you should mount this host directory to container using -v flag

docker run -v /var/dir1/:/program test file1.txt file2.txt

哦,我知道为什么会有问题。

Oh, I know why we have problem.

当我将主机目录安装到工作目录时,实际上是从工作目录中删除容器中所有创建的文件,包括。 / program 二进制文件。因此 docker run 命令失败。

因此我们不必挂载工作目录。例如,我们可以挂载工作目录的子目录。

When I am mounting the host directory to working dir I am actually deleting all created files inside container from it working dir including the ./program binary file. So the docker run command is failed.
therefore we not must mount working directory. We can mount subdirectory of working directory for example.

 docker run -v /var/dir1/:/program/dir test dir/file1.txt dir/file2.txt 

对我有用!

这篇关于使用Docker读取输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆