fail to link redis container to node.js container in docker

I had deployed a simple redis based nodejs application on the digital ocean cloud.

Here is the node.js app.

  • How to check if docker daemon is running?
  • Clone and run laravel application easily
  • Fixing World-writable MySql error in Docker
  • Docker need to access hosts's Postgres database
  • If an marathon app running in a docker image, is in 'deploying' state, where can we check the reason why the app isn't deploying?
  • Docker Compose does not bind ports
  • var express = require('express');
    var app = express();   
    app.get('/', function(req, res){
      res.send('hello world');
    });
    app.set('trust proxy', 'loopback') 
    app.listen(3000);
    
    var redisClient = require('redis').createClient(6379,'localhost');
    redisClient.on('connect',function(err){
       console.log('connect');
    })
    

    In order to deploy the application, I used one node.js container and one redis container respectively,and linked node.js container with redis container.

    The redis container could be obtained by

    docker run -d --name redis -p 6379:6379 dockerfile/redis
    

    and the node.js container is based on google/nodejs, in which Dockerfile is simply as

    FROM google/nodejs
    WORKDIR   /src
    EXPOSE  3000
    CMD ["/bin/bash"]
    

    my node.js image is named as nodejs and built by

    docker build -t nodejs Dockerfile_path
    

    and the container is run by copying my host applications files to the src folder in the container and linking the existing redis container

    docker run -it --rm -p 8080:3000 --name app -v node_project_path:/src --link redis:redis nodejs
    

    finally I got into the container successfully, then installed the npm modules by npm install and then start the application by node app.js.

    But I got a error saying:

    Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED
    

    As redis container is exposed to 6379, and my nodejs container is linking to redis container. in my node.js app, connecting to localhost redis server with port 6379 is supposed to be ok, why in fact it is not working at all

  • Docker volumes and package.json not found
  • Docker: HTTP code 403 while uploading metadata: “invalid character '<' looking for beginning of value”
  • Docker | How to build and deploy war file in jboss container hosted in Docker
  • How do I make a standalone application out of a Docker container?
  • How to get .CSV file into local machine from Docker image of Cassandra database
  • Docker container can not be found using curl
  • One Solution collect form web for “fail to link redis container to node.js container in docker”

    When you link the redis container to the node container, docker will already modify the hosts file for you

    You should then be able to connect to the redis container via:

    var redisClient = require('redis').createClient(6379,'redis'); // 'redis' is alias for the link -> what's in the hosts file.
    

    From: https://docs.docker.com/userguide/dockerlinks/

    $ sudo docker run -d -P --name web --link db:db training/webapp python app.py
    

    This will link the new web container with the db container you created earlier. The –link flag takes the form:

    --link name:alias
    

    Where name is the name of the container we’re linking to and alias is an alias for the link name. You’ll see how that alias gets used shortly.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.