Connect Redis from Another Container
Tags: docker, redis, network
Published: 2021-11-27 12:53:22

---
title: Connect Redis from Another Container
published: true
date: 2021-10-08 00:00:00 UTC
tags: docker, redis, network
canonical_url: https://tech.serhatteker.com/post/2021-10/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`:

```bash
$ docker run \
    --name redislocal \
    -p "6379:6379" \
    redis:6.2.5
```

Or in a more appropriate way:

```bash
$ 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
```

#### 1.1.2. docker-compose

Maybe I started it via `docker-compose`:

```bash
$ docker-compose -f compose-redis-local.yml up -d --build
```

And `compose-redis-local.yml` something like:

```yaml
# 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
```


## 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*:

```bash
$ docker network create local-dev
```
Ensure it was created:

```bash
$ 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
```

#### 2.1.1. docker cli

Start within this network:

```bash
$ 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
```

#### 2.1.2. docker-compose

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

```yaml
# 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
```

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:
- [docker network]
- [docker-compose network]

## 3. Connect

Now you can connect in the same network:

```bash
$ docker run \
    -it \
    --rm \
    --net local-dev \
    redis:6.2.5 \
    redis-cli -h redislocal -a pa$$w0rD ping
PONG
```


All done!




[docker-compose example]: http://tech.serhatteker/post/2021-10/connect-redis-from-another-container/#112-docker-compose
[docker network]: https://docs.docker.com/network/
[docker-compose network]: https://docs.docker.com/compose/networking/