Unable to start docker mongo image on windows

Although Augustas is right, but I found this answer particularly useful with docker-compose.yml.

What you can do as a workaround is:

  1. Create a .env file in the same path as your docker-compose file and store the current path in an environment variable like this:
MONGO_HOST_DATA=/Users/user123/MyMongoProject //path to mongo data folder (for C:\Users\user123\MyMongoProject)
  1. Now, in your docker-compose file, replace the above-mentioned variable:
    services:
      my-mongo-db:
      build: ./database
      ports:
       - 32815:27017
      volumes:
    - ${MONGO_HOST_DATA}/database/db:/data/db
  • If you want to see how the actual path replaced the environment variable, you can use the following command:
docker-compose config

Additionally, if you use any disk drive other than C:\ you might want to change the Docker file sharing settings:

Windows Docker settings window

Hope this helps.


WARNING (Windows & OS X): The default Docker setup on Windows and OS X uses a VirtualBox VM to host the Docker daemon. Unfortunately, the mechanism VirtualBox uses to share folders between the host system and the Docker container is not compatible with the memory mapped files used by MongoDB (see vbox bug, docs.mongodb.org and related jira.mongodb.org bug). This means that it is not possible to run a MongoDB container with the data directory mapped to the host.

Answer from docker mongo GitHub

Workaround to persist data:

docker volume create --name=mongodata
docker run -d -p 27017:27017 -v mongodata:/data/db --name=mymongo mongodb:3.3

More info here


If you are using docker-compose then following is the way replicate the answer above.

services:
  mongodb_container:
    ...
    volumes:
      - mongodata:/data/db
volumes:
  mongodata:

Please note that the sample above indicates only needed parts of the yml file for the mongodb storage.