Run Redis with Docker without installing it on the host, it can be quickly started through the docker run command; it can customize configuration files and mount them to implement memory policies and other settings; it can persist data by naming volume redis-data; it is recommended to use Docker Compose to facilitate the deployment and maintenance of the development environment.
To run Redis in a Docker container, you don't need to install Redis on your host machine. Docker lets you spin up a Redis instance quickly using the official Redis image. Here's how to do it step by step.
1. Pull and Run Redis with Docker
If you want to get Redis up and running fast, use the docker run command with the official Redis image from Docker Hub.
docker run --name my-redis -d -p 6379:6379 redisThis command:
- --name my-redis : Assigns a custom name to the container
- -d : Runs the container in detached mode (in the background)
- -p 6379:6379 : Maps port 6379 on your host to port 6379 in the container (default Redis port)
- redis : Uses the official Redis image (pulls latest if not present)
2. Customize Redis Configuration
If you need to use a custom Redis configuration, create a local redis.conf file and mount it into the container.
- Create a config file:
echo "maxmemory-policy allkeys-lru" >> redis.conf
- Run Redis with the mounted config:
This starts Redis using your specified configuration for memory limits and evidence policy.
3. Persist Data Using Volumes
To make sure your data isn't lost when the container stops, use a Docker volume to persist Redis data.
docker run --name my-redis -d -p 6379:6379 -v redis-data:/data redis- -v redis-data:/data : Mounts a named volume to Redis's default data directory
Docker manages the volume automatically, so your data stays safe across container restarts or updates.
4. Use Docker Compose (Recommended for Development)
For easier management, especially in development, define your Redis service in a docker-compose.yml file.
version: '3.8'services:
redis:
image: redis
container_name: my-redis
Ports:
- "6379:6379"
Volumes:
- redis-data:/data
command: redis-server --appendonly yes
Volumes:
redis-data:
Then start it with:
docker-compose up -dThis approach is clean, repeatable, and ideal for projects with multiple services.
Basically just pick the method that fits your workflow. For quick testing, use docker run . For better setup and consistency, go with Docker Compose.
The above is the detailed content of How to run Redis in a Docker container?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Uninstall the old version of Docker to avoid conflicts, 2. Install yum-utils and add the official Docker repository, 3. Install DockerCE, CLI and containerd, 4. Start and enable Docker services, 5. Run hello-world image to verify that the installation is successful, 6. Optionally configure non-root users to run Docker.

DockerforWindowsusesaLinuxVMorWSL2toruncontainersbecauseWindowslacksnativeLinuxkernelfeatures;1)itautomaticallymanagesalightweightLinuxVM(orusesWSL2)withHyper-VtohosttheDockerdaemonandcontainers;2)theDockerCLIandDesktopinterfaceforwardcommandstotheda

When the reset computer is stuck, wait and observe the activity of the hard disk to confirm whether it is running; then disconnect the network to avoid update interference, or enter safe mode to eliminate software conflicts; check disk errors through chkdsk, clean the SoftwareDistribution cache to fix update problems; if it is still invalid, use Windows installation media to start the repair and perform the reset operation.

TouseDockereffectivelyforlocaldevelopment,firstinstallDockerDesktoporEngineandverifywithdocker--versionanddockerrunhello-world;thencreateaDockerfiletodefineyourapp’senvironmentandadocker-compose.ymlformulti-servicesetupslikeaNode.jsappwithPostgreSQL;

UseDockertorunPostgreSQLwithoutlocalinstallationbystartingacontainerwiththeofficialimage,settingpassword,port,andvolumeforpersistence.2.Createanamedvolumepostgres-datatopreservedataacrosscontainerrestarts.3.Customizedatabasenameanduserviaenvironmentv

Checkcontainerlogsusingdockerlogs[container_id]toidentifystartuperrorslikemissingfilesordependencyfailures.2.Runthecontainerinteractivelywithdockerrun--rm-it--entrypoint/bin/shimage_nametoinspectenvironmentandmanuallytestcommands.3.Examineexitcodesvi

TokeepaRedisinstancehealthy,monitorkeymetricsinthefollowingorder:1.Memoryusage,trackingused_memoryandused_memory_rsswhileapproachingsystemRAMlimits,andmanagingitviamaxmemory,evictionpolicies,efficientdatastructures,andcompression;2.CPUutilization,mon

The main difference between SUNION and SUNIONSTORE is the way of processing the result. 1.SUNION is used to return union elements of multiple collections, without modifying data, and is suitable for temporary viewing of results; 2.SUNIONSTORE stores union results in a new key, and is suitable for scenarios where persistent results are required. Both times have O(N), but SUNIONSTORE may have a greater impact on performance due to write operations. You should pay attention to memory usage and execution timing when using it.
