Katlock
Katlock

Reputation: 1396

mongo disconnect after connecting to executing thru shell in docker

I want to close the mongo shell after executing the following in a docker command:

#!/bin/bash

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

if [ $? -eq 0 ]; then \
  echo "connected to mongo successful"; \
else \
  echo "mongo connection NOT successful"; \
fi; \

When it connects it goes to a shell prompt within mongo. Is there a way to pass a shell command to do an exit right in or after the docker command?

thanks

Upvotes: 1

Views: 338

Answers (1)

Webert Lima
Webert Lima

Reputation: 14025

Usually (of course it depends on the base image you're using) you wouldn't need to invoke "sh -c". Also, the -it combination is usually what makes the shell open and wait for input. Try to change your command a little bit, like below, without -it and sh -c:

docker run --link sonams-mongo:mongo --rm mongo mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"

if that doesn't help, try this:

echo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test" | docker run --link sonams-mongo:mongo --rm mongo mongo

Upvotes: 1

Related Questions