[英]Cannot connect to mongodb container from linux host
I have the following Dockerfile that only installs mongodb 3 我有以下仅安装mongodb 3的Dockerfile
# 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: 当我从容器运行mongo客户端时,它可以连接而没有任何错误:
> 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 我的主机是Ubuntu 14.04盒子,网络接口/网桥是
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 无法从主机连接到mongod容器
> 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 ? 我在这里想念什么吗?
MongoDB's default configuration is to bind mongod
to the IP of localhost
. MongoDB的默认配置是将
mongod
绑定到localhost
的IP。 Search for the following line in your mongod.conf
: 在
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. 然后
mongod
将绑定到启动过程中可用的所有 IP。 If you set a specific IP like this 如果您这样设置一个特定的IP
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. 然后
mongod
仅绑定到该特定IP地址,并且仅对可以在指定端口上访问该IP的主机可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.