W
W
wargych2021-12-02 14:05:26
Docker
wargych, 2021-12-02 14:05:26

How to set consul kv values ​​when running in docker?

I want to make sure that consul rises in the docker with some values ​​\u200b\u200balready filled in the key-value store.
As far as I understand, the only way to do this is to run consul, then add the necessary values ​​via cli or http. Made this Dockerfile

FROM consul:latest
COPY kv-values.sh /kv-values.sh
RUN chmod 755 /kv-values.sh && sh -c ./kv-values.sh
EXPOSE 8300 8400 8500 8600
ENTRYPOINT ["/kv-values.sh"]

But this is how the docker swears:
sh: ./kv-values.sh: not found

The contents of the script:
spoiler
!/bin/bash

set -uex; \
consul agent -server --bootstrap -data-dir ./consul/data & \
let "timeout = $(date +%s) + 15"; \
while ! curl -f -s 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 sql/config/addr db; \
consul kv put sql/config/port 1433;
...

If the contents of the script are inserted into the Dockerfile in RUN, then the commands pass and the build succeeds, but the container does not start (no such container). I suspect that this is due to the fact that I write the wrong ENTRYPOINT ["consul"].

In general, I will be grateful for advice on how to implement it correctly and what are my mistakes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wargych, 2021-01-02
@wargych

Found 3 ways.
1. The first and correct from the point of view of the appointment of the consul is described here: we
raise the cluster for use as a discovery service and then fill it with data from the client.
https://github.com/deployable/docker-consul
2. The way we have stopped so far is to store data in volume. There are rights issues when deploying a project to a new environment.
3. A crooked and incorrect, but working way is to substitute your script as an entrypoint, which launches 2 scripts - one in the background pings the rise of the consul and fills it with data, the second actually raises the consul itself.
Sample contents of files:

docker-compose.yml
version: '3.2'
services:
consul:
build:
context: ./
dockerfile: Dockerfile
ports:
- 8500:8500
entrypoint: sh -c "cd /confrun/ && sh kvconf.sh"

Dockerfile

FROM consul:latest
COPY ./confrun/ /confrun/
RUN chmod 755 -R /confrun/
EXPOSE 8300 8400 8500 8600
ENTRYPOINT sh -c "cd /confrun/ && sh kvconf.sh"

kvconf.sh

#!/usr/bin/dumb-init /bin/sh
sh ./subscriber.sh &
sh ./runner.sh

subscriber.sh

#!/bin/bash
let "timeout = $(date +%s) + 15"; \
while ! curl -f -s 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 sql/config/addr db;
...все нужные команды. Чтобы избежать повторного заполнения при старте, нужно дописать consul get с проверкой на значение...

runner.sh

#!/bin/bash
consul agent -server -ui -bind 0.0.0.0 -client 0.0.0.0 -bootstrap-expect 1 -data-dir /consul/data -config-dir /consul/config

Well, in principle, instead of runner.sh, it would be more correct to substitute the standard entrypoint.sh consul script.
This path was not completed to the end, because. still too crutch in my opinion, but everything started and the values ​​​​were added.

S
Sergey Brovko, 2021-12-09
@cyber01

Purely by docker error:

COPY kv-values.sh /kv-values.sh
RUN chmod 755 /kv-values.sh && sh -c ./kv-values.sh

You copied the script to the root, did a chmod for it at the root too, but are you running it from the current directory? remove the dot in the sh -c ./kv-values.sh command.
Well, in addition to everything - is there bash in consul:latest at all? Or just sh?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question