Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker named volumes vs DOC (data-only-containers)

Tags:

Up to recent version of Docker (v1.10), we were thought that we can use DOC: data-only containers. So I would create such DOC (based on e.g. busybox) and use --volumes-from to link it to my container. You can still read about this in Docker documentation.

With new version of docker, it is said that instead of DOC we should use named volumes. Here is an example of docker-compose.yml:

version: '2'    
services:
  elasticsearch:
    image: elasticsearch:2.2.0
    command: elasticsearch -Des.network.host=0.0.0.0
    ports:
      - "9201:9200"
    volumes:
      - "es-data:/usr/share/elasticsearch/data"

volumes:    
  es-data:

Here we created and use named volume es-data.

There is still not much documentation on this new feature. I am asking:

  • Can we replace DOC with named containers? How long volume is persisted? What if I remove the container that is using it?
  • How can we e.g. backup now? Previously, I could docker run --rm --volumes-from es-data ... and then tar it.
like image 734
igr Avatar asked Mar 15 '16 12:03

igr


People also ask

Why would you choose data volume containers over data volumes?

The main advantage of both volumes and the data container pattern is that bind mounting on a host is host dependent, meaning you couldn't use that in a docker file. Volumes allow you the flexibility to define volumes when you build your images.

What are the two types of Docker volumes?

Docker volumes are used to persist data from within a Docker container. There are a few different types of Docker volumes: host, anonymous, and, named. Knowing what the difference is and when to use each type can be difficult, but hopefully, I can ease that pain here.

What are named volumes Docker?

Named volumes Named volumes can persist data after we restart or remove a container. Also, it's accessible by other containers. These volumes are created inside /var/lib/docker/volume local host directory.

What is the reason for using volumes in Docker?

The purpose of using Docker volumes is to persist data outside the container so it can be backed up or shared. Docker volumes are dependent on Docker's file system and are the preferred method of persisting data for Docker containers and services.


1 Answers

Can we replace DOC with named containers?

In many cases, yes, named containers will be a better option.

How long volume is persisted? What if I remove the container that is using it?

If you remove the container, the volume will still be there. The only way to remove the volume is to use docker-compose down -v or docker volume rm <volume name>.

How can we e.g. backup now? Previously, I could docker run --rm --volumes-from es-data ... and then tar it.

Instead of --volumes-from, you can use --volume=<volume name>.

Note that volumes created by docker-compose are always prefixed with the project name, so if you use it with a docker command the full name is actually <project_name>_es-data.

like image 176
dnephin Avatar answered Sep 24 '22 03:09

dnephin