'How use `echo` in a command in docker-compose.yml to handle a colon (":") sign?

Here is my 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 throws the error:

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

Looks as if I cannot use the colon sign : in command, is this true?



Solution 1:[1]

The colon is how YAML introduces a dictionary. 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. You can always use something like this online YAML parser to test out your YAML syntax.

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. If you want to run multiple commands, put them in a shell script and copy that into the image.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1