Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share volumes among containers by docker-compose

My docker-compose defines two containers. I want one container shares a volume to the other container.

version: '3'
services:
  web-server:
    env_file: .env
    container_name: web-server
    image: web-server
    build: 
      dockerfile: docker/Dockerfile
    ports: 
      - 3000:3000
      - 3500:3500
    volumes:
      - static-content: /workspace/static
    command: sh /workspace/start.sh

  backend-server:
    volumes:
      - static-content: /workspace/static
  volumes:
    static-content:

The above docker composer file declares two services, web-server and backend-server. And I declares the named volume static-content under services. I got below error when I run docker-composer -f docker-composer.yml up:

services.web-server.volumes contains an invalid type, it should be a string
services.backend-server.volumes contains an invalid type, it should be a string

so how can I share volumes throw docker-composer?

like image 913
Joey Yi Zhao Avatar asked Nov 06 '17 12:11

Joey Yi Zhao


1 Answers

You have an extra space in your volume string that causes Yaml to change the parsing from an array of strings to an array of name/value maps. Remove that space in your volume entries (see below) to prevent this error:

version: '3'
services:
  web-server:
    env_file: .env
    container_name: web-server
    image: web-server
    build: 
      dockerfile: docker/Dockerfile
    ports: 
      - 3000:3000
      - 3500:3500
    volumes:
      - static-content:/workspace/static
    command: sh /workspace/start.sh

  backend-server:
    volumes:
      - static-content:/workspace/static
  volumes:
    static-content:

For more details, see the compose file section on volumes short syntax.

like image 88
BMitch Avatar answered Oct 13 '22 16:10

BMitch