To try this example on your PC you only need to install Docker Desktop and Node.js then follow the described steps.
Create a directory for this example and inside create two directories, one for the Node.js microservice and the other for Redis.
Copy the microservice files from its repository to the webservice directory, then create a Dockerfile inside redis-server directory.
COPY redis.conf /redis.conf
COPY run.sh /run.sh
CMD [ "/run.sh" ]
ENTRYPOINT [ "sh", "-c" ]
Create the run.sh file inside the redis-server.
It will start Redis using our custom redis.conf file.
echo "Starting Redis with config file..."
Download the Redis tar.gz file from the official website, extract the redis.conf from it and copy it inside the redis-server directory.
Now we have to find in redis.conf some settings and change them like this.
#Comment this line
#And disable proteced mode, so the microservice can connect
#Comment the default save settings for RDB
#And just set for this example to save every 5 seconds for only 1 operation
save 5 1
#save 300 10
#save 60 10000
#Change the dir setting to save Redis persistence backup files in a directory of our machine
#Turn on AOF
Create the docker-compose.yml file inside the directory of the project.
It will describe our application, with two Docker container, one for the Node.js microservice and one for Redis.
Create on your machine the redis-data directory that will be mounted to the redis-data directory of the Redis container.
Deploy and start your app using Docker Compose from inside the project directory.
You will then be able to see the logs of your Docker Compose app.
And in the Docker Desktop dashboard you can see your Docker Compose app running with its two containers.
And then retrieve the saved value through its key.
Now we can see that Redis created the dump.rdb and appendonly.aof files in the redis-data directory of our machine.
Try to turn off our application, even deleting the containers and then turn up it again.
docker-compose down --remove-orphans
docker image prune -a
You will not lost your data, because Redis automatically will rebuild its database using these files.
So if you try again to retrieve the saved value through its key, you will get it as if the app has never stopped.
You can find the source code on this GitHub repository:
It also includes the Postman collection used and and a sw-version.txt file to specify the softwares used for this project and their versions.
In the next post I’ll talk about how to deploy, managing and resizing this app via Kubernetes.