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?
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.
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.
Redis is an open source key-value store that functions as a data structure server. docker pull redis.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With