1. Create a new Dockerfile file
Dockerfile
12345678910111213141516 |
# VERSION 0.0.1
FROM ubuntu:latest
MAINTAINER lanhong Turnbull
"[email protected]"
# Add 10gen official apt source to the sources list
RUN apt-key adv --keyserver hkp:
//keyserver
.ubuntu.com:80 --recv 7F0CEB10
RUN
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 Ubuntu
RUN dpkg-divert --
local --rename --add
/sbin/initctl
RUN
ln -s
/bin/true /sbin/initctl
# Install MongoDB
RUN apt-get update
RUN apt-get
install mongodb-10gen
# Create the MongoDB data directory
RUN
mkdir -p
/backup/dockerRepository/mongodb/data/db
EXPOSE 27017
ENTRYPOINT [
"usr/bin/mongod"
]
|
2. Compile the image and execute the following command in the directory where the Dockerfile is located
1 |
sudo docker build -t lanhong /mongodb . |
3. MongoDB can be run as a management container and can be connected via local port
1234567891011 |
# Regular style
MONGO_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 out
sudo docker logs $MONGO_ID
# Connect and play around
mongod --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 : 27017 networld/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 image sudo docker commit <container_id> <some_name> e.g sudo docker commit afa886fad446 lanhong /mongodb # List your containers sudo docker images |
"Docker" compiles MongoDB image