How to use command in docker-compose.yml

12,075

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.

Share:
12,075
Yuwen Yan
Author by

Yuwen Yan

You

Updated on June 06, 2022

Comments

  • Yuwen Yan
    Yuwen Yan almost 2 years

    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 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? another question is if I want to run multi-commands, is below yml file right?

    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"