Implementation of sharing data between Docker Volume containers

Implementation of sharing data between Docker Volume containers

What is volume?

Volume means capacity in English, and in Docker it means data volume, which is a container used to store data.

Why data sharing?

There are multiple Tomcats in a cluster. The codes deployed to Tomcats in the cluster are copies of the same code. If the page file changes, it means that the page file in each container must be updated. In a large-scale cluster, such workload will be infinitely magnified. At this time, data sharing is needed to solve this problem. The so-called data sharing refers to multiple containers sharing a data copy. How can we achieve this in a docker environment?

Data Sharing Principles

Create a space on the host's hard disk to store shared data. Multiple containers share this directory.

Solutions to achieve data sharing

When it comes to data sharing, it is easy to think of the mount command in Linux, which mounts a shared directory.

[x] Use the parameter -v 宿主機目錄:/容器內容目錄to place the shared files in a file on the host, and then multiple containers share this directory to achieve data sharing. This needs to be done at startup.

[x] Use the parameter -v 宿主機目錄:/容器內容目錄share the container to mount the host shared directory, and then realize data sharing by mounting the shared container

Container directly mounts shared directories

Resources List

Prepare two tomcat containers:
tomcat8000 tomcat8001

Detailed steps

Creating shared data

Create a shared directory: mkdir -p /usr/local/docker/volumn/pages/
Add a shared file in the shared directory: index.html, with the following content

I am a volume shared directory albk!

Create tomcat8000 and tomcat8001

docker run -d --name tomcat8000 -p 8000:8080 -v /usr/local/docker/volumn:/usr/local/tomcat/webapps tomcat

docker run -d --name tomcat8001 -p 8001:8080 -v /usr/local/docker/volumn:/usr/local/tomcat/webapps tomcat
When starting the container, mount the host's /usr/local/docker/volumn directory to the /usr/local/tomcat/webapps directory of the tomcat container, so that data sharing can be achieved

Verify that the container is started normally

docke ps 

docker exec -it a05a987b6da0 /bin/bash
Check the webapps directory to see if it is mounted successfully.

Browser access
http://host ip:8000/pages/index.html
http://host ip:8001/pages/index.html

Modify shared files

echo "我是一個volumn 共享目錄albk! 被修改了" > index.html

Re-authenticate the browser

You can see that our modified content has taken effect in real time, and there is no need to redeploy the tomcat container. However, this will cause the parameters to be very long each time the container is started, which is prone to errors. This is acceptable when the cluster is small, but it is also a lot of work when the scale is large. Let's take a look at another method.

Shared container mounting Create a shared container webpages , using the above method, using -v to mount the shared directory

docker create --name webpages -v /usr/local/docker/volumn/pages:/usr/local/tomcat/webapp tomcat /bin/true

Mounting a shared container

docker run --name tomcat8000 -d -p 8000:8080 --volumes-from webpages tomcat 
docker run --name tomcat8001 -d -p 8001:8080 --volumes-from webpages tomcat
The verification process is the same as above. The actual function of the container webpages is to define a mount point. When modifying the content, you only need to modify the shared directory of the shared container.

Summarize

If there are few containers, use -v If there are many containers, you can use -volumes-from . It is essentially the same as -v. Which one to use depends on the actual situation.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Directory permissions when creating a container with Docker
  • Docker compose custom network to achieve fixed container IP address
  • Ubuntu Docker installation in vmware (container building)
  • How to directly access the docker for windows container intranet through an independent IP
  • How to delete the container created in Docker

<<:  Detailed explanation of how to use join to optimize SQL in MySQL

>>:  Vue3.0 handwriting magnifying glass effect

Recommend

Troubleshooting the reasons why MySQL deleted records do not take effect

A record of an online MySQL transaction problem L...

A brief discussion on the VUE uni-app life cycle

Table of contents 1. Application Lifecycle 2. Pag...

Use a few interview questions to look at the JavaScript execution mechanism

Table of contents Previous words Synchronous and ...

WeChat applet calculator example

WeChat applet calculator example, for your refere...

Detailed explanation of bash command usage

On Linux, bash is adopted as the standard, which ...

Vue Element-ui table realizes tree structure table

This article shares the specific code of Element-...

Detailed steps for Linux account file control management

In the Linux system, in addition to various accou...

Detailed steps for installing and using vmware esxi6.5

Table of contents Introduction Architecture Advan...

JavaScript Objects (details)

Table of contents JavaScript Objects 1. Definitio...

Detailed explanation of MySQL foreign key constraints

Official documentation: https://dev.mysql.com/doc...

MySQL Index Optimization Explained

In daily work, we sometimes run slow queries to r...

Navicat connects to MySQL8.0.11 and an error 2059 occurs

mistake The following error occurs when connectin...

Detailed explanation of four types of MySQL connections and multi-table queries

Table of contents MySQL inner join, left join, ri...

Example code for implementing ellipse trajectory rotation using CSS3

Recently, the following effects need to be achiev...