SoFunction
Updated on 2025-03-03

Complete steps to download and run Redis with Docker on Linux

Download and run Redis with Docker on LinuxComplete steps:

Step 1: Install Docker

First make sure Docker is installed on your Linux system.

Ubuntu:

sudo apt update
sudo apt install  -y
sudo systemctl start docker
sudo systemctl enable docker

CentOS / RHEL:

sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker

Check whether Docker is installed successfully

docker --version

You should see an output similar to the following:

Docker version 24.0.6, build 123abc

Step 2: Pull the Redis image

Use the Docker command to pull Redis images from the official repository:

docker pull redis:5.0.14

After the download is complete, you can run the following command to confirm whether the Redis image is successfully downloaded:

docker images | grep redis

Output example:

redis    5.0.14    1d8b4c9b262e    2 weeks ago   104MB

Step 3: Start the Redis container

Start Redis in Docker using the following command:

docker run -d --name test_redis -p 6379:6379 \
--restart=always \
-v /mnt/middleware/y_redis/data:/data \
-e TZ=Asia/Shanghai \
redis:5.0.14 \
redis-server --requirepass "ddddddd"

Command description:

  • -d: Let the container run in the background.
  • --name test_redis: Specify the container name astest_redis
  • -p 6379:6379: Map port 6379 of the host to port 6379 within the container.
  • --restart=always: Ensure that the container starts automatically after Docker restarts.
  • -v /mnt/middleware/y_redis/data:/data: Mount the host directory to the data directory of the Redis container to realize persistent storage.
  • redis-server --requirepass: Specify the Redis password asgGPNdtd32LY03CF

Step 4: Verify the Redis container status

Check if the container is running:

docker ps | grep test_redis

If the container is not running, check the startup log:

docker logs test_redis

Enter the container and check whether Redis is normal:

docker exec -it test_redis redis-cli

Test using password authentication:

auth gGPNdtd32LY03CF
ping

After success, it will return:

PONG

Step 5: Test the Redis connection

Test connections using the Redis CLI on the host:

redis-cli -h 127.0.0.1 -p 6379 -a gGPNdtd32LY03CF ping

If the connection is successful, it will return:

PONG

Step 6: Firewall settings (if required)

Make sure that port 6379 is not blocked by the firewall:

sudo ufw allow 6379/tcp
sudo ufw reload

Step 7: Data persistence verification

Redis saves data persistence to/mnt/middleware/y_redis/datain the directory. You can use the following command to view the contents of the directory:

ls /mnt/middleware/y_redis/data

Step 8: Troubleshooting

Port conflict

  • Check whether port 6379 is occupied:
sudo netstat -tulnp | grep 6379
  • If the port is occupied, stop the occupancy process:
sudo kill -9 <PID>

The container cannot start

  • View Redis container log:
docker logs test_redis

Data directory permission issues

  • If Redis cannot write to the data directory, check the directory permissions:
sudo mkdir -p /mnt/middleware/y_redis/data
sudo chown -R 1001:1001 /mnt/middleware/y_redis/data

Summarize

Through the above steps, you have successfully downloaded and ran the Redis container on Linux. Make sure to use the correct password when testing the connection. If you encounter any problems, check the logs and check if the port is occupied. If you have any further questions, please feel free to let me know.

The above is the detailed content of the complete steps to download and run Redis using Docker on Linux. For more information about downloading and running Redis in Linux Docker, please follow my other related articles!