Dalvtor
Dalvtor

Reputation: 3286

What is the impact on not using volumes in my docker-compose?

I am new to docker, what a wonderful tool!. Following the Django tutorial, their docs provide a basic docker-compose.yml, that looks similar to the following one that I've created.

version: '3'

services:
  web:
    build: .
    container_name: web
    command: python manage.py migrate
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./src:/src
    ports:
      - "8000:8000"
    depends_on:
      - postgres
  postgres:
    image: postgres:latest
    container_name: postgres
    environment:
      POSTGRES_USER: my_user
      POSTGRES_PASSWORD: my_secret_pass!
      POSTGRES_DB: my_db
    ports:
      - "5432:5432"

However, in every single docker-compose file that I see around, the following is added:

volumes:
  - ./postgres-data:/var/lib/postgresql/data

What are those volumes used for? Does it mean that if I now restart my postgres container all my data is deleted, but if I had the volumes it is not?

Is my docker-compose.yml ready for production?

Upvotes: 0

Views: 91

Answers (1)

gasc
gasc

Reputation: 648

What are those volumes used for?

Volumes persist data from your container to your Docker host.

This:

volumes:
  - ./postgres-data:/var/lib/postgresql/data

means that /var/lib/postgresql/data in your container will be persisted in ./postgres-data in your Docker host.

What @Dan Lowe commented is correct, if you do docker-compose down without volumes, all the data insisde your containers will be lost, but if you have volumes the directories, and files you specified will be kept in your Docker host

You can see this data in your Docker host in /var/lib/docker/volumes/<your_volume_name>/_data even after your container don't exist anymore.

Upvotes: 2

Related Questions