Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create bucket during build of the container? #27

Open
dvomartin opened this issue Jan 12, 2016 · 13 comments
Open

How to create bucket during build of the container? #27

dvomartin opened this issue Jan 12, 2016 · 13 comments

Comments

@dvomartin
Copy link

Hi,
is it possible to configure couchbase inside a container?
I have docker-compose.yml with definition of our cloud (several apps in java, redis, dynamodb, ...).
I would need to configure couchbase db during build of the container because application in another container requires it configured to run.

I have tried to put inside my dockerfile something like this:
FROM couchbase/server:latest
RUN /opt/couchbase/bin/couchbase-cli bucket-create -c localhost -u Administrator -p administrator --bucket=default --bucket-type=couchbase --bucket-ramsize=128 --bucket-replica=1 --wait

But whatewer host i put into -c parameter (localhost, container IP, container ID), I always get [Errno 111] Connection refused. I tried to restart couchbase service before this command and sleep for 10 seconds but nothing helps.

I know that I can use docker exec from outside the container to create bucket (that works fine for me), but I would really like to run whole cloud by just "docker-compose up" command.

Am I missing something? Is there a way how to make this work?

@arun-gupta
Copy link

You need to configure your server before any buckets can be created.

https://github.com/arun-gupta/docker-images/blob/master/couchbase/Dockerfile shows how to configure the Couchbase server and load travel-sample bucket using the REST APIs.

@dvomartin
Copy link
Author

Thank you.

It works!

But I have one more question.
I would like to restore bucket data after setup.
I need to use cbrestore for this. But when I run it in container, there is again error message "could not access REST API.... exception: [Errno 111] Connection refused".
I'm sure that my command is correct - when I run it using docker exec or when I connect into container it works just fine.

Any suggestion how to solve this issue?

@arun-gupta
Copy link

What is the exact command you are using?

@dvomartin
Copy link
Author

I use this command:
RUN cbrestore --bucket-source=default --bucket-destination=default ./2016-01-06T123746Z/2016-01-06T123746Z-full http://127.0.0.1:8096 -u Administrator -p administrator

Password is correct (I changed it in configure-cluster.sh). I also created cluster via REST API because even when was my server configured I couldn't run command from my first comment in this topic - server was still returning connection refused.

Could be this problem caused by user rights or permissions? I'm running Docker on Windows and I have read that it can cause some issues.
Because when I attach to running container and run this command manually, it works.

@arun-gupta
Copy link

I've never personally tried Docker on Windows. Are you using Docker Machine on Windows?

@dvomartin
Copy link
Author

Yes, I'm.

I will try to install docker on virtual machine with linux and see what happens.

@dvomartin
Copy link
Author

Ok, so I have tried to run my Dockerfile under Linux and there is the same issue.
I cannot access couchbase server during Dockerfile build. I have tried to access it just by telnet and again - connection refused.

@dvomartin
Copy link
Author

It seems I have found a solution.
If I place command inside configure-cluster.sh script, it works. It has probably something to do with entrypoint.sh script which is placed inside :)

@tleyden
Copy link
Contributor

tleyden commented Jan 14, 2016

I also created cluster via REST API because even when was my server configured I couldn't run command from my first comment in this topic - server was still returning connection refused.

This sounds like a networking issue. Where are you running this command? Inside or outside the docker container?

If you are running it from outside, you'll either need to expose some ports or spawn the container with --net host

@tleyden
Copy link
Contributor

tleyden commented Jan 14, 2016

Oh, sorry, n/m, I didn't read this carefully enough.

RUN /opt/couchbase/bin/couchbase-cli bucket-create -c localhost -u Administrator -p administrator --bucket=default --bucket-type=couchbase --bucket-ramsize=128 --bucket-replica=1 --wait

Yeah that won't work because there's no couchbase server process running while the docker image is being built, and couchbase-cli is just a wrapper around the REST calls to the running server.

I'd advise against trying to pre-bake buckets at build-time, it feels wrong to me. I haven't done much with docker compose yet, but if you can setup container dependencies, you could do something like this:

Spin up the following containers:

  • couchbase-server
  • couchbase-setup
  • your-app

couchbase-setup would only start after couchbase-server has been launched, and it would be able to access it via a hostname or ip address (say, via docker networking), and then it would issue the cocuhbase-cli and/or REST commands to initialize it and create a bucket and so forth. Afterwards, the your-app container would start up and it would also be able to access the couchbase-server container.

Caveat: the couchbase-setup container will have to wait a little bit (say 5-10s to be safe), because I don't think the couchbase server process is ready to accept requests immediately after the container is launched.

@ceejatec
Copy link
Contributor

Interesting thread. I'd suggest the couchbase-setup container should probably poll for the server being up since the startup delay can be pretty variable. Also, Traun, any clever thoughts for how that setup would work the second time you start it up? couchbase-setup would have to know not to try doing any configuration and just exit.

Perhaps there's an opportunity here for a little Couchbase helper container here to encapsulate that logic...

@dvomartin
Copy link
Author

@tleyden Thank you for the explanation and your suggestions.

I think that I could do that this way but as @ceejatec said, I would need to check if is my server already set up because I don't want to rewrite user data when I run my application for the second time.

I think that I will have to run commands in shell script and check if bucket already exists (for example). If so I can quit the script and don't setup server. What do you think?

@brettporter
Copy link

Did anyone make progress on this since the last comment?

I've just done the same thing using a similar approach to @arun-gupta above, with https://github.com/vishnubob/wait-for-it/blob/master/wait-for-it.sh to wait for start and basing first-time init on the existence of the couchbase data directory before starting the server.

I'd be interested in contributing something back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants