Understanding Docker Container Start and Local PC Synchronization

Asked 1 years ago, Updated 1 years ago, 372 views

When creating containers, you can use the -v option to synchronize local directories and files into containers, but when you stop the container once, modify the files on the local side, and start with docker container start, are the files synchronized (same state)?

Do I need any options in docker container start to synchronize and start?

FROM python
WORKDIR/tmp/mydir
COPY./ex01/tmp/mydir/
CMD ["/bin/bash" ]
docker container run-it --name py-01-v${PWD}/ex01:/tmp/mydirimg_ex01_py_01

docker

2022-11-05 19:29

1 Answers

I have written the code that the respondent can reproduce about the comment.

Please prepare the dockerfile you provided in Dockerfile and save the following on hoge.sh.

mkdir ex01
docker build-timg_ex01_py_01.
echo 'writing before the container start...'>./ex01/hoge.txt
docker container run-it --name py-01-v${PWD}/ex01:/tmp/mydirimg_ex01_py_01bash
echo 'writing after the container stopped...'>>./ex01/hoge.txt
docker container start-ipy-01
docker container rmpy-01

Please do the following from the terminal (you must enter it from the terminal in the middle):

$bash-x hoge.sh
+ mkdir ex01
+ docker build-timg_ex01_py_01.
Sending build context to Docker daemon 3.584kB
Step 1/4: FROM python
 --- >00cd1fb8bdcc
Step 2/4: WORKDIR/tmp/mydir
 --- > Using cache
 --- >9a0978729a11
Step 3/4: COPY./ex01/tmp/mydir/
 --- > Using cache
 --- >6286728cb827
Step 4/4: CMD ["/bin/bash" ]
 --- > Using cache
 --- > 95d2976d0da2
Successfully built 95d2976d0da2
Successfully tagged img_ex01_py_01:latest
+ echo 'writing before the container start...'
+ docker container run-it --name py-01-v/home/user/soj/92006/ex01:/tmp/mydirimg_ex01_py_01bash
root@682e70196520:/tmp/mydir#cat>>hoge.txt
aaaaaaaaaaaaaaaaaaaa
root@682e70196520:/tmp/mydir#exit
exit
+ echo 'writing after the container stopped...'
+ docker container start-ipy-01
root@682e70196520:/tmp/mydir#cathoge.txt
Writing before the container start...
aaaaaaaaaaaaaaaaaaaa
Writing after the container stopped...
root@682e70196520:/tmp/mydir#exit
exit
+ docker container rmpy-01
py-01
$ 

You can see that before and after the first docker container run, writing outside the docker container is reflected by the cat that resumed with docker container start.


2022-11-05 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.