Setting up a cluster and a bucket programmatically through Docker

Hello there !

I am trying to install a Couchbase instance in my local docker stack (using Docker-compose) and, if necessary, be able to set it up (creating the cluster and the bucket) by script.

Right now I’m able to start a CB Container and, by loging to the container, I can run some bash script that sets up the container to my test. Here’s the script :

couchbase-cli cluster-init -c localhost \
    --cluster-username administrator \
    --cluster-password password \
    --services data,index,query \
    --cluster-ramsize 2048 \
    --cluster-index-ramsize 256

couchbase-cli bucket-create \
    --cluster localhost \
    --username administrator \
    --password password \
    --bucket test \
    --bucket-type couchbase \
    --bucket-ramsize 1024 \
    --max-ttl 500000000 \
    --durability-min-level persistToMajority \
    --enable-flush 0

This is text-book example from the documentation btw. Nothing fancy.

As said previously, I let the docker-compose up start everything, then I can just log into the container (through Portainer) and execute my commands inside. It works like a charm.

Now I want to execute that script automatically. The issue being that it seems to crash when I put it in the Entrypoint or some post entrypoint script because it tries to reach the server while it isn’t completly started. Not sure tho, I don’t have much output from the container.

Do you have an idea on how I can trigger with certainty my script when everything runs ?


As part of the Couchbase Fluent Bit integration testing I also do something similar: couchbase-fluent-bit/tools/integration-test at main · couchbase/couchbase-fluent-bit · GitHub

This uses a custom entrypoint but you need to ensure that Couchbase Server is started as you say so it polls for the REST endpoint: couchbase-fluent-bit/ at main · couchbase/couchbase-fluent-bit · GitHub

The other alternative, which is more useful in a multinode set up is to run another server container purely to configure the cluster which can be started after the others and wait for them all.

I just managed to achieve similar results (but a bit more… dirty), basing myself on Create a couchbase bucket during build of the container – Madhur Ahuja

(I mostly switched from curl to couchbase-cli but the logic stays the same)

Still, I really like how loop and check if the server is up instead of a basic sleep.

Yeah this is going to be massively sensitive to timing so I would strongly recommend not relying on a sleep. By definition you also have to wait longer than required (or it’ll break) so it will be slower too.

The approach I took with the compose file lets me run different server versions just by specifying a variable for the version. If you’re building it into a container then you’ll need to build a custom one each time.