Dockerizing a Spring Boot and Redis application - (Spring boot + Redis + Docker + Docker-compose)

Photo by Tom Fisk from Pexels
Photo by Tom Fisk from Pexels
Thought of sharing a example of how to dockerize a spring-boot application work with Redis cache, not only the basic stuff but a example that actually works and also you will learn few most used docker commands. Let's get started. Hope you have basic understanding on how docker works and it's concepts. If you are not familiar with docker please read about it. You can refer my previous blog post.

For this example I'm going to use one of our example repositories which we used to demonstrate how to use Redis with spring-boot and additionally I have added logging configuration which the app will write logs to separate file other than the console log output. You can find the GitHub link at the end of this post.
Let's write the basic docker file for our application. Create a dockerfile in the root of your project.


  1. FROM: The base image for openjdk 11 from docker hub. 
  2. VOLUME: Define a volume to persist data generated from the docker container. In this application mainly logs. If we did not mount a volume once the docker container stops, the data inside the container is gone too.
  3. ARG: Build time variable, here we have used it to reference our jar file.
  4. ADD: Copy the jar file as redis-service.jar
  5. EXPOSE: Inform the docker container to listen on the port at runtime.
  6. ENTRYPOINT: Tell docker how to run your application. (Run the jar file)

Build the image 

Before building the image, build the jar file using mvn clean install.
Run the docker build command from the terminal inside the project root directory. 
docker build -t redis-server:v1 .
In the above command we have defined a docker image with name as redis-service and a tag v1 defined by using -t option
You can see your docker images by running 
docker images

Run the image

docker run  -d -v ~/docker/redis:/redis-service/logs -p 8080:8080 redis-server:v1
d - Run the container in detached mode, simply running the container int the background.
v - Mount a volume. (host-src: conatainer:src)  We have mounted the directory ~/docker/redis in the host to the directory /redis-service/logs in the container, so we can persist and view live logs without going inside the container.
p - Map the port on host to port the app listening in the container

Use docker ps to view running containers 
docker ps

Go inside the container

Using the Container Id we can go inside the running docker container
docker exec -it 6cd2b248f49d bash
You will see the redis-service.jar file and redis-service log directory.

Now we have a running docker container with a volume attached to it and you can view the logs at
~/docker/redis directory without going inside the docker container.
But the application is going to failed if we invoke a API request because we haven't configured the Redis-server yet, for that we will use docker-compose. Docker compose let you to run multiple containers. Let's stop the running container now.

Stopping the container

docker stop <container-id>
docker stop 6cd2b248f49d

Creating the Redis dockerfile

Since we are overriding the default redis configurations we need to copy the configuration file to the redis container. Create a directory named redis inside out project directory and add the redis.conf file. You can find this file in our git repository. Basically we have override the requirepass (redis password used in application.yml) and set the bind to

We simply use the redis image and copy our configurations to the redis container. Now let's create a docker-compose.yml file in the project root directory.
  1. version: Define docker compose file format
  2. container_name: Name of the container 
  3. image: Docker image name with tag 
  4. services: Define a service with a key
  5. build: docker file path to build
  6. links: links to container in another service, here we link redis service to our app service
  7. ports and volumes: same as we discussed in dockerfile
  8. hostname: Define a redis hostname, this hostname should be used in application.yml to connect with redis.

Running the containers

Run the command from the terminal inside the project directory.
docker-compose up --build
You can you the -d  to run the containers in the background. 
Use docker-compose ps to view running containers 
Execute a API call to the application to confirm it working, you tail the application logs from the host ~/docker/redis directory

Stopping the containers

Use docker-compose stop to stop the running docker containers.
Alright that's it for this post, I hope this will help you to getting start with containerizing your applications. You can find the source code below. 

References & Useful Readings.


  1. Great Article I think you should contact Upsoft to get best freelance opportunities on Industrial Projects. <3 Kudos for such a great article.


Post a Comment