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

JavaScript Basics: Immediate Execution Function

Table of contents Immediately execute function fo...

Solutions to MySql crash and service failure to start

I have been in contact with PHP for so long, but ...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...

Ubuntu installs multiple versions of CUDA and switches at any time

I will not introduce what CUDA is, but will direc...

Record the steps of using mqtt server to realize instant communication in vue

MQTT Protocol MQTT (Message Queuing Telemetry Tra...

The concept and characteristics of MySQL custom variables

A MySQL custom value is a temporary container for...

Django+mysql configuration and simple operation database example code

Step 1: Download the mysql driver cmd enters the ...

How to install and deploy ftp image server in linux

Refer to the tutorial on setting up FTP server in...

Advantages and Problems of XHTML CSS Website Design

XHTML is the standard website design language cur...

Solution to Linux CentOS 6.5 ifconfig cannot query IP

Recently, some friends said that after installing...

Key knowledge summary of Vue development guide

Table of contents Overview 0. JavaScript and Web ...

Simple usage of MySQL temporary tables

MySQL temporary tables are very useful when we ne...