Dương Quang Thọ
Dương Quang Thọ

Reputation: 203

Confusing about command with docker exec

I am confused when I use commands with docker exec as below.

# 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

When I use command echo without attaching with /bin/sh, it don't affect my file in container. Anyone, please help me!

Upvotes: 0

Views: 184

Answers (1)

EchoMike444
EchoMike444

Reputation: 1692

When you create a container , you file system is not the same .

/tmp/ in your docker is not the same that the /tmp/ on your system .

when you run

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

you run sh with one 2 parameter -c and "echo 2 >> /tmp/1.txt" , your sh is running inside the container

when you run

docker exec -it testing echo 2 >> /tmp/1.txt

you run echo with one parameter 2 and ** '>> /tmp/1.txt'** is at the same context that your docker command so outside your container .

Upvotes: 1

Related Questions