Search code examples
dockerrediscontainersdocker-volume

docker-compose service without a volume


I have a redis service defined like this in the docker-compose.yml file:

redis:
  container_name: redis
  image: redis
  ports:
    - "6379:6379"

As you can see, there's no volume(s) defined here. Will the redis database persist between calls to docker run / docker-compose run? I can see some data in the database when I use docker run with redis-cli, so I'm curios where it comes from.


Solution

  • There are a couple of things that could be happening here.

    If you repeatedly run docker-compose up -d, it will make an effort to keep existing containers alive. If you're in an ordinary development cycle and repeatedly docker-compose stop app; docker-compose up --build -d, this will only restart the containers that have stopped, so in this sequence the database container will never get stopped and deleted at all; you'll just have the same database container.

    A second thing that happens is that the Redis Dockerfile declares VOLUME /data. If you don't specify anything else for that directory with a volumes: declaration, Docker creates an anonymous volume for that directory. Docker Compose will keep track of these, and the anonymous volume will stay associated with the specific service until you explicitly docker-compose down -v or you restart the service with docker-compose up --renew-anon-volumes.