如何在docker-compose.yml中使用命令

时间:2016-10-24 02:53:00

标签: docker docker-compose dockerfile docker-machine

这是我的docker-compose.yml

elasticsearch:
  ports:
  - 9200:9200/tcp
  image: elasticsearch:2.4
 volumes:
  - /data/elasticsearch/usr/share/elasticsearch/data:/usr/share/elasticsearch/data
 command: /bin/bash -c “echo 'http.cors.enabled: true' > /usr/share/elasticsearch/config/elasticsearch.yml"

它只是抛出错误

Activating (yaml: [] mapping values are not allowed in this context at line 7, column 49

看起来我无法在:中使用签名command,这是真的吗?另一个问题是,如果我想运行多命令,是否正好在yml文件下?

elasticsearch:
  ports:
  - 9200:9200/tcp
  image: elasticsearch:2.4
 volumes:
  - /data/elasticsearch/usr/share/elasticsearch/data:/usr/share/elasticsearch/data
 command: 
  -/bin/bash -c “echo 'http.cors.enabled: true' > /usr/share/elasticsearch/config/elasticsearch.yml"
  -/bin/bash -c “echo 'http.cors.allow-origin: "*"' > /usr/share/elasticsearch/config/elasticsearch.yml"

1 个答案:

答案 0 :(得分:4)

冒号是YAML如何引入字典的。如果你有一个值,你只需要引用该值,例如:

image: "elasticsearch:2.4"

或者使用其中一个块标量运算符,如下所示:

command: >
  /bin/bash -c “echo 'http.cors.enabled: true' > /usr/share/elasticsearch/config/elasticsearch.yml"

有关更多信息,请查看Wikipedia上的YAML页面。您始终可以使用this online YAML parser之类的内容来测试您的YAML语法。

格式正确,您的第一个文档应如下所示:

elasticsearch:
  ports:
    - 9200:9200/tcp
  image: "elasticsearch:2.4"
  volumes:
    - /data/elasticsearch/usr/share/elasticsearch/data:/usr/share/elasticsearch/data
  command: >
    /bin/bash -c “echo 'http.cors.enabled: true' > /usr/share/elasticsearch/config/elasticsearch.yml"

(来自密钥的列表标记(-)的缩进并非严格必要,但我发现它有助于使事情更容易阅读)

docker容器只能运行一个command。如果要运行多个命令,请将它们放在shell脚本中并将其复制到映像中。