5

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

./program data_file config_file

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"]

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

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.

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).

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 version is 18.04.0-ce, build 3d479c0af6.

EDIT

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

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

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

CMD ["./launch.sh"]

But now if I run:

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

I get a permission error...

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

elcortegano
  • 2,444
  • 11
  • 40
  • 58
  • 1
    Possible duplicate of [Passing file as argument to Docker container ](https://stackoverflow.com/questions/41092587/passing-file-as-argument-to-docker-container) – Manish Yadav May 12 '18 at 19:03
  • Its not duplicate if it has not an answer, read it in the flag description – elcortegano May 12 '18 at 19:04
  • You might want to use volume to mount file between your host and container. For example, `docker run -v /path/to/file:/program/ test file3 file4` – Manish Yadav May 12 '18 at 19:12

1 Answers1

6

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

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.

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.

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 

It works for me!

Alexcei Shmakov
  • 2,203
  • 3
  • 19
  • 34
  • This also generates an error. When I create a `~/tmp` directory and set there file1 and file2, I run `docker run -v /home/user/tmp/:/program test file1 file2` and get the following: **docker run -v ~/tmp/:/program test file1 file2 docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./program\": stat ./program: no such file or directory": unknown. ERRO[0001] error waiting for container: context canceled** – elcortegano May 12 '18 at 20:10
  • @EuGENE What do you get when you run `docker run -v /home/user/tmp/:/program test` ? – Manish Yadav May 13 '18 at 02:30
  • The same error is generated: **docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./program\": stat ./program: no such file or directory": unknown. ERRO[0000] error waiting for container: context canceled** – elcortegano May 13 '18 at 08:07
  • @EuGENE, I found the problem. I apologize for my inattention in first. – Alexcei Shmakov May 13 '18 at 09:09
  • Nothing to apologize. In fact, I'm in your debt, thank you very much!! Now it works – elcortegano May 13 '18 at 09:26