DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Connect Redis from Another Container

1. Issue

I started a redis container in my local environment and now I want to connect and/or send data to it from another container.

1.1. Reproduce

1.1.1. docker cli

Let's assume that I started a redis container from cli:

$ docker run \
    --name redislocal \
    -p "6379:6379" \
    redis:6.2.5
Enter fullscreen mode Exit fullscreen mode

Or in a more appropriate way:

$ docker run \
    --name redislocal \
    --hostname redislocal \
    -p "127.0.0.1:6379:6379" \
    -d \
    redis:6.2.5 \
    redis-server \
    --appendonly yes \
    --requirepass pa$$w0rD
Enter fullscreen mode Exit fullscreen mode

1.1.2. docker-compose

Maybe I started it via docker-compose:

$ docker-compose -f compose-redis-local.yml up -d --build
Enter fullscreen mode Exit fullscreen mode

And compose-redis-local.yml something like:

# compose-redis-local.yml
version: "3.5"

volumes:
  local_redis_data: {}

services:
  redis:
    container_name: redislocal
    hostname: redislocal
    image: redis:6.2.5
    command: redis-server --appendonly yes --requirepass pa$$w0rD
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
        - local_redis_data:/data
    restart:
      unless-stopped
Enter fullscreen mode Exit fullscreen mode

2. Solution

Sorry: there is NO way you can connect to above redis containers from another container.

When you spawn containers from docker-compose —docker-compose example, docker will:

  1. Create a network mayapp_default with bridge driver (myapp is the directory you're in)
  2. Create redislocal container and add it to mayapp_default network under the name redis

Similar applies to docker cli.

2.1. Custom Network

What you need is a user defined custom shared network: You should create a custom network than respawn your containers in this network.

Create a network called local-dev:

$ docker network create local-dev
Enter fullscreen mode Exit fullscreen mode

Ensure it was created:

$ docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
6d05d9ee7371   bridge            bridge    local
ac5000845253   host              host      local
d75701b57e1e   kind              bridge    local
5bde7f533689   local-dev         bridge    local
Enter fullscreen mode Exit fullscreen mode

2.1.1. docker cli

Start within this network:

$ docker run \
    --name redislocal \
    --network local-dev
    -p "127.0.0.1:6379:6379" \
    -d \
    redis:6.2.5 \
    redis-server \
    --appendonly yes \
    --requirepass pa$$w0rD
Enter fullscreen mode Exit fullscreen mode

2.1.2. docker-compose

Use this network as an external network in your compose file:

# compose-redis-local.yml
version: "3.5"

volumes:
  local_redis_data: {}

services:
  redis:
    container_name: redislocal
    hostname: redislocal
    image: redis:6.2.5
    command: redis-server --appendonly yes --requirepass 6te0valZ2L
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
        - local_redis_data:/data
    networks:
      - local-dev

networks:
  local-dev:
    external: true
Enter fullscreen mode Exit fullscreen mode

In addition to those, instead of User-defined bridge networks you can use host networks however I don't like this approach since it breaks the isolation nature of docker.

For more info you can look at this official docker docs:

3. Connect

Now you can connect in the same network:

$ docker run \
    -it \
    --rm \
    --net local-dev \
    redis:6.2.5 \
    redis-cli -h redislocal -a pa$$w0rD ping
PONG
Enter fullscreen mode Exit fullscreen mode

All done!

Top comments (0)

About Real-time

Join DEV and MongoDB to build a front-end application using MongoDB Atlas. Change streams to display live updates as your database changes for your entry in the DEV x MongoDB Atlas Hackathon 2022.

→ Join the Hackathon