Search code examples
dockerdocker-composeconsul

how to initial setup consul with defined key/value


I have setup docker config using docker compose.

this is part of docker compose file

version: '3'
networks:
  pm:
services:
  consul:
    container_name: consul
    image: consul:latest
    restart: unless-stopped
    ports:
      - 8300:8300
      - 8301:8301
      - 8302:8302
      - 8400:8400
      - 8500:8500
      - 8600:8600
    environment:
      CONSUL_LOCAL_CONFIG: >-
        {
          "bootstrap": true,
          "server": true,
          "node_name": "consul1",
          "bind_addr": "0.0.0.0",
          "client_addr": "0.0.0.0",
          "bootstrap_expect": 1,
          "ui": true,
          "addresses" : {
            "http" : "0.0.0.0"
          },
          "ports": {
            "http": 8500
          },
          "log_level": "DEBUG",
          "connect" : {
            "enabled" : true
          }
        }
    volumes:
      - ./data:/consul/data
    command: agent -server -bind 0.0.0.0 -client 0.0.0.0 -bootstrap-expect=1

Then set the key value via browser

I would like to add the key/value as initial at new environment, so that additional setup steps at browser could be avoided.

this is the configuration i export by using consul kv command:

# consul kv export config/
[
        {
                "key": "config/",      
                "flags": 0,
                "value": ""
        },
        {
                "key": "config/drug2/",
                "flags": 0,
                "value": ""
        },
        {
                "key": "config/drug2/data",
                "flags": 0,
                "value": "e30="
        }
]

Solution

  • To my knowledge Docker Compose does not have a way to run a custom command/script after the containers have started.

    As a workaround you could write a shell script which executes docker-compose up and then either runs consul kv import or a curl command against Consul's Transaction API to add the data you're trying to load.