Home  >  Article  >  Database  >  How to install and deploy Redis database with Docker

How to install and deploy Redis database with Docker

王林
王林forward
2023-05-27 23:46:451358browse

1. Pull the image

Use the following command:

docker pull redis

2. Create a data volume

Usually, similar to MySQL, Redis is used as a database, we’d better Its configuration, data, etc. need to be mounted to a data volume to be persisted to the host.

Still use the named mount method to facilitate management.

docker volume create redis-config
docker volume create redis-data

In this way, two data volumes are created to store configuration files and data. You can also name them yourself.

3. Create and write the configuration file

First enter the data volume directory of the configuration file. You can view the location of the data volume through the docker volume inspect command:

docker volume inspect redis-config

How to install and deploy Redis database with Docker

After entering the directory, create a file named redis.conf and add the following content:

requirepass 12345678
dir /data

The password is set here to 12345678, the data file storage directory is set to /data. These configurations can be customized. For more configurations, please refer to this blog.

4, create and start the container

Execute the following command:

docker run -id --name=redis -v redis-config:/usr/local/etc/redis -v redis-data:/data -p 6379:6379 -e LANG=C.UTF-8 redis su -l root -c "redis-server /usr/local/etc/redis/redis.conf"

The above parameters are as follows:

  • -v Specify the data volume. You can see that /usr/local/etc/redis in the container is mounted to the data volume redis-config, and /data# in the container is mounted. ##Mount to the data volumeredis-data, it can be seen that the path in the container where the data volume is mounted must be consistent with the corresponding path in the configuration file we wrote in advance

  • -p

    is used to expose the port

  • -e

    is used to specify environment variables within the container, Set the language environment variable LANG of the container to the value C.UTF-8. It is best to set this, otherwise the default environment in the container is English, which may make Redis unable to store Chinese content

  • The end is the startup command, take a look at it separately:
su -l root -c "redis-server /usr/local/etc/redis/redis.conf"

means running

redis-server as

root inside the container And specify the configuration file location. The reason why it needs to be run as

root

is to prevent it from writing data to the disk without permission. It can also be seen that the last specified configuration file path is consistent with the path in the container of the configuration file data volume mounted previously -v. It is easy to understand here, but please note that if you customize other paths and files The name needs to be changed when -v is mounted and when the specified configuration is finally started. Finally, you can use the client to connect to Redis on the server!

The above is the detailed content of How to install and deploy Redis database with Docker. 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