Docker
Redis
Networking
Host Machine
Containerization

Connecting to Redis running in Docker Container from Host machine

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

To connect to Redis running in a Docker container from your host machine, you must publish port 6379 when starting the container with -p 6379:6379. This maps the container's Redis port to the same port on your host, allowing redis-cli, application code, and GUI tools to connect via localhost:6379. Without the port mapping, the container's network is isolated and unreachable from the host.

Starting Redis with Port Mapping

bash
docker run --name my-redis -p 6379:6379 -d redis
  • --name my-redis — gives the container a memorable name
  • -p 6379:6379 — maps host port 6379 to container port 6379
  • -d — runs in detached (background) mode
  • redis — uses the official Redis image

Connecting from the Host

bash
1# Using redis-cli (install with: brew install redis / apt install redis-tools)
2redis-cli -h localhost -p 6379
3
4# Test the connection
5redis-cli ping
6# PONG
7
8# Set and get a value
9redis-cli set greeting "hello"
10redis-cli get greeting
11# "hello"

Using a Different Host Port

bash
1# Map to a non-standard port on the host
2docker run --name my-redis -p 6380:6379 -d redis
3
4# Connect on the custom port
5redis-cli -h localhost -p 6380

This is useful when you already have a local Redis instance on port 6379.

Docker Compose

yaml
1# docker-compose.yml
2version: '3.8'
3services:
4  redis:
5    image: redis:7-alpine
6    ports:
7      - "6379:6379"
8    volumes:
9      - redis-data:/data
10    command: redis-server --appendonly yes
11
12volumes:
13  redis-data:
bash
1docker compose up -d
2
3# Connect from host
4redis-cli -h localhost -p 6379 ping
5# PONG

Connecting with a Password

bash
1# Start Redis with a password
2docker run --name my-redis -p 6379:6379 -d redis redis-server --requirepass mysecretpassword
3
4# Connect with authentication
5redis-cli -h localhost -p 6379 -a mysecretpassword
6
7# Or authenticate after connecting
8redis-cli -h localhost -p 6379
9> AUTH mysecretpassword
10# OK

Docker Compose with a password:

yaml
1services:
2  redis:
3    image: redis:7-alpine
4    ports:
5      - "6379:6379"
6    command: redis-server --requirepass ${REDIS_PASSWORD}
7    environment:
8      - REDIS_PASSWORD=mysecretpassword

Connecting from Application Code

Python

python
1import redis
2
3r = redis.Redis(host='localhost', port=6379, db=0)
4r.set('key', 'value')
5print(r.get('key'))  # b'value'
6
7# With password
8r = redis.Redis(host='localhost', port=6379, password='mysecretpassword')

Node.js

javascript
1const Redis = require('ioredis');
2
3const redis = new Redis({
4    host: 'localhost',
5    port: 6379,
6    // password: 'mysecretpassword',
7});
8
9await redis.set('key', 'value');
10const value = await redis.get('key');
11console.log(value); // 'value'

Java (Jedis)

java
1Jedis jedis = new Jedis("localhost", 6379);
2jedis.set("key", "value");
3String value = jedis.get("key");
4System.out.println(value); // "value"
5jedis.close();

Connecting from Another Container

Containers on the same Docker network can connect by container name:

yaml
1# docker-compose.yml
2services:
3  redis:
4    image: redis:7-alpine
5    # No 'ports' needed for container-to-container communication
6
7  app:
8    build: .
9    depends_on:
10      - redis
11    environment:
12      - REDIS_URL=redis://redis:6379

The app container connects to redis:6379 using the service name as the hostname. Port mapping (ports:) is only needed for host-to-container access.

Custom Redis Configuration

bash
1# Mount a custom redis.conf
2docker run --name my-redis \
3  -p 6379:6379 \
4  -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf \
5  -d redis redis-server /usr/local/etc/redis/redis.conf
conf
1# redis.conf
2bind 0.0.0.0
3port 6379
4requirepass mysecretpassword
5maxmemory 256mb
6maxmemory-policy allkeys-lru
7appendonly yes

Verifying the Connection

bash
1# Check if the container is running
2docker ps | grep redis
3
4# Check which port is mapped
5docker port my-redis
6# 6379/tcp -> 0.0.0.0:6379
7
8# Check Redis logs
9docker logs my-redis
10
11# Execute redis-cli inside the container
12docker exec -it my-redis redis-cli
13> INFO server

Common Pitfalls

  • Forgetting -p 6379:6379: Without port mapping, the host cannot reach the container's Redis. Container ports are isolated by default. Always include -p for host access.
  • Redis bind directive: If using a custom redis.conf, ensure bind 0.0.0.0 (not bind 127.0.0.1). The default bind 127.0.0.1 only accepts connections from inside the container, rejecting host connections.
  • Firewall blocking the port: On Linux, iptables or ufw may block Docker's published ports. Check with sudo ufw status and allow port 6379 if needed.
  • Port already in use: If a local Redis instance is already running on port 6379, Docker fails to bind the port. Either stop the local Redis (sudo systemctl stop redis) or map to a different host port (-p 6380:6379).
  • Using localhost from another container: Containers should connect by service name (redis:6379), not localhost. Inside a container, localhost refers to that container itself, not the host or other containers.

Summary

  • Use -p 6379:6379 when starting the container to expose Redis to the host
  • Connect with redis-cli -h localhost -p 6379 or any Redis client library
  • Use Docker Compose with ports: ["6379:6379"] for reproducible setups
  • Container-to-container connections use the service name, not port mapping
  • Set bind 0.0.0.0 in custom redis.conf to accept external connections

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.