Here is the docker-compose.yml configuration

version: '2' services: web: build: . ports: - "3030:3000" depends_on: - cache environment: - REDIS_PORT_6379_TCP_ADDR:"redis:6379" - MONGO_PORT_27017_TCP_ADDR:"mongodb://mongo:27017' cache: image: redis:latest ports: - "6379:6379" 

I lift with the docker-compose up command

After running nodejs from a web container, it cannot connect to redis . And throws an error - Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED

As I understand the whole problem in the new version of docker-compose, how to link containers correctly?

Here is a working example from the old version that I took from here.

Dockerfile

 FROM node:0.10.38 RUN mkdir /src RUN npm install nodemon -g WORKDIR /src ADD app/package.json /src/package.json RUN npm install ADD app/nodemon.json /src/nodemon.json EXPOSE 3000 CMD npm start 
  • publish Dockerfile for web service just in case - Mikhail Vaysman
  • links considers obsolete (but still working) features, the general recommendation is to use a private network (which will be created by default when you raise docker-compose) - etki
  • @etki Hmm .. Network? I mean the network section? - modelfak
  • Yes, there is a definite translation - etki

2 answers 2

Just add the links key and then ports from the cache service can be removed altogether. Is MongoDB running outside the container?

 version: '2' services: web: build: . ports: - "3030:3000" depends_on: - cache environment: - REDIS_PORT_6379_TCP_ADDR=cache - MONGO_PORT_27017_TCP_ADDR:"mongodb://mongo:27017' links: - cache cache: image: redis:latest ports: - "6379:6379" 
  • But what about the option - depends_on ? I pointed out there or she is responsible for something else - modelfak
  • she describes a dependency, but does not provide a link. - Mikhail Vaysman
  • No, it did not start, a similar error can add a working configuration for comparison - modelfak
  • Add Dockerfile - Mikhail Vaysman
  • updated the question and Dockerfile is no different from the fact that the link - modelfak

This is the problem - https://docs.docker.com/compose/environment-variables/#/environment-variables-created-by-links

Proceeding from this rule - with a new version the host for any linked container is equal to alias, in my case it is redis - then the working connection redis:6379

And environment variables as indicated in the example - here for redis and other ready-made containers do not need to be used.

  • you have no alias. and the name will not be redis, but cache. I checked my version - it works for me. - Mikhail Vaysman