MongoDB starts succesfully in the container. Now we need to commit the container, to save the state and push to the docker index.
sudo docker commit a1f0680f8458 codiez/mongodb
Remember to replace your container id, mine was a1f0680f8458 which you can see at the prompt, and your <username>/<container_name>
12345678910111213141516
dock@saas:~$ sudo docker commit a1f0680f8458 codiez/mongodb
dock@saas:~$ sudo docker login
Username: codiez
Password:
Email: ismail@codiez.co.za
Login Succeeded
dock@saas:~$ sudo docker push codiez/mongodb
The push refers to a repository [codiez/mongodb](len: 1)Processing checksums
Sending image list
Pushing repository codiez/mongodb (1 tags)Pushing 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c
Image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c already pushed, skipping
Pushing tags for rev [8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c] on {https://registry-1.docker.io/v1/repositories/codiez/mongodb/tags/latest}Pushing f0ab8043e4e8135379d35410a4847769efb9245d8d4817cb24a2196c434c8506
Once that completes, we can start MongoDB in the container, and map it to a port by running:
sudo docker run -d -p 27017 codiez/mongodb /usr/bin/mongod --smallfiles
This basically runs the container, based on the codiez/mongodb image, with the command /usr/bin/mongod and maps the default mongodb port 27017 to an external port.
Check that the container is running and get the port:
123
dock@saas:~$ sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
a1f0680f8458 codiez/mongodb:latest /usr/bin/mongod 5 seconds ago Up 4 seconds 49157->27017
You can also inspect the image to grab the port by running sudo docker inspect container_id
Now, we can test an external connection to MongoDB, test from your local machine and connect to the VM IP with the port for your container:
123456789
$ mongo 192.168.0.21:49157
MongoDB shell version: 2.4.5
connecting to: 192.168.0.21:49157/test
> db
test> db.posts.insert({title:"Hello MongoDB in Docker"})> db.posts.find(){"_id" : ObjectId("5227058d112c68baaa3b94d9"), "title" : "Hello MongoDB in Docker"}>
Update: Stop the container and restart it since we do not want to commit with the test data.
One final commit to save the command and port mapping. Also notice you do not have to enter in the entire container id when running commands, just the first few characters.
1234
dock@saas:~$ sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
298a43e2f98e codiez/mongodb:latest /usr/bin/mongod --sm 35 seconds ago Up 34 seconds 49164->27017
dock@saas:~$ sudo docker commit -run '{"Cmd": ["/usr/bin/mongod", "--smallfiles"], "PortSpecs": [":27017"]}' 298a4 codiez/mongodb
Now that we have an image, we can run docker pull codiez/mongodb to grab the container and run it with docker run -d codiez/mongodb