sync_gateway with couchbase-server

I had the same problem, using Docker and Docker Compose.

Using Docker CLI (this scenario works):

  1. Start Couchbase Server:
    docker run -d -v ~/couchbaseserver-data:/opt/couchbase/var -p 8091:8091 --name couchbaseserver couchbase:community-4.0.0

  2. Setup the server and sync_gateway bucket.

  3. Start Couchbase Sync:
    docker run -d -p 4984:4984 -p 4985:4985 --link couchbaseserver:couchbaseserver couchbase/sync-gateway:1.1.0 http://www.example.com/config.json

Using Docker Compose (this scenario works, because I have added delay for 10 seconds “sleep 10” between Couchbase Server start and Couchbase Sync start):

docker-compose.yml:

syncgateway:
  image: couchbase/sync-gateway:1.1.0
  entrypoint: /bin/bash
  command: -c "sleep 10 && /usr/local/bin/sync_gateway /data/config/config.json"
  ports:
    - "4984:4984"
    - "4985:4985"
  volumes:
    - ~/syncgateway-data:/data
  links:
    - couchbaseserver:couchbaseserver
couchbaseserver:
  image: couchbase:community-4.0.0
  ports:
    - "8091:8091"
  volumes:
    - ~/couchbaseserver-data:/opt/couchbase/var

~/syncgateway-data/config/config.json:

{
  "interface": ":4984",
  "adminInterface": "0.0.0.0:4985",
  "log": [
    "*"
  ],
  "databases": {
    "sync_gateway": {
      "server": "http://couchbaseserver:8091",
      "bucket": "sync_gateway",
      "username": "sync_gateway",
      "password": "p4ssw0rd"
    }
  }
}
  1. First setup Couchbase Server and the bucket:
    docker-compose up and kill it later Ctrl + C
    or
    docker-compose start couchbaseserver
    when it’s ready stop it:
    docker-compose stop couchbaseserver

  2. Now start the server and sync again:
    docker-compose up

Everything will work, because there is a delay between the services, otherwise the same error:
Error opening database: 502 Unable to connect to server: HTTP error 401 Unauthorized is occurred.

Probably the docker image should be changed like adding delay options or maybe something else?
I can see there are a lot of examples here: https://github.com/couchbase/sync_gateway/tree/master/examples , but none of them are using docker compose + real server (not walrus)