Gustavo Barbosa
Gustavo Barbosa

Reputation: 31

Volume share from container to host docker-compose

I'm trying to share data between container and host. So I just want to do this to store container files. The data must be shared from a container to host.

My docker-compose.yml

version: "3.3"
services:
  django:
    image: python:slim
    volumes:
      - type: volume
        source: ./env
        target: /usr/local/lib/python3.6/site-packages
        volume:
          nocopy: true
      - ./src:/usr/src/app
    ports:
      - '80:80'
    working_dir: /usr/src/app
    command: bash -c "pip install -r requirements.txt && python manage.py runserver"

When I run docker throws this:

ERROR: for django Cannot create container for service django: invalid bind mount spec "/Users/gustavoopb/git/adv/env:/usr/local/lib/python3.6/site-packages:nocopy": invalid volume specification: '/Users/gustavoopb/git/adv/env:/usr/local/lib/python3.6/site-packages:nocopy': invalid mount config for type "bind": field VolumeOptions must not be specified ERROR: Encountered errors while bringing up the project.

https://docs.docker.com/compose/compose-file/#long-syntax-3

Upvotes: 2

Views: 8739

Answers (3)

Gustavo Barbosa
Gustavo Barbosa

Reputation: 31

My problem was keeping the python env when my container goes down. To do this I need to share the env inside of the container to host. I tried the docker docs suggestion, but it wasn't working.

volume: 
    nocopy: true

My solution: I create a named container.

version: "2"

services:
  django:
    image: python:2.7
    command: bash -c "pip install -r requirements.txt && python manage.py collectstatic --no-input && python manage.py migrate && python manage.py runserver 0.0.0.0:80"
    env_file:
      - .env
    volumes:
      - .:/app
      - env:/Library/Python/2.7/site-packages
    links:
      - database
    ports:
      - "8000:80"
    working_dir: /app

volumes:
  env:

Upvotes: 1

BMitch
BMitch

Reputation: 264761

You're trying to use the named volume syntax with a bind mount. I'd switch your syntax to:

version: "3.3"
services:
  django:
    image: python:slim
    volumes:
      - type: bind
        source: ./env
        target: /usr/local/lib/python3.6/site-packages
      - ./src:/usr/src/app
    ports:
      - '80:80'
    working_dir: /usr/src/app
    command: bash -c "pip install -r requirements.txt && python manage.py runserver"

Note the change in the type and the lack of the nocopy option. Copying files from the image to a host bind isn't supported, that is only available with named volumes.

Upvotes: 2

Ayushya
Ayushya

Reputation: 10447

The volume specified in the docs is not used in the service, rather, it is specified externally to service. Try removing the last line from volume:

volume:
  nocopy: true

Upvotes: 0

Related Questions