How to use Redis as docker container

In this article i will show you how to use the Redis docker container to start a redis instance! Not difficult but can be tricky for Docker…

How to use Redis as docker container
Photo by benjamin lehman on Unsplash

In this article i will show you how to use the Redis docker container to start a redis instance! Not difficult but can be tricky for Docker noobs! lets dive in!

Create the following file and save it as redis.yml

version: "3.8" 
services: 
  redis: 
    image: redis 
    ports: 
      - 6379:6379 
    volumes: 
      - /data/redis:/data 
    logging: 
      driver: syslog 
      options: 
        tag: docker-redis
  • The image section defines that the docker image named redis will be used, if it does not exist on the file system it will be downloaded from docker hub, the default docker repository.
  • The ports section maps the container port that will be forwarded to the docker host port, in this case container port 6379 will be forwarded to docker host 6379.
  • The volumes section defines that the redis container /data path will be mapped to the /data/redis path of the docker host, it assumes that the /data/redis directory exists on the docker host filesystem. (Important note: if no volume is defined then all data will be lost after container termination)
  • The logging section defines that the driver that will be used is syslog, this means that the logs will not printed to the stdout of the console but to the docker host syslog facility, apart from this further modification is needed to configure docker to write to syslog, you can follow my instructions here on how to enable syslog logging for docker https://medium.com/@kpatronas/docker-syslog-logging-per-container-f76276396204

Start the container

To start the container you need enter the following on the terminal

$ docker-compose -f ./redis.yml up -d
  • -f parameter defines the location of the yml file we created
  • up instructs docker to start the container
  • -d parameter defines that we want to start on the background

Get the state of the container

To view that state of the container we can use the docker ps command. Enter the following on a terminal

% docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS              PORTS                    NAMES 
a089f96ff42c   redis     "docker-entrypoint.s…"   3 minutes ago   Up About a minute   0.0.0.0:6379->6379/tcp   redis

Stop the container

To stop the container enter the following

docker-compose -f ./redis.yml down

Concussion

In this article we saw how we can configure a Redis container, its not difficult but can be tricky for novice people to set volumes or how to configure logging.

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…