Arnab Dutta
Arnab Dutta

Reputation: 1

Docker containers inside same network not able to communicate with each other

I have three docker containers running in the same network. I used docker-compose to bring up the containers. The docker-compose script is:

version: '3.5'

services:
  ### Jesse's Workspace ################################################
  jesse:
    image: salehmir/jesse:latest
    depends_on:
      - postgres
      - redis
    tty: true
    env_file:
      - ../.env
    ports:
      - "9000:9000"
      # Jupyter Port
      - "8888:8888"
    volumes:
      - ../:/home
    container_name: jesse
    command: bash -c "jesse install-live --no-strict && jesse run"

  ### PostgreSQL ################################################
  postgres:
    image: postgres:14-alpine
    restart: always
    environment:
      - POSTGRES_USER=jesse_user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=jesse_db
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    container_name: postgres

  ### Redis ################################################
  redis:
    image: redis:6-alpine
    ports:
      - "6379:6379"
    container_name: redis
    command: redis-server --save "" --appendonly no
    
volumes:
  postgres-data:

Since I have not specified networks, I have checked all the containers are running inside the docker_default bridge network. DNS resolution works fine inside the containers by container names, but ping doesn't work neither any type of connectivity.

Since, I have exposed the port 6379 of the redis container, I am able to connect to redis from my host system. 127.0.0.1:6379. But, from any other container the connection is refused. I have tried to spin up another ubuntu container inside the same network, and noticed that I don't have internet connectivity inside the containers, i.e, no outgoing traffic. I am guessing this is something OS specific, as the same setup runs smoothly on my Mac. I have checked the ufw firewall status, which is inactive.

The jesse container is trying to connect to redis, which is not accepting any connections.

Traceback (most recent call last):
jesse  |   File "/usr/local/bin/jesse", line 33, in <module>
jesse  |     sys.exit(load_entry_point('jesse', 'console_scripts', 'jesse')())
jesse  |   File "/usr/local/bin/jesse", line 25, in importlib_load_entry_point
jesse  |     return next(matches).load()
jesse  |   File "/usr/local/lib/python3.9/importlib/metadata.py", line 77, in load
jesse  |     module = import_module(match.group('module'))
jesse  |   File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
jesse  |     return _bootstrap._gcd_import(name[level:], package, level)
jesse  |   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
jesse  |   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
jesse  |   File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
jesse  |   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
jesse  |   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
jesse  |   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
jesse  |   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
jesse  |   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
jesse  |   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
jesse  |   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
jesse  |   File "/jesse-docker/jesse/__init__.py", line 12, in <module>
jesse  |     from jesse.services import auth as authenticator
jesse  |   File "/jesse-docker/jesse/services/auth.py", line 5, in <module>
jesse  |     from jesse.services.env import ENV_VALUES
jesse  |   File "/jesse-docker/jesse/services/env.py", line 18, in <module>
jesse  |     if jh.is_unit_testing():
jesse  |   File "/jesse-docker/jesse/helpers.py", line 368, in is_unit_testing
jesse  |     from jesse.config import config
jesse  |   File "/jesse-docker/jesse/config.py", line 2, in <module>
jesse  |     from jesse.modes.utils import get_exchange_type
jesse  |   File "/jesse-docker/jesse/modes/utils.py", line 3, in <module>
jesse  |     from jesse.services import logger
jesse  |   File "/jesse-docker/jesse/services/logger.py", line 3, in <module>
jesse  |     from jesse.services.redis import sync_publish
jesse  |   File "/jesse-docker/jesse/services/redis.py", line 23, in <module>
jesse  |     async_redis = asyncio.run(init_redis())
jesse  |   File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
jesse  |     return loop.run_until_complete(main)
jesse  |   File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
jesse  |     return future.result()
jesse  |   File "/jesse-docker/jesse/services/redis.py", line 12, in init_redis
jesse  |     return await aioredis.create_redis_pool(
jesse  |   File "/usr/local/lib/python3.9/site-packages/aioredis/commands/__init__.py", line 188, in create_redis_pool
jesse  |     pool = await create_pool(address, db=db,
jesse  |   File "/usr/local/lib/python3.9/site-packages/aioredis/pool.py", line 58, in create_pool
jesse  |     await pool._fill_free(override_min=False)
jesse  |   File "/usr/local/lib/python3.9/site-packages/aioredis/pool.py", line 383, in _fill_free
jesse  |     conn = await self._create_new_connection(self._address)
jesse  |   File "/usr/local/lib/python3.9/site-packages/aioredis/connection.py", line 111, in create_connection
jesse  |     reader, writer = await asyncio.wait_for(open_connection(
jesse  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
jesse  |     return await fut
jesse  |   File "/usr/local/lib/python3.9/site-packages/aioredis/stream.py", line 23, in open_connection
jesse  |     transport, _ = await get_event_loop().create_connection(
jesse  |   File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1056, in create_connection
jesse  |     raise exceptions[0]
jesse  |   File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1041, in create_connection
jesse  |     sock = await self._connect_sock(
jesse  |   File "/usr/local/lib/python3.9/asyncio/base_events.py", line 955, in _connect_sock
jesse  |     await self.sock_connect(sock, address)
jesse  |   File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 502, in sock_connect
jesse  |     return await fut
jesse  |   File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 537, in _sock_connect_cb
jesse  |     raise OSError(err, f'Connect call failed {address}')
jesse  | TimeoutError: [Errno 110] Connect call failed ('172.18.0.2', 6379)

The python code that is used to connect:

async def init_redis():
    return await aioredis.create_redis_pool(
        address=(ENV_VALUES['REDIS_HOST'], ENV_VALUES['REDIS_PORT']),
        password=ENV_VALUES['REDIS_PASSWORD'] or None,
        db=int(ENV_VALUES.get('REDIS_DB') or 0),
    )

The .env values

REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=

docker ps:

docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED             STATUS          PORTS                                       NAMES
2a741ee69b20   postgres:14-alpine   "docker-entrypoint.s…"   About an hour ago   Up 58 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres
9012709c0bd1   redis:6-alpine       "docker-entrypoint.s…"   About an hour ago   Up 58 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis

I tried to ping the redis container from postgres container like this:

docker exec -it 2a ping redis
PING redis (172.18.0.2): 56 data bytes
^C
--- redis ping statistics ---
26 packets transmitted, 0 packets received, 100% packet loss

So, the DNS resolution works fine, but communication is not working. Whereas I can connect to redis from my host system.

Upvotes: 0

Views: 1188

Answers (1)

Slava Kuravsky
Slava Kuravsky

Reputation: 2824

The containers have to run in the same docker network https://docs.docker.com/compose/networking/

Upvotes: 0

Related Questions