Detailed explanation of persistent storage of redis under docker

Detailed explanation of persistent storage of redis under docker

In this chapter, we will start to operate redis in the spring Boot project under docker

Preparation:

(1) Create a folder: usr/local/work/share

(2) Pull the project, which is a packaged jar package

(3) Put the extracted jar package into the folder just created, and create a file named docker-compose.yml

(4) Create a data folder in the tmp directory

(5) And write the following content in the docker-compose.yml file:

redis:
 image: redis:3
 ports:
 - "6379:6379"
 volumes:
 - /tmp/data:/data
 - java:
 image: bolingcavalry/springbootrun:0.0.1
 links:
 - redis:redishost
 volumes:
  - /usr/local/work/share:/usr/Downloads
 ports:
 - "8080:8080"
 tty: true

Here we only explain volumes. To put it simply, the data stored in the redis container in docker is persisted to the local directory.

Official statement:

The Redis port 6379 has been pre-configured by Redis and is exposed from the container to the host. In the docker-compose.yml file, it is exposed from the host to the outside world, so that any node can go to the Redis Desktop Manager and manage this Redis instance. On top of that, there are several things in the Redis spec that make data persist between deployments of this stack: Redis always runs on a manager, so it always uses the same filesystem; Redis accesses an arbitrary directory in the host’s filesystem as /data inside the container, and this is where Redis stores its data.

In summary, this is about creating the "source of truth" in the host's physical file system for your Redis data. Without this, Redis will store its data in /data on the container filesystem, which will be cleared if the container is redeployed. This source of truth has two components: placement constraints on the Redis service, ensuring it always uses the same host; and volumes created to allow containers to access /data (on the host) as /data (inside the Redis container). Files stored in /data on a given host will persist as containers come and go, thus maintaining continuity.

2. Start the container

After writing the above content to docker-compose.yml, execute the command docker-compose up -d in the directory where the file is located to start two containers. Then execute docker ps to see the container information as follows:

After the jar package is downloaded, it is placed in the local /usr/local/work/share directory. This directory is mapped to the container's /usr/Downloads, so we can directly access this file after entering the container.

3. Deploy jar package

Execute the following command to enter the running springboot container:

docker exec -it share_java_1 /bin/bash

Enter the /usr/Downloads directory and you can see the files:

redistempletedemo-0.0.1-SNAPSHOT.jar

Run the following command to start the container:

java -jar redistempletedemo-0.0.1-SNAPSHOT.jar

The startup is successful, the information is as follows:

Local testing

implement

http://localhost:8080/set/name/hxy

implement

http://localhost:8080/get/name

Let's go to the redis container to check and execute the following command to enter the redis container:

docker exec -it share_redis_1 /bin/bash

redis-cli

Execute get name in the console to see the value corresponding to name, as shown below:

The simple test has been successful. As mentioned above, all data will disappear as long as the redis container is closed, but we configured it in the docker-compose.yml above.

volumes: - /tmp/data:/data

This means that the data just saved is persisted to the local tmp/data directory. Open tmp/data and you will find a file called dump.rdb

Open it with a terminal and enter the command: vi dump.rdb and you will see the data you just saved name:hxy

That's all about the persistence of redis under docker. If there is a better way, please give me more advice. Thank you for reading this.

Related projects: https://github.com/haoxiaoyong1014/springboot-examples/tree/master/springboot-redis-docker

Additional knowledge: Install Redis under Docker, start it, set the password, and enable persistence

Pull the image

docker pull redis:5.0

Start and set password, enable persistence

docker run -d --name redis-server -p 6379:6379 redis:5.0 --requirepass "mypassword" --appendonly yes

The above detailed explanation of persistent storage of redis under docker is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Why is Redis fast? How to achieve high availability and persistence
  • In-depth explanation of two persistence solutions of redis
  • Detailed explanation of redis persistence, master-slave synchronization and sentinel under Linux
  • A brief discussion on the persistence of redis memory data
  • Detailed explanation of two persistence solutions of Redis: RDB and AOF
  • Redis persistence in-depth explanation

<<:  How to make React components full screen

>>:  Some references about colors in HTML

Recommend

Practical operation of using any font in a web page with demonstration

I have done some research on "embedding non-...

HTML marquee tag usage examples

This tag is not part of HTML3.2 and only supports ...

Mysql specifies the date range extraction method

In the process of database operation, it is inevi...

How to use a game controller in CocosCreator

Table of contents 1. Scene layout 2. Add a handle...

JS implements random generation of verification code

This article example shares the specific code of ...

Commonly used English fonts for web page creation

Arial Arial is a sans-serif TrueType font distribu...

Detailed explanation of a method to rename procedure in MYSQL

Recently I have used the function of renaming sto...

jQuery to achieve sliding stairs effect

This article shares the specific code of jQuery t...

A practical record of restoring a MySQL Slave library

Description of the situation: Today, I logged int...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...

Linux system (Centos6.5 and above) installation jdk tutorial analysis

Article Structure 1. Preparation 2. Install Java ...

Basic statements of MySQL data definition language DDL

MySQL DDL statements What is DDL, DML. DDL is dat...

How to reset the initial value of the auto-increment column in the MySQL table

How to reset the initial value of the auto-increm...