1

The following is an example given in https://docker-curriculum.com/

version: "3"
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    volumes:
      - esdata1:/usr/share/elasticsearch/data
  web:
    image: prakhar1989/foodtrucks-web
    command: python app.py
    depends_on:
      - es
    ports:
      - 5000:5000
    volumes:
      - ./flask-app:/opt/flask-app
volumes:
    esdata1:
      driver: local

and it says The volumes parameter specifies a mount point in our web container where the code will reside about the /opt/flask-app

I think it means, /opt/flask-app is a mount point and it points to the host machines ./flask-app

However it doesn't say anything about esdata1 and I can't apply the same explanation as given to /opt/flask-app since there's no esdata1 directory/file in the host machine.

What is happening for the esdata1 ? My guess is that it means creating a volume (The closest thing I can think of is a disk partition) and name it esdata1 and mount it on /usr/share/elasticsearch/data, am I correct on this guess?

eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

0

These are a bit different things: volumes and bind mounts. Bind mounts let you specify folder on host machine, which would serve as a storage. Volumes (at lease for local driver) also have folders on host machines, but their location is managed by Docker and is sometimes a bit more difficult to find.

When you specify volume in docker-compose.yml, if your path starts with / or . it becomes a bind mount, like in web service. Otherwise, if it is a single verb, it is a volume, like for es service.

You can inspect all volumes on your host machine by running docker volume ls.

What is happening for the esdata1 ? My guess is that it means creating a volume (The closest thing I can think of is a disk partition) and name it esdata1 and mount it on /usr/share/elasticsearch/data, am I correct on this guess?

That's all correct.

I do not pretend on setting up the rules, but in general, volumes are more suitable for sharing common data between several containers, like in docker-compose, while bind mounts suite better for sharing data from host to container, like some initial configs for services.

grapes
  • 8,185
  • 1
  • 19
  • 31
  • Thanks for quick answer, what about persistant data such as DB or elasticsearch data? which of `volumes` or `bind mounts` are preferred for those? – eugene Feb 04 '19 at 15:25
  • It depends whether you need this data outside or not. If you use it internally in `docker-compose`, using volume is a better solution. – grapes Feb 04 '19 at 15:33
  • Just written an answer few minutes ago on persisting the data: https://stackoverflow.com/a/54519405/10524386 The last part of the answer relates to the topic – grapes Feb 04 '19 at 15:35
  • heh, that's me again actually, I'll be reading the answers there .. Thanks! – eugene Feb 04 '19 at 15:44