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

Docker Nginx container production and deployment implementation method

Quick Start 1. Find the nginx image on Docker Hub...

Detailed View of Hidden Columns in MySQL

Table of contents 1. Primary key exists 2. No pri...

How to install Nginx in a specified location in Centos system

How to install Nginx in a specified location in C...

A Brief Analysis of the Differences between “:=” and “=” in MySQL

= Only when setting and updating does it have the...

A brief analysis of the usage of HTML float

Some usage of float Left suspension: float:left; ...

In-depth explanation of binlog in MySQL 8.0

1 Introduction Binary log records SQL statements ...

Nginx implements dynamic and static separation example explanation

In order to speed up the parsing of the website, ...

Centos7 startup process and Nginx startup configuration in Systemd

Centos7 startup process: 1.post(Power-On-Self-Tes...

How to solve the problem of forgetting the root password of Mysql on Mac

I haven't used mysql on my computer for a lon...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

How to install and use Ubuntu Docker

Table of contents 1. Automatic installation using...

Sitemesh tutorial - page decoration technology principles and applications

1. Basic Concepts 1. Sitemesh is a page decoratio...

Implementation of drawing audio waveform with wavesurfer.js

1. View the renderings Select forward: Select bac...