parent
dfef66f4b5
commit
1b79141b47
16 changed files with 16 additions and 109 deletions
@ -1,47 +1,3 @@ |
||||
# Data Persistence in Docker |
||||
|
||||
Docker enables you to run containers that are isolated pieces of code, including applications and their dependencies, separated from the host operating system. Containers are ephemeral by default, which means any data stored in the container will be lost once it is terminated. To overcome this problem and retain data across container lifecycles, Docker provides various data persistence methods. |
||||
|
||||
In this section, we will cover: |
||||
|
||||
- [Docker Volumes](#docker-volumes) |
||||
- [Bind Mounts](#bind-mounts) |
||||
- [Docker tmpfs mounts](#docker-tmpfs-mounts) |
||||
|
||||
### Docker Volumes |
||||
|
||||
Docker volumes are the preferred way to persist data generated and utilized by a Docker container. A volume is a directory on the host machine Docker uses to store files and directories that can outlive the container's lifecycle. Docker volumes can be shared among containers, and they offer various benefits like easy backups and data migration. |
||||
|
||||
To create a volume, use the following command: |
||||
|
||||
```bash |
||||
docker volume create volume_name |
||||
``` |
||||
|
||||
To use a volume, add a `--volume` (or `-v`) flag to your `docker run` command: |
||||
|
||||
```bash |
||||
docker run --volume volume_name:/container/path image_name |
||||
``` |
||||
|
||||
### Bind Mounts |
||||
|
||||
Bind mounts allow you to map any directory on the host machine to a directory within the container. This method can be useful in development environments where you need to modify files on the host system, and those changes should be immediately available within the container. |
||||
|
||||
To create a bind mount, use the `--mount` flag with `type=bind` in your `docker run` command: |
||||
|
||||
```bash |
||||
docker run --mount type=bind,src=/host/path,dst=/container/path image_name |
||||
``` |
||||
|
||||
### Docker tmpfs mounts |
||||
|
||||
Docker tmpfs mounts allow you to create a temporary file storage directly in the container's memory. Data stored in tmpfs mounts is fast and secure but will be lost once the container is terminated. |
||||
|
||||
To use a tmpfs mount, add a `--tmpfs` flag to your `docker run` command: |
||||
|
||||
```bash |
||||
docker run --tmpfs /container/path image_name |
||||
``` |
||||
|
||||
By employing these methods, you can ensure data persistence across container lifecycles, enhancing the usefulness and flexibility of Docker containers. Remember to choose the method that best suits your use case, whether it's the preferred Docker volumes, convenient bind mounts, or fast and secure tmpfs mounts. |
Loading…
Reference in new issue