Reputation: 17072
I have the following Dockerfile that only installs mongodb 3
# Start with docker's base ubuntu image
FROM ubuntu:14.04.2
# Mongodb prerequisite
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
# Update package sources
RUN apt-get -y update
# Install Mongodb binaries
RUN apt-get -y install mongodb-org
# Copy configuratio file
COPY mongod.conf /etc/mongod.conf
# Create mongo data path (will be mapped to a volume on host machine later on)
RUN mkdir -p /data/db
RUN chown -R mongodb:mongodb /data/db
# Expose MongoDB port
EXPOSE 27017
# Run mongo with mongodb user
USER mongodb
# Run mongod using provided configuration file
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--config", "/etc/mongod.conf"]
I create the image with
sudo docker build -t mongod .
I run the container with
sudo docker run -d -P mongod
And verify it's started
> sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43c75e7e44b3 mongod:latest "/usr/bin/mongod --c 3 minutes ago Up 3 minutes 0.0.0.0:49165->27017/tcp silly_mccarthy
When I run a mongo client from the container, it can connect without any error:
> sudo docker exec -ti 43c75e7e44b3 bash
$ mongo
MongoDB shell version: 3.0.1
connecting to: test
Welcome to the MongoDB shell.
....
My host is a Ubuntu 14.04 box and the network interfaces / bridges are
docker0 Link encap:Ethernet HWaddr 63:54:7b:f3:47:33
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
....
em1 Link encap:Ethernet HWaddr c4:2f:e3:64:ae:7c
inet addr:192.168.1.101 Bcast:192.168.1.255 Mask:255.255.255.0
....
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
....
From my host, I cannot connect to the mongod container
> mongo --port 49165
MongoDB shell version: 3.0.1
connecting to: 127.0.0.1:49165/test
2015-03-21T18:14:55.936+0100 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:49165
2015-03-21T18:14:55.936+0100 I NETWORK SocketException: remote: 127.0.0.1:49165 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:49165]
2015-03-21T18:14:55.936+0100 I NETWORK DBClientCursor::init call() failed
2015-03-21T18:14:55.937+0100 E QUERY Error: DBClientBase::findN: transport error: 127.0.0.1:49165 ns: admin.$cmd query: { whatsmyuri: 1 }
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Is there something I'm missing here ?
Upvotes: 0
Views: 2672
Reputation: 20703
MongoDB's default configuration is to bind mongod
to the IP of localhost
. Search for the following line in your mongod.conf
:
bindIp = 127.0.0.1
If you comment this line out using a hash like this
# bindIp = 127.0.0.1
then mongod
will bind to all IPs available during startup. If you set a specific IP like this
bindIp = 192.168.0.42
then mongod
binds only to this specific IP address and will only be available to hosts which can access that IP on the specified port.
Upvotes: 1