0

Setup for the problem:

Create a data volume container

$ docker create --name dbdata -v /dbdata mongo /bin/true

Start mongo in a container linked to the data volume container

$ docker run -d --name mongo --volumes-from dbdata mongo

Verify you can connect to mongo using the mongo client

$ docker run -it --link mongo:mongo --rm mongo sh -c 'exec mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"'

The problem:

The docker-machine ssh takes a host and a command argument to execute on the host. I'd like to execute the following mongodump command, which works once I ssh into the docker host:

$ docker-machine ssh demo
root@demo:~# docker run --rm --link mongo:mongo -v $HOME:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR'
2015-09-15T16:34:02.676+0000    writing test.samples to /backup/test/samples.bson
2015-09-15T16:34:02.678+0000    writing test.samples metadata to /backup/test/samples.metadata.json
2015-09-15T16:34:02.678+0000    done dumping test.samples (1 document)
2015-09-15T16:34:02.679+0000    writing test.system.indexes to /backup/test/system.indexes.bson

However, using the docker-machine ssh command to execute the above command in a single step doesn't work for me:

$ docker-machine ssh demo -- docker run --rm --link mongo:mongo -v $HOME:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR'
SSH cmd error!
command: docker run --rm --link mongo:mongo -v /Users/tony:/backup mongo bash -c mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR
err    : exit status 1
output : 2015-09-15T16:53:07.717+0000   Failed: error connecting to db server: no reachable servers

So if the container to run the mongodump command can't connect to the mongo container, I figure there's probably an issue with --host $MONGO_PORT_27017_TCP_ADDR (it should be passed as is into the container, so premature expansion causing an empty string?), but a bit stumped trying to get it right. Any ideas are appreciated.


Update: I'm one step closer. The following appears to execute the command correctly, although the data isn't written to the system and the session hangs:

$ docker-machine ssh demo -- $(docker run --rm --link mongo:mongo -v $HOME:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR')
2015-09-15T18:02:03.347+0000    writing test.samples to /backup/test/samples.bson
2015-09-15T18:02:03.349+0000    writing test.samples metadata to /backup/test/samples.metadata.json
2015-09-15T18:02:03.349+0000    done dumping test.samples (1 document)
2015-09-15T18:02:03.350+0000    writing test.system.indexes to /backup/test/system.indexes.bson
Subfuzion
  • 1,789
  • 20
  • 18
  • I need to docker-machine scp the result from the host, so I got fixated on docker-machine ssh to execute the command. Thanks to Nathan LeClaire's (@upthecyberpunks) suggestion on Twitter, the best solution is to avoid this hassle altogether and run a container to execute the command: $ docker run --rm --link mongo:mongo -v /root:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR' I'm still curious about executing the cmd over ssh, but if no one provides a solution, I'll answer my own question with this one. – Subfuzion Sep 15 '15 at 19:01

2 Answers2

1

The question asked for a solution based on docker ssh, but since no one responded, I'll answer the question myself with what is a better solution anyway.

As suggested by Nathan LeClaire (@upthecyberpunks) to me over twitter, the better solution is to avoid the hassle altogether and simply run a container to execute the mongodump command.

$ docker run \
    --rm \
    --link mongo:mongo \
    -v /root:/backup mongo bash \
    -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR'

Not technically required for the answer, but the resulting test db backup file can then be transferred from the docker host machine to your current directory via docker scp:

$ docker-machine scp -r dev:/root/test .
Subfuzion
  • 1,789
  • 20
  • 18
0

Since I cannot add a comment to the orginal nice answer, just add a little explanation here, $MONGO_PORT_27017_TCP_ADDR should be the ip of our machine, for example, the virtual machine's ip in my virtualbox is 100.100.100.10, so the last line shoulb be:

-c 'mongodump --out /backup --host 100.100.100.10' or

-c 'mongodump --out /backup --host 100.100.100.10:27017'.

If not add the host field, great chances are that we will encounter some error like: *** Failed: error connecting to db server: no reachable servers.

And thanks again to the orginal answer ^_^.

Community
  • 1
  • 1
miaodx
  • 1
  • 2