CodeGym /Courses /Docker SELF /Basics of Data Storage in Docker

Basics of Data Storage in Docker

Docker SELF
Level 19 , Lesson 0
Available

1.1 Volumes

Storing data in Docker is an important part of working with containers. Containers themselves are ephemeral, which means the data inside them will be lost after stopping or deleting the container. To preserve data between runs and ensure long-term storage, Docker offers two main technologies: volumes and bind mounts.

Volumes

Volumes are resources managed by Docker for storing data. They exist independently of the lifecycle of containers, which allows them to be used for long-term data storage. Volumes can be created and managed by Docker, providing an easy way to store data outside of containers.

Benefits of Using Volumes

  1. Isolation from the Host: volumes are stored in a special directory on the host managed by Docker, which ensures their isolation.
  2. Ease of Management: Docker provides convenient commands for creating, deleting, and managing volumes.
  3. Compatibility and Portability: volumes can be easily moved between different hosts and used with various containers.

Example of Creating and Using Volumes

Creating a Volume:

Terminal

docker volume create my_volume

Using a Volume in a Container:

In this example, the volume my_volume is mounted into the /data directory inside the my_container container.

Terminal

docker run -d --name my_container -v my_volume:/data nginx

1.2 Bind Mounts

Bind mounts let you mount specific host directories into containers. This way, containers can use files and directories from the host, which is super handy for development and testing when you need to work with local files.

Advantages of using bind mounts:

  1. Direct access to host files: containers can directly use files and directories from the host.
  2. Convenience for development: developers can easily change files on the host and see those changes in containers in real-time.
  3. Flexibility: bind mounts can be any directories on the host, giving you more flexibility in managing data.

Example of using bind mounts

Running a container with a bind mount:

Terminal

docker run -d --name my_container -v /path/on/host:/data nginx

In this example, the directory /path/on/host on the host is mounted into the directory /data inside the container my_container.

Comparison of Volumes and Bind Mounts:

Characteristic Volumes Bind Mounts
Data Storage Managed by Docker Managed by the user
Isolation Isolated from the host's file system Use the host's file system
Performance Optimized for Docker Depends on the host file system
Applicability Long-term data storage Development and testing

1.3 Practical Examples

Practical examples of usage

Example 1: Using Volumes for a Database

To ensure long-term storage of database data, you can use volumes.

Creating a volume:

Terminal

docker volume create db_data

Running a database container using the volume:

Terminal

docker run -d --name postgres -e POSTGRES_PASSWORD=mysecretpassword -v 
db_data:/var/lib/postgresql/data postgres

Example 2: Using Bind Mounts for Web Development

For developing a web application, you can use bind mounts so that changes in local files are immediately reflected in the container.

Running a container with a bind mount:

Terminal

docker run -d --name web_dev -v $(pwd)/my_website:/usr/share/nginx/html nginx

In this example, the current working directory my_website is mounted to the directory /usr/share/nginx/html of the nginx container.

3
Task
Docker SELF, level 19, lesson 0
Locked
Creating a volume for data storage
Creating a volume for data storage
3
Task
Docker SELF, level 19, lesson 0
Locked
Using a Bound Directory for Development
Using a Bound Directory for Development
3
Task
Docker SELF, level 19, lesson 0
Locked
Storing PostgreSQL database data in a volume
Storing PostgreSQL database data in a volume
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION