1. Create a new Dockerfile file
Dockerfile
| 12345678910111213141516 |
# VERSION 0.0.1FROM ubuntu:latestMAINTAINER lanhong Turnbull "[email protected]"# Add 10gen official apt source to the sources listRUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list# Hack for initctl not being available in UbuntuRUN dpkg-divert --local --rename --add /sbin/initctlRUN ln -s /bin/true /sbin/initctl# Install MongoDBRUN apt-get updateRUN apt-get install mongodb-10gen# Create the MongoDB data directoryRUN mkdir -p /backup/dockerRepository/mongodb/data/dbEXPOSE 27017ENTRYPOINT ["usr/bin/mongod"] |
2. Compile the image and execute the following command in the directory where the Dockerfile is located
| 1 |
sudodocker build -t lanhong/mongodb. |
3. MongoDB can be run as a management container and can be connected via local port
| 1234567891011 |
# Regular styleMONGO_ID=$(sudo docker run -d lanhong/mongodb)# Lean and mean(用这个命令)MONGO_ID=$(sudo docker run -d lanhong/mongodb --noprealloc --smallfiles --dbpath /backup/dockerRepository/mongodb/data/db)# Check the logs outsudo docker logs $MONGO_ID# Connect and play aroundmongod --port <port you get from `docker ps`> |
Here MONGO or Mongod command not, let install Apt-get installs mongodb-clients.
To set up the Docker external port, use the following command
| 1 |
MONGO_ID=$(docker run -d -p 5000:27017networld/mongodb --dbpath=/data/db) |
This allows you to see two ports in the boot Docker container, and then access the http://serverip:5000
4. Commit (save) the status of the container
Save the container state to the container image so that the state can be reused
| 12345 |
# Commit your container to a new named imagesudodocker commit <container_id> <some_name>e.g sudo docker commit afa886fad446 lanhong/mongodb# List your containerssudodocker images |
"Docker" compiles MongoDB image