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 use docker container data volume

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use docker container data volumes". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Container data volume what is a container data volume

A Review of docker's ideas

Package the application and environment into a mirror image!

data? If the data is in the container, then we delete the container, then the data will be lost! Requirements: data can be persisted

Such as MySQL, if the container is deleted, it will run away! Requirements: MySQL data can be stored locally!

There can be a technology for data sharing between containers! The data generated in the Docker container is synchronized locally!

This is the roll technology! In fact, it is the mount of the directory, mount the directory in our container to Linux!

To sum up: container persistence and synchronization! Data can also be shared between containers!

Use data Volum

Method 1: directly use the command to mount-v

Docker run-it-v host directory: directory within the container

# Test

[root@dockertest] # docker run-it-v / home/ceshi:/home centos / bin/bash

# after startup, we can view the mount information through the docker inspect container id

one

one

Synchronization of test files

Let's test it again!

1. Stop the container

2. Modify the file on the host

3. Start the container

4. The data in the container is still synchronized!

Benefit: we only need to modify it locally in the future, and it will be synchronized automatically in the container!

Practice: install MySQL

Consider: the problem of data persistence in MySQL!

# get an image

[root@dockertest network-scripts] # docker pull mysql:5.7

# run the container. Need to do data mount! # to install and launch mysql, you need to configure a password, which is a point to pay attention to.

# official test: docker run-- name some-mysql-e MYSQL_ROOT_PASSWORD=my-secret-pw-d mysql:tag

# start our

-d running in the background

-p port mapping

-v data volume mount

-e environment configuration

-- name container name

[root@dockertest network-scripts] # docker run-d-p 3310 MYSQL_ROOT_PASSWORD=123456 3306-v / home/mysql/conf:/etc/mysql/conf.d-v / home/mysql/data:/var/lib/mysql-e MYSQL_ROOT_PASSWORD=123456-- name mysql01 mysql:5.7

E669cf988dabeea812faa544be2511fe4a5ad0c250022afbd10081f304fe26d7

# after successful startup, let's use sqlyog locally to test the connection

# sqlyog-- connects to the 3310-3310 mapping of the server and the 3306 mapping in the container, and we can connect at this time!

# create a database in the local test to see if the path we mapped is ok!

Suppose we delete the container

Found that we mounted to the local data volume is still not lost, which realizes the container data persistence function!

Named and anonymous mount

# Anonymous mount

-v path within the container!

Docker run-d-P-- name nginx01-v / etc/nginx nginx

# check the status of all volume

[root@dockertest network-scripts] # docker volume ls

DRIVER VOLUME NAME

Local 1b3548d552bdac0cd277157af18c5925903c509940bff9390326a29c1cc83fa2

Local 310d040dc6743c5b7b68e2c3cae0399b8103a0144a63c6b1749694f69a17cd91

Local f472a5ba40dc3d7ad4768ee56ea1cff57132fd6f847bb72fd4431c7bd499cede

# found here, this is anonymous mount, we only write the path inside the container in-v, not the path outside the container!

# mount with a name

[root@dockertest network-scripts] # docker run-d-P-- name nginx02-v qls-nginx:/etc/nginx nginx

Acbf53630d19aa7fc614ac54e61c36e3dcbd4d9d914de640fc15f7f841d4959e

[root@dockertest network-scripts] # docker volume ls

DRIVER VOLUME NAME

Local 1b3548d552bdac0cd277157af18c5925903c509940bff9390326a29c1cc83fa2

Local 310d040dc6743c5b7b68e2c3cae0399b8103a0144a63c6b1749694f69a17cd91

Local f472a5ba40dc3d7ad4768ee56ea1cff57132fd6f847bb72fd4431c7bd499cede

Local qls-nginx

# through-v volume name: path within the container

# check this volume

All volumes in the docker container are in / var/lib/docker/volumes/xxxx/_data if no directory is specified.

We can easily find our volume by naming mount, and in most cases we use named mount.

# how to determine whether to mount a named mount or an anonymous mount or a specified path mount!

-v path in container # anonymous mount

-v volume name: the path in the container # mount with a name

-v / host path:: the path in the container # specifies the path to mount!

Expand:

# ro rw changes the read and write permissions through the path within the-v container

Ro readonly # read-only

Rw readwrite # readable and writeable

# once the permissions of this container are set, the container has a limit on what we can mount!

Docker run-d-P-- name nginx02-v qls-nginx:/etc/nginx:ro nginx

Docker run-d-P-- name nginx02-v qls-nginx:/etc/nginx:rw nginx

# ro as long as you see ro, the container can only be operated through the host, but cannot be operated inside the container!

First acquaintance of Dockerfile

Dockerfile is the build file used to build docker images! Command script! Try it first! (commit can be built, which is the second time mode here.)

Through this script, you can generate an image, one layer at a time, so the script is a command, and each command is a layer.

# create a dockerfile file with any name you like. It is recommended to name it Dockerfile

# contents of the file

FROM centosVOLUME ["volume01", "volume02"] CMD echo "- end----" CMD / bin/bash

# each command here is a layer of the mirror image.

# start the container written by yourself

This volume must have a synchronized directory with the outside!

Check the path where the volume is mounted

Test whether the files have been synchronized.

This approach is used a lot because we usually build our own images!

Assuming that there is no volume mounted when building the image, mount the volume by manual image-v volume name: the path within the container!

Data volume container

For example, two mysql synchronize data

# start 3 containers and start through the image we just wrote

# Test: you can delete docker01 to see if docker02 and 03 can still access this file

# the results of the test are still accessible

Data sharing with multiple mysql

[root@dockertest network-scripts] # docker run-d-p 3310 MYSQL_ROOT_PASSWORD=123456 3306-v / etc/mysql/conf.d-v / var/lib/mysql-e MYSQL_ROOT_PASSWORD=123456-- name mysql01 mysql:5.7

[root@dockertest network-scripts] # docker run-d-p 3310 MYSQL_ROOT_PASSWORD=123456 3306l-- name mysql02-- volumes-from mysql01 mysql:5.7

# data synchronization between the two containers can be achieved at this time!

This is the end of the content of "how to use docker Container data volumes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report