Howto persist MongoDB - data of a running Docker container in a new image?

后端 未结 2 573
我寻月下人不归
我寻月下人不归 2021-02-04 14:08

i have a running Mongo DB Container called xyz from offical Mongo Image. i created the container with docker run -d -p 21707:21707 mongo In this container i create

2条回答
  •  梦如初夏
    2021-02-04 15:09

    I wanted to make an image to test things around authentication and I has the same issue. I solved it this way:

    Dockerfile

    FROM mongo
    
    COPY setup.sh /
    RUN chmod +x /setup.sh
    RUN /setup.sh
    
    ENTRYPOINT ["/usr/bin/mongod"]
    CMD ["--auth", "--dbpath=/containerdb"]
    

    setup.sh

    #!/bin/bash
    
    mkdir containerdb
    
    mongod --auth --dbpath=/containerdb &
    
    until mongo admin --eval 'db.createUser({"user":"root", "pwd":"password", "roles": [ "root" ] })'
    do
      echo 'Mongo not yet ready, waiting 1 sec until retry'
      sleep 1
    done
    
    mongod --shutdown --dbpath=/containerdb
    

    Bim! When you run this image you have a mongodb with authentication activated and a root user. The key is to workaround the VOLUME in the base Dockerfile by storing the data somewhere else.

提交回复
热议问题