Is it possible to configure sync gateway with environment variables? I am trying to deploy sync gateway (and CB server itself) in Docker containers where the config.json file is baked into the container through the Dockerfile. I would like to have CB server that the SG points to injected into the container as an environment variable. That way, I can use the same container in dev, test, and prod by injecting the DB URLs into the environment, and not having to have environment specific config.json files. I can’t use command line configuration since I have some unsupported options (like an event handler where I would like to inject the URL).
I don’t believe you can do this. @traun is our resident expert on Docker; maybe he knows a different way to configure SG in a container?
What about simply wrapping the startup with a script that takes your envvars and creates the needed config.json?
Having similar thoughts and as I only wanted to dynamically add one variable I wrote an ugly one liner at the end of my dockerfile
CMD sed 's/$COUCHBASE_PORT_8091_TCP_ADDR/'"$COUCHBASE_PORT_8091_TCP_ADDR"'/' /etc/sync_gateway/sync_conf_temp.json > /etc/sync_gateway/sync_conf.json && /usr/local/bin/sync_gateway /etc/sync_gateway/sync_conf.json
Hey @paulharter – I just filed an enhancement request for Sync Gateway here:
(you can subscribe to the issue to receive updates)
However, if the only customization you need is for Sync Gateway to be able to contact the Couchbase Server, the cleanest approach is to use is probably Docker Networks. (Docker container links used to be the way to do this, but Docker Networks has replaced this)
More info: https://docs.docker.com/engine/userguide/networking/dockernetworks/
@paulharter try the following:
docker run -itd --net=app --name=cbserver couchbase/server
docker run -p 4984:4984 -p 4985:4985 -itd --net=app --name=sg couchbase/sync-gateway https://git.io/vzd7M
That will create a custom bridge network named “app” and put both containers in the network so that they can reach each other by hostname. The Sync Gateway container can reach the Couchbase Server container by the “cbserver” hostname, which is what is specified in the config file at https://git.io/vzd7M
@traun Thanks for this.
Using docker networks does look neater. I’m using CB in a container for dev and testing but not currently in production, so have a static address for the production server. Maybe I could give this address an alias that docker in aware of.
Is there still a reason not to use the Couchbase docker in production, or should it be fine now?
I guess templating the config json would still be a useful mechanism if it wan’t too hard to add.
Do you know if environment variable support inside sync gateway config ever made it in? I hoped this would work: “server”:“http://$DOCKERCLOUD_IP_ADDRESS:8091”, but it doesn’t. I can’t use the name of the service as there are 3 couchbase services in play, couchbase, couchbase-1, couchbase-2, couchbase-3, so that value is going to give me the 3 ip addresses. I tried to use localhost:8091 in the config as the sync gateway container and couchbase container are on the same node. Using net: bridge, it’s not reachable… thought they would be able to communicate on localhost but I’m quite new to docker cloud so still figuring out the right setup.
Hope you got this working. Bridge sets up its own virtual network, so you need to use the IP addresses from there.