|
1 | | -Notes coming soon. |
| 1 | +## Types of Docker volumes |
| 2 | +There are two type of volumes in Docker. |
| 3 | +The first is a Docker bind mount which is a path from the host machines directory structure to a mount point inside the container. |
| 4 | +The second is a Docker named volume. This is a managed mount point in Docker which allows the attachment from multiple containers. |
2 | 5 |
|
3 | | -[Edit these notes](https://github.com/howToCodeWell/course-notes) |
| 6 | +## How to create a bind mount |
| 7 | + |
| 8 | +To create a bind mount use the `-v` argument when running a container. This argument has two parts seperated by a `:`. |
| 9 | + |
| 10 | +```bash |
| 11 | +-v <HOST_DIR>:<CONTAINER_DIR> |
| 12 | +``` |
| 13 | +The left hand-side of the `:` is the directory structure from the host machine. |
| 14 | +The right hand-side of the `:` is the mount point of the container. |
| 15 | + |
| 16 | +To mount the host machines home directory into a new directory called data on the container run the following |
| 17 | +```bash |
| 18 | +$ docker run -it -v ~/home/peterfisher:/data ubuntu bash |
| 19 | +``` |
| 20 | + |
| 21 | +## How to crate a Docker volume |
| 22 | +To create a Docker volume when running a container use the `-v` argument like the bind example but instead of supplying a path from the host machine pass a name of the volume that you wish to create, |
| 23 | +```bash |
| 24 | +-v <VOLUME_NAME>:<CONTAINER_DIR> |
| 25 | +``` |
| 26 | +To create a Docker volume called `data` which is mounted to the container at the path `/data` run the following |
| 27 | + |
| 28 | +```bash |
| 29 | +$ docker run --name test1 -it -v data:/data ubuntu bash |
| 30 | +``` |
| 31 | +A named Docker volume can also be attached to another container at a different mount point. |
| 32 | +The following example attaches the same data Docker volume to a new container called `test2`. The Docker volume is mounted at the path `/data/store` in the container. |
| 33 | +```bash |
| 34 | +$ docker run --name test2 -it -v data:/data/store ubuntu bash |
| 35 | +``` |
| 36 | +## To list Docker volumes |
| 37 | +To list all the docker volumes on the hostmachine run the following |
| 38 | +```bash |
| 39 | +$ docker volume list |
| 40 | +``` |
| 41 | + |
| 42 | +## To create a Docker volume |
| 43 | +To create a Docker volume without create or running a Docker container run the following |
| 44 | +```bash |
| 45 | +$ docker volume create <VOLUME_NAME> |
| 46 | +``` |
| 47 | + |
| 48 | +## How to attach volumes from another container |
| 49 | +To mount volumes that are attached from other containers pass the `--volumes-from` argument when creating a new container. |
| 50 | +The above command could be simplified by replacing the `-v` argument with the `--volumes-from` argument. Please note that both the name of the Docker volume and the original mount point inside the container will be copied. |
| 51 | +```bash |
| 52 | +$ docker run --name test2 -it --volumes-from test1 ubuntu bash |
| 53 | +``` |
| 54 | + |
| 55 | +## How to inspect volume configuration |
| 56 | +To inspect the mounts of a container run the following command and look for the mounts section in the JSON output. |
| 57 | +```bash |
| 58 | +$ docker inspect <CONTAINER_NAME_OR_ID> |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | +To inspect the configuration of a Docker volume run the following |
| 63 | +```bash |
| 64 | +$ docker volume inspect <VOLUME_NAME> |
| 65 | +``` |
| 66 | +This will show the name of the volume, the driver and the mount point of the container. |
0 commit comments