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
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
1. Docker pulls the redis image
docker pull redis
2. View the local image
docker images
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
##6. Enter the started container
docker exec -it myredis /bin/bash
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
redis-cli -h xx.xxx.xx.xxx -p 6379 -a xxx
8. Use the Redis Desktop Manager client to connect
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!