将命令与docker exec混淆

将命令与docker exec混淆,docker,Docker,当我对docker exec使用命令时,我感到困惑,如下所示 # docker exec -it testing cat /tmp/1.txt 1 ... # docker exec -it testing echo 2 >> /tmp/1.txt # docker exec -it testing cat /tmp/1.txt 1 ... # docker exec -it testing /bin/sh -c "echo 2 >> /tmp/1.txt" # dock

当我对docker exec使用命令时,我感到困惑,如下所示

# docker exec -it testing cat /tmp/1.txt
1
...
# docker exec -it testing echo 2 >> /tmp/1.txt
# docker exec -it testing cat /tmp/1.txt
1
...
# docker exec -it testing /bin/sh -c "echo 2 >> /tmp/1.txt"
# docker exec -it testing cat /tmp/1.txt
1
2

当我使用命令echo而不附加/bin/sh时,它不会影响容器中的文件。任何人,请帮帮我

创建容器时,文件系统不同

docker中的/tmp/与系统中的/tmp/不同

当你跑的时候

 docker exec -it testing /bin/sh -c "echo 2 >> /tmp/1.txt"
docker exec -it testing echo 2 >> /tmp/1.txt
使用一个2参数-c“echo 2>/tmp/1.txt”运行sh,您的sh在容器内运行

当你跑的时候

 docker exec -it testing /bin/sh -c "echo 2 >> /tmp/1.txt"
docker exec -it testing echo 2 >> /tmp/1.txt
使用一个参数2运行echo,并且**'>>/tmp/1.txt'**与docker命令在容器外部的上下文相同