简体   繁体   English

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

[英]How to use command in docker-compose.yml

Here is my docker-compose.yml ,这是我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"

it just throw below error它只是抛出以下错误

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

Looks I can't use sign : in command , is it true?看起来我不能使用 sign : in command ,是真的吗? another question is if I want to run multi-commands, is below yml file right?另一个问题是如果我想运行多个命令,在 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"

The colon is how YAML introduces a dictionary.冒号是 YAML 引入字典的方式。 If you have it in a value, you just need to quote the value, for example like this:如果你有一个值,你只需要引用这个值,例如这样:

image: "elasticsearch:2.4"

Or by using one of the block scalar operators, like this:或者使用块标量运算符之一,如下所示:

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

For more information, take a look at the YAML page on Wikipedia.有关更多信息,请查看 Wikipedia 上的YAML页面。 You can always use something like this online YAML parser to test out your YAML syntax.你总是可以使用类似这样的在线 YAML 解析器来测试你的 YAML 语法。

Properly formatted, your first document should look something like:格式正确后,您的第一个文档应如下所示:

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"

(The indentation of the list markers ( - ) from the key isn't strictly necessary, but I find that it helps make things easier to read) (从键中缩进列表标记 ( - ) 并不是绝对必要的,但我发现它有助于使事情更容易阅读)

A docker container can only run a single command .一个 docker 容器只能运行一个command If you want to run multiple commands, put them in a shell script and copy that into the image.如果您想运行多个命令,请将它们放在一个 shell 脚本中并将其复制到映像中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM