๐ฏ What's target?
Sometimes we neeeds share and backup & restore among multiple containers.
It's time to use volume.
Volume path according to Host OS
Host OS | path |
Linux | /var/lib/docker (standard) |
Windows | ?? (VM) |
Mac | ~/Libfrary/Containers/com.docker.docker/Data/vms/0/tty (VM) |
Windows, Mac both use Virtual machine based on linux since docker use default linux mechanism
Step by Step using data container backup and restore
1. Create your volume
# create volume with explicit name
# In this example, use the name of 'shared-volume'
$ docker volume create shared-volume
2. Data container with volume mapping
# make data container mapping the volume you created.
# In this example, use '/data-volume'
$ docker run -it --name data-container \
-v shared-volume:/data-volume \
ubuntu:22.04
Check it what it be made successfully
# 'data-volume' directory is created in container
$ ls
bin data-volume etc lib lib64 media opt root sbin sys usr
boot dev home lib32 libx32 mnt proc run srv tmp var
3. make other container using '--volumes-from' option
--volumes-from
Use this for mount volumes from data container which you have already made.
# --voluems-from [container-name] (you want to mount)
$ docker run -it --name container1 \
> --volumes-from data-container \
> ubuntu:20.04
Check it what it be made successfully and share the same volume between data-container and container1
root@2e26227154cc:/# ls
bin data-volume etc lib lib64 media opt root sbin sys usr
boot dev home lib32 libx32 mnt proc run srv tmp va
Share the volume only 'data-volume' between multiple containers
4. create file in shared volume
# container1
root@2e26227154cc:/data-volume# echo 'test1' > test1.txt
root@2e26227154cc:/data-volume# ls
test1.txt
# container2
root@a3d7e231e702:/data-volume# ls
test1.txt
volume is alive! even though data container is removed.
# remove containers except the container2
$ docker container rm data-container container1
The file 'test1.txt' is remain after removed other containers
# container2
root@a3d7e231e702:/data-volume# ls
test1.txt
Remove volumes
A Docker data volume persists after a container is deleted. There are two types of volumes to consider:
- Named volumes have a specific source from outside the container, for example awesome:/bar.
- Anonymous volumes have no specific source so when the container is deleted, instruct the Docker Engine daemon to remove them.
- [Docker volume documents]
Summary in picture
Volume vs Bind mount
Volume | Bind Mount | |
Why to use | Share persistent data | |
When to use | Share data among containers ex) DB data |
- Share data between host os and container - Edit on host and reflect in container ex) Source code |
Rebuild required? | True | False |
Use anonymous volume only when you need prioritizing docker internal path than external path. (Host OS)
For example, set anonymous volume on `node_modules` per each app container which have separately
๐ Conclusion
Usually use named volume with -v option
{volume-name}:{container-volume-path}
๐ Reference
'๊ธฐํ > Docker' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Docker] Docker network for communication among containers (0) | 2023.04.23 |
---|---|
[Docker] Host to container network (0) | 2023.04.19 |
[Docker] volume ์ฌ์ฉํ (0) | 2022.05.18 |
[Docker] Docker compose (0) | 2021.12.20 |
Docker ๊ฐ๋ (0) | 2021.12.18 |