Docker Redis Container
Are you looking to set up a Redis container using Docker on your MacOS machine? Look no further! In this article, we'll walk you through the process of creating and configuring a Docker Redis container, including starting the container, and testing the database.
Step 1: Create a Docker Compose File
We'll create a Docker compose file that defines our Redis container.
redis-docker-compose.yml
services:
redis-db:
# Apple M1 Chip
# platform: linux/amd64
image: redis:7-alpine
container_name: redis-db
restart: always
ports:
- 6381:6379
command: redis-server --save 60 1 --loglevel warning --requirepass Password1234
volumes:
- redis_datadir:/data
networks:
- backend-network
networks:
backend-network:
driver: bridge
volumes:
redis_datadir:
This file defines a single service, redis-db, which uses the official Redis 7-alpine image and maps port 6381 on your local machine to port 6379 in the container. The redis database authentication password set as Password1234.
Step 2: Start the Container
Now that we have our environment file and Docker compose file set up, it's time to start the container! Run the following command:
docker-compose -f redis-docker-compose.yml up -d
This will start the container in detached mode, meaning it will run in the background.
Step3: Test Redis Database Container
Once the container is running, we can test our database. Run the following commands:
Let us create some keys & values.
Note: you should have redis-cli Client installed locally.
db_password=Password1234
redis-cli -h 127.0.0.1 -p 6381 -a ${db_password}
> set "Key1" "Value1"
> set "Key2" "Value2"
> SAVE
> exit
Test the database keys-
redis-cli -h 127.0.0.1 -p 6381 -a ${db_password}
> select 1
> keys *
> get "Key1"
> get "Key2"
> exit
Step 4: Stop the Container
Finally, we can stop the container by running the following command:
docker-compose -f redis-docker-compose.yml down
This will stop the container and remove it from memory.
That's it! We've successfully created and configured a Docker Redis container.