1

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:

  1. The backup directory can be quite big—upwards of 50GB—and it take a long time to build the image.
  2. When I use the VOLUME argument in the Dockerfile I'm not clear on how to put it in the right place so that I can reference it with the --dbpath flag when executing mongod.

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.

Pardoner
  • 1,011
  • 4
  • 16
  • 28
  • 1
    You shouldn't need to do anything; a Dockerfile `VOLUME` directive isn't necessary here, and you can't use it to specify a specific host directory. Even if you build a custom image you will need to run something very similar to your original `docker run` command, including its `--publish` and `--volume` options. – David Maze May 12 '20 at 00:51
  • @DavidMaze so are the `--volume` flag and `VOLUME` argument unrelated? What is the difference? – Pardoner May 12 '20 at 01:15
  • 1
    `VOLUME` is an instruction to Docker to create and mount a new empty anonymous volume on the named directory if nothing else is mounted there. It comes with some confusing side effects, like preventing future `RUN` instructions from changing that directory. You can't control the name or physical location of the volume, and you can use `docker run -v` on any directory whether or not its declared as a `VOLUME`. You almost never need `VOLUME`. – David Maze May 12 '20 at 10:01

0 Answers0