How do I mount a host directory as a volume in docker compose

前端 未结 5 1394
时光取名叫无心
时光取名叫无心 2020-12-07 08:59

I have a development environment I\'m dockerizing and I would like the ability to livereload my changes without having to rebuild docker images. I\'m using docker compose be

5条回答
  •  生来不讨喜
    2020-12-07 09:55

    There are a few options

    Short Syntax

    Using the host : guest format you can do any of the following:

    volumes:
      # Just specify a path and let the Engine create a volume
      - /var/lib/mysql
    
      # Specify an absolute path mapping
      - /opt/data:/var/lib/mysql
    
      # Path on the host, relative to the Compose file
      - ./cache:/tmp/cache
    
      # User-relative path
      - ~/configs:/etc/configs/:ro
    
      # Named volume
      - datavolume:/var/lib/mysql
    

    Long Syntax

    As of docker-compose v3.2 you can use long syntax which allows the configuration of additional fields that can be expressed in the short form such as mount type (volume, bind or tmpfs) and read_only.

    version: "3.2"
    services:
      web:
        image: nginx:alpine
        ports:
          - "80:80"
        volumes:
          - type: volume
            source: mydata
            target: /data
            volume:
              nocopy: true
          - type: bind
            source: ./static
            target: /opt/app/static
    
    networks:
      webnet:
    
    volumes:
      mydata:
    

    Check out https://docs.docker.com/compose/compose-file/#long-syntax-3 for more info.

提交回复
热议问题