Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Deploy and Run Redis in Docker

Start Redis in a container, keep its data, set a password and manage it with Docker Compose.

VPS and Cloud Servers3 min read19 screenshots

Why run Redis in Docker

  • Isolation. The container does not mix with other software on the host.
  • Portability. The same image runs on any system that has Docker.
  • Simplicity. You pull an image and do not compile or install anything.
  • Scale. More instances are easy to start.
    Redis running in Docker

Before you start

Check that Docker is installed:

docker --version
The Docker version

Step 1: Pull the image

docker pull redis:latest
Pulling the Redis image

Step 2: Run a basic container

docker run --name my-redis -d redis:latest
Running Redis

--name gives the container a name and -d runs it in the background.

Step 3: Open the port

To reach Redis from outside the container, map port 6379:

docker run --name my-redis -p 6379:6379 -d redis:latest
Publishing port 6379

Check that it runs:

docker ps
The running container
Warning: Do not open port 6379 to the whole internet without a password and a firewall. Bind it to localhost with -p 127.0.0.1:6379:6379 if only local apps need it.

Step 4: Test it

Open the Redis command line inside the container:

docker exec -it my-redis redis-cli
The Redis command line

Try to save and read a value:

SET mykey "Hello World"
GET mykey
Setting and reading a key

Step 5: Keep the data

Without a volume, the data is lost when the container is removed. Use a volume and turn on the append-only file:

docker run --name my-redis -p 6379:6379 -v redis_volume:/data -d redis:latest redis-server --appendonly yes
Running Redis with a volume

Look at the volume:

docker volume inspect redis_volume
Inspecting the volume

Step 6: Use your own configuration

Create a redis.conf file:

echo "maxmemory 256mb
maxmemory-policy allkeys-lru
save 60 1000" > redis.conf

Run Redis with that file:

docker run --name my-redis -p 6379:6379 -v $(pwd)/redis.conf:/usr/local/etc/redis/redis.conf -v redis_volume:/data -d redis:latest redis-server /usr/local/etc/redis/redis.conf
Mounting the configuration file

Step 7: Add a password

Add this line to redis.conf:

requirepass mypassword

Restart the container, then connect and sign in:

docker exec -it my-redis redis-cli
AUTH mypassword
Authenticating in Redis

Use a long, random password in real use.

Step 8: Use Docker Compose

Save this as docker-compose.yml:

services:
  redis:
    image: redis:latest
    container_name: my-redis
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    restart: unless-stopped
volumes:
  redis-data:
The Compose file

Start it:

docker compose up -d
Starting with Compose

(On older installations, the command is docker-compose up -d.)

Step 9: Logs

docker logs my-redis
Container logs

Follow the logs live:

docker logs -f my-redis
Live logs

Step 10: Stop and clean up

docker stop my-redis
Stopping the container
docker rm my-redis
Removing the container

The volume stays until you remove it with docker volume rm redis_volume.

Step 11: Update

docker pull redis:latest
Pulling a newer image

Then stop and remove the old container, and start a new one with the same volume. The data is kept.

Quick recap

  • docker run -d -p 6379:6379 redis starts Redis.
  • Use a volume and --appendonly yes to keep data.
  • Set a password and keep the port closed to the internet.