Create a Docker data volume container
$ docker create --name dbdata -v /dbdata mongo /bin/true
Start mongo, expose port to connect
$ 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 docker-machine ssh
takes a host
and a command
argument to execute on the host.
The following doesn't work:
$ docker-machine ssh demo 'docker run --rm --link mongo:mongo -v $(pwd):/backup mongo bash -c "mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR"'
SSH cmd error!
command: docker run --rm --link mongo:mongo -v $(pwd):/backup mongo bash -c "mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR"
err : exit status 3
output : 2015-09-15T16:30:20.460+0000 error parsing command line options: expected argument for flag `-h, --host'
2015-09-15T16:30:20.462+0000 try 'mongodump --help' for more information
Using separate steps, it works:
$ 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
The flag parsing terminator (--) can be used to get the ssh command to pass the entire command string, but now we get a different error:
$ 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
My latest attempt 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
http://stackoverflow.com/questions/32591809/docker-machine-ssh-command-for-mongodump
Thanks a lot, @nathanleclaire. I need to docker-machine scp the result from the host, so I got fixated on docker-machine ssh to execute the command. Now you have me wondering if I should get the result without docker-machine scp on the host as well...