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

What is data persistence like in docker

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

Share

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

This article shows you what data persistence is like in docker. It is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.

Method 1 (bind mouthing)

Let's start with an Nginx dockerfile.

FROM nginxWORKDIR /usr/nginx/htmlCOPY index.html index.html

We want to mount the working directory in the container to the data volume of our server, so as not to lose data after the container is deleted.

First we build the dockerfile into an image named saniii/my-nginx-mount

docker build -t saniii/my-nginx-mount .

run containers

docker run -d -p 3331:80 -v $(pwd):/usr/nginx/html --name nginx1 saniii/my-nginx-mount-d background run-p bind host port to container port 3331: indicates server port:80 indicates port in container-v data bind $(pwd) indicates current path:/usr/nginx/html is path in container to bind--name container named nginx 1 saniii/my-nginx-mount container name run

See if the data has been successfully bound

docker exec -it nginx1 /bin/bash interactive run container We create a file in the container, exit

Method 2 (Volume)

We can define the persistence path of our data in the container in dockerfile

https://github.com/docker-library/mysql/blob/9d1f62552b5dcf25d3102f14eb82b579ce9f4a26/5.7/Dockerfile

The above is the best practice in mysql official dockerfile. mysql runs in the container. data persistence is in the following path

command sudo docker run -d -v mysql:/var/lib/mysql --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:5.7-v Data binding names our data volume mysql /var/lib/mysql Path of the data volume in the container-e MYSQL_ALLOW_EMPTY_PASSWORD=true Set mysql No password

Running Volume docker volume ls View Volume persistent address on host docker volume inspect mysql

Verify that the data is successfully persisted Interactive Run Container docker exec -it mysql1 /bin/bash

Login to mysql View database mysql-uroot-hlocalhost-p

We create a database

Exit the container and delete.

docker rm -f mysql1 -f Force deletion of running containers

We start a mysql container again, using the previous Volume mysql

sudo docker run -d -v mysql:/var/lib/mysql --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:5.7

We found that the data still existed.

This way we can make the data persistent.

The above is what data persistence looks like in docker. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

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