Home >Database >Redis >How to use docker to start redis and access it remotely on Linux

How to use docker to start redis and access it remotely on Linux

王林
王林forward
2023-05-27 08:01:102913browse

1. Install docker on centos7

1. Install some necessary system tools

yum install -y yum-utils device-mapper-persistent-data lvm2

2. Install docker’s yum source

yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo	# 中央仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo	# 阿里仓库

3. Install docker

yum install docker-ce  # 由于 repo 中默认只开启 stable 仓库,故这里安装的是最新稳定版

You can view all docker versions in all warehouses and select a specific version to install

yum list docker-ce --showduplicates | sort -r

How to use docker to start redis and access it remotely on Linux

yum install docker-ce-20.10.9.ce

4. Start the docker service

systemctl start docker	# 启动 Docker 
systemctl enable  docker	# 开机自启

5. Verify that docker

 docker version

has client and service parts, indicating that docker installation and startup are both Successful

How to use docker to start redis and access it remotely on Linux

2. Docker installs redis and starts it

1. Docker pulls the redis image

docker pull redis

2. View the local image

docker images

How to use docker to start redis and access it remotely on Linux

3. Mount the configuration file

The default redis installed by docker It can only be connected locally and cannot be accessed remotely, so you need to manually mount the external redis configuration file.

(1) Create a directory structure to store redis configuration files and data in any Linux directory: /docker/redis/conf, /docker/redis/data.

(2) Download the configuration file redis.conf from the official website and place it in the configuration file directory /docker/redis/conf.

(3) Modify the following configuration:

  • 1) bind 127.0.0.1: Comment out this part, which restricts redis to local access only

  • 2) protected-mode no: The default is yes, the protected mode is turned on, and restricted to local access

  • 3) requirepass 123456: Configure the redis connection password, the default is Commented

  • 4) dir ./: Change the local redis database storage folder (optional)

  • 5) appendonly yes: redis persistence ization, once this redis is turned on, it will not be automatically cleared every time it is restarted

4. Create a container and start the redis server

docker run -itd -p 6379:6379 --name lhjredis -v /docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /docker/redis/data:/data redis redis-server /etc/redis/redis.conf

1) –name: Give the container a name;

2) -p: Port mapping (host port: container port);

3) -v: Mount custom configuration (Customized configuration: internal configuration of the container);

This command has two mounts: the customized redis configuration on Linux (/docker/redis/conf/redis.conf) is mounted to the container The default configuration file /etc/redis/redis.conf of the redis application in the container; the customized data storage directory (/docker/redis/data) on Linux is mounted to the default data storage directory (/data) of the redis application in the container.

In this way, the redis application in the docker container will use the customized configuration file on Linux, and the data of the redis application in the docker container will be placed in the customized data storage directory on Linux.

4) -d: running in the background;

5) redis-server --appendonly yes: Execute the redis-server startup command in the container and open the redis persistence configuration;

5. Start successfully, check the status

docker ps

How to use docker to start redis and access it remotely on Linux

##6. Enter the started container

docker exec -it myredis  /bin/bash

How to use docker to start redis and access it remotely on Linux

Execute the

docker exec -it container name/bin/bash command to enter the started container;

exit command You can exit the container

7. Use the redis client in the container

redis-cli

How to use docker to start redis and access it remotely on Linux

After entering the container, use the above command Start the Redis client, which will connect to your local Redis server.

If you are connecting to redis of other servers, you need to add parameters (host address, port number, password)

redis-cli -h xx.xxx.xx.xxx -p 6379 -a xxx

8. Use the Redis Desktop Manager client to connect


How to use docker to start redis and access it remotely on Linux

## Note

    When an error is reported when starting the container port, you can use netstat -lntp | grep 6379 to check which program is occupying it
  • You can use sudo kill 6379 kills the program occupying the port
  • If you use Alibaba Cloud, etc., please be sure to open the corresponding port

The above is the detailed content of How to use docker to start redis and access it remotely on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete