Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Redis to a Docker Container?

Tags:

docker

ruby

redis

I have a Sinatra app that works fine in Docker:

# Image
FROM ruby:2.3.3
RUN apt-get update && \
    apt-get install -y net-tools


# Install app
ENV APP_HOME /app
ENV HOME /root
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY Gemfile* $APP_HOME/
RUN bundle install
COPY . $APP_HOME


# Configure App
ENV LANG en_US.UTF-8
ENV RACK_ENV production

EXPOSE 9292


# run the application
CMD ["bundle", "exec", "rackup"]

But when I try to add Redis:

# Redis
RUN         apt-get update && apt-get install -y redis-server
EXPOSE      6379
CMD         ["/usr/bin/redis-server"]

Redis does not seem to start.

So, what is a good way to add Redis to a Ruby (FROM ruby:2.3.3) Docker container?

like image 648
B Seven Avatar asked Feb 21 '17 18:02

B Seven


People also ask

How do I connect Redis to docker?

To connect to a Redis instance from another Docker container, add --link [Redis container name or ID]:redis to that container's docker run command. To connect to a Redis instance from another Docker container with a command-line interface, link the container and specify the host and port with -h redis -p 6379.

How does Redis work with docker?

If you wish to connect to a Docker container running Redis from a remote server, you can use Docker's port forwarding to access the container with the host server's IP address or domain name. To use Docker's port forwarding for Redis, add the flag -p [host port]:6379 to the docker run command.

What is docker Redis?

Redis is an open source key-value store that functions as a data structure server. docker pull redis.


1 Answers

Split this into two containers. You can use docker-compose to bring them up on a common network. E.g. here's a sample docker-compose.yml:

version: '2'

services:
  sinatraapp:
    image: sinatraapp:latest
    ports:
    - 9292:9292
  redis:
    image: redis:latest

The above can include more options for your environment, and assumes your image name is sinatraapp:latest, change that to the image you built. You'll also need to update your sinatra app to call redis by the hostname redis instead of localhost. Then run docker-compose up -d to start the two services.

like image 93
BMitch Avatar answered Oct 02 '22 12:10

BMitch