-2

I found that if I use two commands, the second command cann't read the file in volume, (while the first can read it.) like that:

[root@iZu51 test]# echo 'hello world' >> /data/test/a.txt
[root@iZu51 test]# docker run --rm -v /data/test:/data debian:stretch-slim cat /data/a.txt
hello world
[root@iZu51 test]# docker run --rm -v /data/test:/data debian:stretch-slim cat /data/a.txt && cat /data/a.txt
hello world
cat: /data/a.txt: No such file or directory
[root@iZu51 test]# docker run --rm -v /data/test:/data debian:stretch-slim cat /data/a.txt; cat /data/a.txt
hello world
cat: /data/a.txt: No such file or directory

How to fixed it? or it's a bug

Wei
  • 451
  • 1
  • 5
  • 15

2 Answers2

1

No, this isn't a bug in Docker. You're just using it wrong. Specifically:

  1. You're not quoting the ; or &&, so your outer shell is interpreting them and Docker isn't seeing them.
  2. Docker interprets the arguments to docker exec as the name of a program then its arguments, not as an arbitrary shell thing. If you want the inner shell to parse them, then you need to wrap them with sh -c or something like it.
0

It is because you are running a command into docker and the second is outside of docker

Try to use bash -c or sh -c depending on your sehll

docker run --rm -v /data/test:/data debian:stretch-slim bash -c 'cat /data/a.txt && cat /data/a.txt'
callmemath
  • 8,185
  • 4
  • 37
  • 50