I'm attempting to create a MongoDB Docker container using a data directory on my host machine. This works just fine if I run docker
from the command line:
docker run \
--detach \
--name db_backup \
--publish 27017:27017 \
--volume $PWD/data:/backup/db \
mongo:latest \
mongod --dbpath /backup/db
I'm having a couple of problems converting the above command to a Dockerfile, particularly the --volume
argument:
- The backup directory can be quite big—upwards of 50GB—and it take a long time to build the image.
- When I use the
VOLUME
argument in theDockerfile
I'm not clear on how to put it in the right place so that I can reference it with the--dbpath
flag when executingmongod
.
Here's what my Dockerfile looks like at the moment.
FROM mongo:latest
VOLUME ./data
EXPOSE 27017 27017
CMD [ "mongod", "--dbpath", "/backup/db" ]
I should mention that I've added ./data to my .dockerignore
(not sure if thats ok) otherwise it take too long to build the image. This seems to work fine when using the --volume
flag.