Jayaram
Jayaram

Reputation: 6606

How to correctly specify named volumes when trying to backup files in docker

I have the below docker-compose file

version: '2'

services:
  postgres:
    container_name: postgres
    image: postgres:${POSTGRES_VERSION}
    volumes:
      - postgresdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=42EXP

  node:
    container_name: node
    build: .
    links:
      - postgres:postgres
    ports:
      - "8000:8000"
    depends_on:
      - postgres
    command: npm start

volumes:
  postgresdata:

on running docker volume ls i have

local               42exp_postgresdata

When trying to perform a backup through:

docker run --rm --volume=42exp_postgresdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /var/lib/postgresql/data

i get the below error:

docker: Error response from daemon: Invalid volume spec "42exp_postgresdata": Invalid volume destination path: '42exp_postgresdata' mount path must be absolute..

I'm not sure where i'm going wrong since its pretty much the exact code specified in the docs. How do i properly specify a named volume to backup?

Upvotes: 1

Views: 511

Answers (2)

aceton1k
aceton1k

Reputation: 61

You need to mount the volume somewhere:

docker run --rm -v 42exp_postgresdata:/var/lib/postgresql/data -v $(pwd):/backup \
    ubuntu tar cvf /backup/backup.tar /var/lib/postgresql/data

EDIT:

Note that -v | --volume option has many formats. You can use it to bind-mount folders from host to container:

-v /folder/on/host:/folder/in/container

mount named volumes to container:

-v named-volume:/folder/in/container

or just to specify un-named volumes for container:

-v /this/folder/in/container/will/be/stored/in/volume

Upvotes: 1

Matthew
Matthew

Reputation: 11347

Have you tried using --volumes-from mycompose_postgres_1 (or whhatever your resulting docker image is called)? See: https://docs.docker.com/engine/tutorials/dockervolumes/

Upvotes: 0

Related Questions