Working with Container Volume
- Create a new docker volume named as web2_redis
docker volume create web2_redis
- Get list of docker volumes
docker volume ls
- Inspect docker volumes
docker volume inspect web2_redis
- Run a redis container with new created docker volume attached to it
docker container run --rm -itd -p 6379:6379 --name redis --net firstnetwork -v web2_redis:/data redis:3.2-alpine
-e = execute command in Docker -p = Define container & host port -it = interactive mode -rm = removes container when it stopped --name = Define container name -d = Run container is background -v = Define volume -net = Define network
- Launch Flask application in browser, so counter can be incremented
- Save state of redis using redis-cli, so counter state is saved
docker exec redis redis-cli SAVE
- Stop redis container
docker container stop redis
- Re-run redis container
docker container run --rm -itd -p 6379:6379 --name redis --net firstnetwork -v web2_redis:/data redis:3.2-alpine
- Launch Flask application in browser, so counter can be incremented
Here you will see counter is managed, this happens because state of redis container was saved inside a docker volume! - Stop web2 & redis container
docker container stop redis
docker container stop web2