How to run Consul on docker with initial key-value pair data?

I am trying to spin a Consul server on docker container and use it as config server for my SpringBoot cloud application. For that I want to have some pre-configured data(Key-Value pairs) in Consul.

My current config in docker-compose.yml is:

  • Hazelcast in multinode docker environments with TCPIP
  • What is the difference between docker-compose up and docker-compose start?
  • HTTP Request from Dockerfile not successful
  • Container healthcheck status via tasks json
  • jax-rs client is not creating jax-rs cookie
  • I can't get red5-hls-plugin to build
  •   consul:
        image: "progrium/consul:latest"
        container_name: "consul"
        ports:
          - '9330:8300'
          - '9400:8400'
          - '9500:8500'
          - '9600:53'
        command: "-server -bootstrap -ui-dir /ui"
    

    Is there a way to pre-populate key-value pairs?

  • Installing docker-ce through puppet
  • Java library for Docker repository?
  • Run multiple servers from a docker image
  • How to add a device to all docker containers
  • Deploy Docker environment on Elastic Beanstalk
  • Modifying a Docker container's ENTRYPOINT to run a script before the CMD
  • One Solution collect form web for “How to run Consul on docker with initial key-value pair data?”

    Compose

    Compose doesn’t have “tasks” as built in concept, but you can set them up with multiple compose files in a project. A docker-compose-init.yml could define the tasks, rather than long running services but you need to manage orchestration yourself. I’ve put an example on my consul demo.

    docker-compose up -d
    docker-compose -f docker-compose-init.yml run consul_init
    

    Image Build

    You can add image build RUN steps to add the data. The complication here is running the server the same way you normally would, but in the background, and adding the data all in the one RUN step.

    FROM progrium/consul:latest
    RUN set -uex; \
        consul agent -server --bootstrap -data-dir /consul/data & \ 
        let "timeout = $(date +%s) + 15"; \
        while ! curl -f -s http://localhost:8500/v1/status/leader | grep "[0-9]:[0-9]"; do\
          if [ $(date +%s) -gt $timeout ]; then echo "timeout"; exit 1; fi; \
          sleep 1; \
        done; \
        consul kv put somekey somevalue;
    

    Image Startup

    Some databases add a script to the image to populate data at startup. This is normally so users can control setup via environment variables injected at run time, like mysql/postgres/mongo.

    FROM progrium/consul:latest
    ENTRYPOINT my-entrypoint.sh
    

    Then your script starts the server, sets up the data, and then at the end continues on as the image would have before.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.