Docker is becoming main streamline to package and deploy self sufficient application containers. It wrap up a piece of software in a complete file system that contains everything it needs to run: code, run-time, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in. The same Linux kernel and libraries can be shared between multiple containers running on the host.
Please visit my blog previous blog Install Docker to setup docker.
Let’s Started
Docker hub provides images for all the software and tools. let’s find out the MongoDB docker image.
krishna@ubuntu:~$ sudo docker search mongodb NAME DESCRIPTION STARS OFFICIAL AUTOMATED mongo MongoDB document databases provide high av... 3212 [OK] tutum/mongodb MongoDB Docker image – listens in port 270... 193 [OK] frodenas/mongodb A Docker Image for MongoDB 13 [OK] bitnami/mongodb Bitnami MongoDB Docker Image 11 [OK] azukiapp/mongodb Docker image to run MongoDB by Azuki - htt... 3 [OK] appelgriebsch/mongodb Configurable MongoDB container based on Al... 3 [OK] tianon/mongodb-mms https://mms.mongodb.com/ << more informati... 2 [OK] cpuguy83/mongodb 2 [OK] tozd/mongodb Base image for MongoDB server. 2 [OK] tozd/meteor-mongodb MongoDB server image for Meteor applications. 1 [OK] bigtruedata/php-mongodb PHP image with MongoDB support 1 [OK] tomiti/mongodb MongoDB 3.2.0 0 [OK] bigm/mongodb mongodb 0 [OK] anapsix/mongodb MongoDB in a box 0 [OK] nanobox/mongodb MongoDB service for nanobox.io 0 [OK] vger/mongodb MongoDB, based on Debian Jessie 0 [OK] qnib/mongodb MongoDB image of QNIBTerminal 0 [OK] knight42/mongodb Dockerized MongoDB 0 [OK] recteurlp/mongodb Fedora DockerFile for MongoDB 0 [OK] konstruktoid/mongodb MongoDB 3.X with WiredTiger 0 [OK] tcaxias/mongodb Percona's MongoDB on Debian. Storage Engin... 0 [OK] markusma/mongodb markusma/mongodb trusted build 0 [OK] radiantwf/mongodb-enterprise MongoDB Enterprise Docker image 0 [OK] spidasoftware/mongodb Our mongodb image 0 [OK] radiantwf/mongodb MongoDB Enterprise Docker image 0 [OK] krishna@ubuntu:~$
There are many MongoDB Docker images available, but it is always better to go with the official release. As they are highly optimised, bug free and stable. You can download to your computer using pull command. After that we can list down the docker images.
krishna@ubuntu:~$ sudo docker pull mongo Using default tag: latest latest: Pulling from library/mongo 47e616392c23: Pull complete b8dc044ce904: Pull complete e32719290561: Pull complete 02d8f1b053c5: Pull complete d836acd00557: Pull complete 84f67488220c: Pull complete de17099d47ff: Pull complete 4a180e6abe11: Pull complete fc25b86cb5cf: Pull complete 6400c13c01f6: Pull complete d7c7ea74f012: Pull complete Digest: sha256:b37aaa85f165a7a6be115a16cfef2115d7033650af8a1a839fcb494c9df5d9e1 Status: Downloaded newer image for mongo:latest krishna@ubuntu:~$ krishna@ubuntu:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE mongo latest 6329fba85f65 5 days ago 360MB mysql latest d5127813070b 2 weeks ago 407MB krishna@ubuntu:~$
Run MongoDB Container
After an image has been downloaded, you may then run a container using the downloaded image with the run
sub command. If an image has not been downloaded when docker
is executed with the run
sub command, the Docker client will first download the image, then run a container using it.
krishna@ubuntu:~$ sudo docker run --name nosql -d mongo [sudo] password for krishna: eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711 krishna@ubuntu:~$ krishna@ubuntu:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eb76dcc46cd7 mongo "docker-entrypoint..." 19 seconds ago Up 18 seconds 27017/tcp nosql krishna@ubuntu:~$ krishna@ubuntu:~$ sudo docker logs nosql about to fork child process, waiting until server is ready for connections. forked process: 17 2017-04-30T14:27:24.168+0000 I CONTROL [main] ***** SERVER RESTARTED ***** 2017-04-30T14:27:24.174+0000 I CONTROL [initandlisten] MongoDB starting : pid=17 port=27017 dbpath=/data/db 64-bit host=eb76dcc46cd7 2017-04-30T14:27:24.174+0000 I CONTROL [initandlisten] db version v3.4.4 2017-04-30T14:27:24.174+0000 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] allocator: tcmalloc 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] modules: none 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] build environment: 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] distmod: debian81 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] distarch: x86_64 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] target_arch: x86_64 2017-04-30T14:27:24.175+0000 I CONTROL [initandlisten] options: { net: { bindIp: "127.0.0.1", port: 27017, ssl: { mode: "disabled" } }, processManagement: { fork: true, pidFilePath: "/tmp/tmp.3p07k4PLBz" }, systemLog: { destination: "file", logAppend: true, path: "/proc/1/fd/1" } } 2017-04-30T14:27:24.179+0000 I STORAGE [initandlisten] 2017-04-30T14:27:24.179+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2017-04-30T14:27:24.179+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2017-04-30T14:27:24.179+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=488M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2017-04-30T14:27:24.209+0000 I CONTROL [initandlisten] 2017-04-30T14:27:24.209+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-04-30T14:27:24.209+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-04-30T14:27:24.209+0000 I CONTROL [initandlisten] 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-04-30T14:27:24.210+0000 I CONTROL [initandlisten] 2017-04-30T14:27:24.222+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' 2017-04-30T14:27:24.236+0000 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" } 2017-04-30T14:27:24.236+0000 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM 2017-04-30T14:27:24.237+0000 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs 2017-04-30T14:27:24.237+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4 child process started successfully, parent exiting 2017-04-30T14:27:24.239+0000 I NETWORK [thread1] waiting for connections on port 27017 2017-04-30T14:27:24.279+0000 I NETWORK [thread1] connection accepted from 127.0.0.1:37596 #1 (1 connection now open) 2017-04-30T14:27:24.279+0000 I NETWORK [conn1] received client metadata from 127.0.0.1:37596 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.4" }, os: { type: "Linux", name: "PRETTY_NAME="Debian GNU/Linux 8 (jessie)"", architecture: "x86_64", version: "Kernel 4.4.0-62-generic" } } /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* 2017-04-30T14:27:24.282+0000 I - [conn1] end connection 127.0.0.1:37596 (1 connection now open) killing process with pid: 17 2017-04-30T14:27:24.295+0000 I CONTROL [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends 2017-04-30T14:27:24.295+0000 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets... 2017-04-30T14:27:24.295+0000 I NETWORK [signalProcessingThread] closing listening socket: 7 2017-04-30T14:27:24.295+0000 I NETWORK [signalProcessingThread] closing listening socket: 8 2017-04-30T14:27:24.295+0000 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock 2017-04-30T14:27:24.295+0000 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog... 2017-04-30T14:27:24.295+0000 I FTDC [signalProcessingThread] Shutting down full-time diagnostic data capture 2017-04-30T14:27:24.295+0000 I STORAGE [signalProcessingThread] WiredTigerKVEngine shutting down 2017-04-30T14:27:24.317+0000 I STORAGE [signalProcessingThread] shutdown: removing fs lock... 2017-04-30T14:27:24.317+0000 I CONTROL [signalProcessingThread] now exiting 2017-04-30T14:27:24.317+0000 I CONTROL [initandlisten] shutting down with code:0 MongoDB init process complete; ready for start up. 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=eb76dcc46cd7 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] db version v3.4.4 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] allocator: tcmalloc 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] modules: none 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] build environment: 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] distmod: debian81 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] distarch: x86_64 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] target_arch: x86_64 2017-04-30T14:27:25.312+0000 I CONTROL [initandlisten] options: {} 2017-04-30T14:27:25.316+0000 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=488M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.714+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' 2017-04-30T14:27:25.715+0000 I NETWORK [thread1] waiting for connections on port 27017 krishna@ubuntu:~$
Looks good. Our MongoDB Container is running now. As per the details from above logs.
Let’s verify the status of the container.
krishna@ubuntu:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eb76dcc46cd7 mongo "docker-entrypoint..." 19 seconds ago Up 18 seconds 27017/tcp nosql krishna@ubuntu:~$
MongoDB container is now running and accessible on port 27017 of that container. Use the following command to see what happened during the container startup:
krishna@ubuntu:~$ sudo docker logs nosql
Connecting to Container
Next, we can get detailed info about the MongoDB container named nosql. Run the inspect command:
krishna@ubuntu:~$ sudo docker inspect nosql [ { "Id": "eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711", "Created": "2017-04-30T14:27:23.935832101Z", "Path": "docker-entrypoint.sh", "Args": [ "mongod" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2254, "ExitCode": 0, "Error": "", "StartedAt": "2017-04-30T14:27:24.156770808Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:6329fba85f659b362927801fb9a3baea98688791d01630ea7b25d79deb9e0053", "ResolvConfPath": "/var/lib/docker/containers/eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711/resolv.conf", "HostnamePath": "/var/lib/docker/containers/eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711/hostname", "HostsPath": "/var/lib/docker/containers/eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711/hosts", "LogPath": "/var/lib/docker/containers/eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711/eb76dcc46cd7523d8aa3ba67b2070a39f552ac62527bad61d23c42ed837cd711-json.log", "Name": "/nosql", "RestartCount": 0, "Driver": "aufs", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "docker-default", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": null, "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": -1, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0 }, "GraphDriver": { "Data": null, "Name": "aufs" }, "Mounts": [ { "Type": "volume", "Name": "086fcbfddde782626c02ee60162bcb42a4006183f3741b5cd6b5e6c7152b9c07", "Source": "/var/lib/docker/volumes/086fcbfddde782626c02ee60162bcb42a4006183f3741b5cd6b5e6c7152b9c07/_data", "Destination": "/data/configdb", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "8f2bf17d9c4844375b85c7905ab9dc6d146e11f1a363c32abbe6d626c693411e", "Source": "/var/lib/docker/volumes/8f2bf17d9c4844375b85c7905ab9dc6d146e11f1a363c32abbe6d626c693411e/_data", "Destination": "/data/db", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "eb76dcc46cd7", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "27017/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "GPG_KEYS=0C49F3730359A14518585931BC711F9BA15703C6", "MONGO_MAJOR=3.4", "MONGO_VERSION=3.4.4", "MONGO_PACKAGE=mongodb-org" ], "Cmd": [ "mongod" ], "ArgsEscaped": true, "Image": "mongo", "Volumes": { "/data/configdb": {}, "/data/db": {} }, "WorkingDir": "", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "ddcca10b550e7b0e5f77d586f32e3c16aad77a44cc6c662e7d812fb05012f8db", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "27017/tcp": null }, "SandboxKey": "/var/run/docker/netns/ddcca10b550e", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "39c24c51c904c61fdf6c376e17df2cb5aa5ba8209c6358fa700969c1a6a864b4", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "7a367ae13a2468b97511223b6e791f99e11d7d31821c0f42ad52466004531817", "EndpointID": "39c24c51c904c61fdf6c376e17df2cb5aa5ba8209c6358fa700969c1a6a864b4", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } } } } ] krishna@ubuntu:~$
One way to access mongodb is though the MongoDB Client.
Another Access Method
Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form
root@eb76dcc46cd7:/#
Now you may run any command inside the container.
krishna@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eb76dcc46cd7 mongo "docker-entrypoint..." 12 minutes ago Up 12 minutes 27017/tcp nosql krishna@ubuntu:~$ krishna@ubuntu:~$ sudo docker exec -it nosql bash root@eb76dcc46cd7:/# root@eb76dcc46cd7:/# mongo MongoDB shell version v3.4.4 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.4 Server has startup warnings: 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2017-04-30T14:27:25.316+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-04-30T14:27:25.712+0000 I CONTROL [initandlisten] > >
Conclusion
VOWW…. We now have a MongoDB instance running in a container.
Docker allocates a dynamic IP address on every running container. Whenever a container is restarted, you will get a new IP address. You can get the IP address range from the Docker network interface in the Linux box. To sort this issue there is an option called –link
Pingback: MongoDB Backup & Restore in Docker Container - Exabig
Pingback: Trasnfer git repository from one provider to another - Exabig