Nếu các thùng chứa bị cô lập, làm thế nào chúng có thể giao tiếp với máy chủ, có lẽ để lưu trữ dữ liệu? Bởi vì khi chúng tôi tạo vùng chứa từ một hình ảnh, mọi dữ liệu được tạo sẽ bị mất khi vùng chứa bị xóa.
Vì vậy, chúng ta cần một cách để có bộ nhớ vĩnh viễn.
Chúng ta có thể làm như vậy bằng cách sử dụngGắn kếtvàTập.
Không có nhiều sự khác biệt giữa hai loại, ngoại trừ Bind Mounts có thể trỏ đến bất kỳ thư mục nào trên máy chủ và không được Docker quản lý trực tiếp.
Hãy bắt đầu với chúng. Một ví dụ cổ điển là nhật ký. Giả sử ứng dụng của bạn tạo một tệp nhật ký, bên trong vùng chứa, trong/usr/src/app/logs
. Bạn có thể ánh xạ nó vào một thư mục trên máy chủ, bằng cách sử dụng-v
(giống như--volume
) gắn cờ khi bạn chạy vùng chứa vớidocker run
, như thế này:-v ~/logs:/usr/src/app/logs
Thao tác này sẽ ánh xạ thư mục đó tới thư mục con nhật ký trong thư mục chính của người dùng.
Node: cái
-m
hoặc là--mount
cờ hoạt động theo cách rất giống nhau
Đây là cờ được sử dụng vớiexamplenode
hình ảnh chúng tôi đã tạo trước đây:
docker run -d -p 80:3000 -v ~/logs:/usr/src/app/logs --name node-app examplenode
Vì vậy, bây giờ chúng ta có thể chạy ứng dụng Node của mình và bất kỳ nhật ký nào sẽ được lưu trữ trong máy tính chủ, thay vì bên trong vùng chứa Docker.
Lưu ý rằng
examplenode
ứng dụng không tạo bất kỳ đăng nhập nào/usr/src/app/logs
, nó chỉ là một ví dụ và bạn sẽ cần thiết lập việc ghi nhật ký đó trước.
Thông tin chi tiết về khối lượng sẽ được liệt kê khi bạn chạydocker inspect
trên tên vùng chứa, trong "Gắn kết":
"Mounts": [
{
"Type": "bind",
"Source": "/Users/flavio/logs",
"Destination": "/usr/src/app/logs",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],Can you see "Type": "bind"
? That means that we have created a bind mount.
Now, let’s talk about Volumes.
The difference between Bind Mounts and Volumes is that by creating volumes, Docker will store the data in a folder it manages, which means it will take care of file permissions and ownership, and it will give you the tools to manage those volumes. While bind mounts are based on filesystem paths, and Docker can’t provide the tooling around them.
For example, Docker lets you remove all unused volumes by running docker volume prune
or docker system prune --volumes
.
To create a volume, we first need to run docker volume create
:
docker volume create logs
Now you can use docker volume ls
and docker volume inspect
to get more data about the system volumes:

Now run docker run
with the option -v logs:/usr/src/app/logs
(tell the volume name instead of a folder)
docker run -d -p 80:3000 -v logs:/usr/src/app/logs --name node-app examplenode
Now running docker inspect
on the image will show the mounted volume:
"Mounts": [
{
"Type": "volume",
"Name": "logs",
"Source": "/var/lib/docker/volumes/logs/_data",
"Destination": "/usr/src/app/logs",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],See? Now the logs will be stored in the /var/lib/docker/volumes/logs/_data
folder.
Volumes will be essential when it will be time to deploy a container on a cloud service, for example.
You can remove a volume running docker volume rm <name>
.
More docker tutorials:
- Introduction to Docker
- Introduction to Docker Images
- Introduction to Docker Containers
- Dockerfiles
- Installing Docker on macOS
- First steps with Docker after the installation
- Using Docker Desktop to manage a Container
- Create a simple Node.js Hello World Docker Container from scratch
- What to do if a Docker container immediately exits
- Working with Docker Containers from the command line
- Working with Docker Images from the command line
- Sharing Docker Images on Docker Hub
- How to access files outside a Docker container
- How to commit changes to a Docker image
- Updating a deployed container based on a Docker image