コンテナが分離されている場合、おそらくデータを格納するために、コンテナはどのようにホストマシンと通信できますか?画像からコンテナを作成する場合、コンテナを削除すると、生成されたデータはすべて失われるためです。
したがって、永続的なストレージを用意する方法が必要です。
私たちはを使用してそうすることができますバインドマウントそしてボリューム。
バインドマウントはホストコンピューター上の任意のフォルダーを指すことができ、Dockerによって直接管理されないことを除いて、2つの間に大きな違いはありません。
それらから始めましょう。 1つの典型的な例はログです。アプリがコンテナ内にログファイルを作成するとします。/usr/src/app/logs
。を使用して、それをホストマシン上のフォルダにマップできます。-v
(と同じ--volume
)コンテナを実行するときのフラグdocker run
、 このような:-v ~/logs:/usr/src/app/logs
これにより、そのフォルダーがユーザーのホームディレクトリのlogsサブフォルダーにマップされます。
ノード:
-m
または--mount
フラグは非常によく似た方法で機能します
これはで使用されるフラグですexamplenode
以前に作成した画像:
docker run -d -p 80:3000 -v ~/logs:/usr/src/app/logs --name node-app examplenode
これで、Nodeアプリを実行できるようになり、ログはDockerコンテナー内ではなく、ホストコンピューターに保存されます。
注意してください
examplenode
アプリはログインを生成しません/usr/src/app/logs
、これは単なる例であり、最初にそのログを設定する必要があります。
ボリュームに関する詳細は、実行時に一覧表示されますdocker inspect
コンテナ名の「マウント」の下:
"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