Reputation: 4254
I have script that restores the database restore.sh
:
mongorestore --port 27017 --db myapp `pwd`/db-dump/myapp
I want to run this in a short lived docker-container using the image mvertes/alpine-mongo.
To run a shortlived container the --rm
is used:
docker run --rm --name mongo -p 27017:27017 \
-v /data/db:/data/db \
mvertes/alpine-mongo
But how do I execute my script in the same command?
Upvotes: 1
Views: 6811
Reputation: 4364
Check out the docker run reference:
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
You can pass in the command you wish to execute. In your case, this could be the restore script. You must consider two things, though.
CMD
directive in the Dockerfile.If you look at the Dockerfile, you see this as its last line:
CMD [ "mongod" ]
This means the default command that the container executes is mongod
. When you specify a command for docker run
, you "replace" this with the command you pass in. In your case: Passing in the restore script will overwrite mongod
, which means Mongo never starts and the script will fail.
You have two options:
Since you want to run this in a short-lived container, option 2 might be better suited for you. Just remember to start mongod
with the --fork
flag to run it in daemon mode.
$ docker run --rm --name mongo -p 27017:27017 \
-v /data/db:/data/db \
-v "$(pwd)":/mnt/pwd \
mvertes/alpine-mongo "mongod --fork && /mnt/pwd/restore.sh"
Hopefully, this is all it takes to solve your problem.
Upvotes: 1