Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand docker persistent storage and data sharing

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to understand docker persistent storage and data sharing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand docker persistent storage and data sharing.

The first way to persist data: Data Volume

Use Data Volume:

First define Volume ["/ var/lib/mysql"] in Dockerfile. This path is the path in the container, and the data file is stored under this path.

Map this path to the host's hard disk, using the-v option

Docker run-v mysql:/var/lib/mysql

We use Data Volume persistence because our container is a place where data sources are generated, generating files and data itself, and we don't want our files and data to disappear as the container disappears, so we persist in this way.

The second way of data persistence Bind Mounting

The difference between this approach and the first one is:

Data Volume needs to define the Volume to be created in Dockerfile, but Bink Mounting does not need it. We just need to specify the corresponding relationship between the local directory and the directory in the container when starting the container:

Docker run-v / home/aaa:/root/aaa

In this way, the local directory files are synchronized with the files in the container, and if the local files are modified, the files in the container will also be modified.

Create a Dockerfile with the following content:

FROM nginx:latestWORKDIR / usr/share/nginx/htmlCOPY index.html index.html

This can copy the local index.html to / usr/share/nginx/html in the container.

The index.html is as follows:

Hello docker!

Perform build:

Ocker build-t vincent/my-nginx .Sending build context to Docker daemon 3.072kBStep 1, 3: FROM nginx:latest-> 719cd2e3ed04Step 2, 3: WORKDIR / usr/share/nginx/html-- > Using cache-> ebda8a0ae3aaStep 3, 3: COPY index.html index.html-> e3e9c5946773Successfully built e3e9c5946773Successfully tagged vincent/my-nginx:latest

Create a container:

Docker run-d-p 80:80-name web vincent/my-nginx2cc59b89c8f4c2d60c65ad5b7490fec11c727f38a30c72a0d777209e2ea2b987

Verify:

Curl 127.0.0.1hello docker!

It means you can access it!

Delete this container:

Docker rm-f web

Recreate the container and add the parameter-v to map a local file to it:

Docker run-d-v $(pwd): / usr/share/nginx/html-p 80:80-- name web vincent/my-nginxec423f40c66477471f38d8bc8a1002a1767e99d810b69b4c991446b9ced5fdea

$(pwd) represents the current path.

Go to the container and create a file called test.txt:

Docker exec-it web / bin/bashroot@ec423f40c664:/usr/share/nginx/html# lsDockerfile index.htmlroot@ec423f40c664:/usr/share/nginx/html# touch test.txt

After exiting, you can see the file test.txt on the host. Modify the contents of the file, you can modify the contents of the host synchronously, indicating that the file has been synchronized.

Because the directory $(pwd) of our host is synchronized with the directory / usr/share/nginx/html in the container.

At this point, I believe you have a deeper understanding of "how to understand docker persistent storage and data sharing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report