Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing Redis DB docker container to a NodeJS docker container

Running windows 7 with vagrant 1.6.5 and virtual box. I've got two docker containers; - nodejs - redis

Part of my solution is running and working! The nodejs app when I run it outside the VM can connect to the redis container on port 6379. When I run the same app inside the nodejs docker container I get the following exception:

Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED

I can replicate the exception on the external project by stopping the redis container so the issue looks like its related to how the containers are linked/binding on internal ports on the VM.

The containers are started as follows;

$ docker run -d -p 6379:6379 --name redis myproject/redis
$ docker run --rm -it -P -p 9090:9090 --name node --link redis:rs myproject/node

The second command allows me to run and delete the image so that I can validate and debug the issue. From my understanding the linkage between the containers is correct!

I want to remove the host port 6379 so you can only hit the 9090 port which will expose Redis functionality.

Sample nodejs dockerfile;

# DOCKER-VERSION 0.10.0

FROM ubuntu
RUN env
# make sure apt is up to date
RUN apt-get update

# install nodejs and npm
RUN apt-get install -y nodejs npm git git-core
RUN PATH=/usr/bin/node:$PATH

# Bundle app source
ADD src /opt/app/

# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD src/package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
RUN cp -a /tmp/node_modules /opt/app/redis

EXPOSE  9090
CMD ["nodejs", "/opt/app/redis/server.js"]`

Any help is appreciated.

Sample Redis dockerfile

# Pull base image.
FROM dockerfile/ubuntu

# Install Redis.
RUN \
  cd /tmp && \
  wget http://download.redis.io/redis-stable.tar.gz && \
  tar xvzf redis-stable.tar.gz && \
  cd redis-stable && \
  make && \
  make install && \
  cp -f src/redis-sentinel /usr/local/bin && \
  mkdir -p /etc/redis && \
  cp -f *.conf /etc/redis && \
  rm -rf /tmp/redis-stable* && \
  sed -i 's/^\(bind .*\)$/# \1/' /etc/redis/redis.conf && \
  sed -i 's/^\(daemonize .*\)$/# \1/' /etc/redis/redis.conf && \
  sed -i 's/^\(dir .*\)$/# \1\ndir \/data/' /etc/redis/redis.conf && \
  sed -i 's/^\(logfile .*\)$/# \1/' /etc/redis/redis.conf

# Define mountable directories.
VOLUME ["/data"]

# Define working directory.
WORKDIR /data

# Define default command.
ENTRYPOINT ["redis-server"]

# Define default command.
#CMD ["redis-server", "/etc/redis/redis.conf"]

# Expose ports locally on VM
EXPOSE 6379

J

like image 595
user1859465 Avatar asked Mar 18 '26 11:03

user1859465


1 Answers

Found the solution!!!

I updated my JS file with the following;

var redisHost  = process.env.REDIS_PORT_6379_TCP_ADDR;
var redisPort  = process.env.REDIS_PORT_6379_TCP_PORT;
console.log('Settings ' + redisHost + ' ' + redisPort);
var http = require("http"), server, redis_client = require("redis").createClient(redisPort, redisHost);

These environment variables are configured based on the alias used when linking the containers i.e. running the following command;

docker run --rm -it -P -p 9090:9090 --name node --link redis:redis sapvagrant/node env

will produce the following output;

TERM=xterm
REDIS_PORT=tcp://172.17.0.38:6379
REDIS_PORT_6379_TCP=tcp://172.17.0.38:6379
REDIS_PORT_6379_TCP_ADDR=172.17.0.38
REDIS_PORT_6379_TCP_PORT=6379
REDIS_PORT_6379_TCP_PROTO=tcp
REDIS_NAME=/node/redis
HOME=/root

Whats important here is the alias used in the link redis:redis, if i was to call rename this to redis:rs then the environments would be;

RS_PORT=tcp://172.17.0.38:6379
RS_PORT_6379_TCP=tcp://172.17.0.38:6379
RS_PORT_6379_TCP_ADDR=172.17.0.38
RS_PORT_6379_TCP_PORT=6379
RS_PORT_6379_TCP_PROTO=tcp
RS_NAME=/node/redis

Now when I run the container nodejs is able to bind to the Redis database and Im able to expose port 9090 which will display some database functionality BUT the port 6379 is not exposed.

Sample project available at; https://github.com/longieirl/docker-redis-nodejs

Happy days.

J

like image 101
user1859465 Avatar answered Mar 20 '26 06:03

user1859465