Docker-compose: connection refused between containers, but access to the service from the host

TL DR: How do I change my docker-compose.yml below so that one container can use the service of another through a custom (non-standard) port?

I have a fairly common setup: containers for a web application (Padrino [Ruby]), Postgres, Redis, and a queue structure (Sidekiq). The web application comes with a custom Dockerfile, other services come either from standard images (Postgres, Redis), or mount data from the web application (Sidekiq). They are connected to each other via docker-compose.yml :

 version: '2' services: web: build: . command: 'bundle exec puma -C config/puma.rb' volumes: - .:/myapp ports: - "9000:3000" depends_on: - postgres - redis sidekiq: build: . command: 'bundle exec sidekiq -C config/sidekiq.yml -r ./config/boot.rb' volumes: - .:/myapp depends_on: - postgres - redis postgres: image: postgres:9.5 environment: POSTGRES_USER: my-postgres-user POSTGRES_PASSWORD: my-postgres-pass ports: - '9001:5432' volumes: - 'postgres:/var/lib/postgresql/data' redis: image: redis ports: - '9002:6379' volumes: - 'redis:/var/lib/redis/data' volumes: redis: postgres: 

One of the key points that should be noted here is that I expose container services to non-standard ports (9000-9002).

If I started the installation using docker-compose up , the Redis and Postgres containers look fine, but the containers for the web application and Sidekiq fail because they cannot connect to Redis in redis:9002 . It is noteworthy that the same setup works if I use 6379 (standard Redis port) instead of 9002.

docker ps also looks great afaik:

 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9148566c2509 redis "docker-entrypoint.sh" Less than a second ago Up About a minute 0.0.0.0:9002->6379/tcp rubydockerpadrino_redis_1 e6d47321c939 postgres:9.5 "/docker-entrypoint.s" Less than a second ago Up About a minute 0.0.0.0:9001->5432/tcp rubydockerpadrino_postgres_1 

What's even more confusing: I can access the Redis container from the host through redis-cli -h localhost -p 9002 -n 0 , but the web application and Sidekiq containers cannot establish a connection.

I use this version of dockers on MacOS: Docker version 1.12.3, build 6b644ec, experimental

Any ideas what I'm doing wrong? I would appreciate how to start my installation.

+2
source share
1 answer

When you bind ports like this one to '9002:6379' , you tell Docker to forward traffic from localhost:9002 redis:6379 . This is why it works from your main machine:

 redis-cli -h localhost -p 9002 -n 0 

However, when containers talk to each other, they are all connected to the same network by default (Docker bridge or docker0 ). By default, containers are free to communicate with each other on this network, without the need to open ports. On this network, your redis container redis for traffic on a regular port ( 6379 ), the host is not involved at all. Therefore, your connection with the container and the container runs on 6379 .

+4
source

Source: https://habr.com/ru/post/1013478/


All Articles