MongoDB Docker镜像连接问题

huangapple go评论58阅读模式
英文:

MongoDB docker image connection issue

问题

我尝试在本地运行一个运行MongoDB的Docker容器,但遇到了一些问题。
我使用的是MacOS,这是我的Docker文件:

FROM mongo:4.4.10

RUN apt-get update && \
    apt-get install -y mongodb-database-tools

ENV MONGO_INITDB_ROOT_USERNAME admin
ENV MONGO_INITDB_ROOT_PASSWORD admin

EXPOSE 27017

CMD ["mongod", "--bind_ip_all"]

但当我尝试在容器内部连接Mongo时,我得到了以下错误:

root@1820984d904c:/# mongo
MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

我是这样运行容器的:

docker run -it -p 27017:27017 --rm --name containername imagename bash

有任何建议吗?

谢谢

英文:

Im try to run a local docker container running MongoDB but I got some issues.
Im using MacOS and this is my docker file:

FROM mongo:4.4.10

RUN apt-get update && \
    apt-get install -y mongodb-database-tools

ENV MONGO_INITDB_ROOT_USERNAME admin
ENV MONGO_INITDB_ROOT_PASSWORD admin

EXPOSE 27017

CMD ["mongod", "--bind_ip_all"]

But when I try to connect in mongo inside the container I got:

root@1820984d904c:/# mongo
MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

Im running the container like this:

docker run -it -p 27017:27017 --rm --name containername imagename bash

Any ideas?

Thanks

答案1

得分: 1

你没有在docker run命令中启动Mongo,而是启动了bash

根据https://docs.docker.com/engine/reference/run/#overriding-dockerfile-image-defaults:

> CMD(默认命令或选项)
>
> 回忆一下Docker命令行中的可选COMMAND:
>
> docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
> 这个命令是可选的,因为创建IMAGE的人可能已经在Dockerfile CMD指令中提供了默认的COMMAND。作为操作者(从该IMAGE运行容器的人),你可以通过指定一个新的COMMAND来覆盖那个CMD指令。

所以,使用以下命令启动你的Docker:

docker run -p 27017:27017 --rm --name containername imagename

然后使用以下命令进入容器的bash环境:

docker exec -it containername bash
英文:

You are not starting mongo with your docker run command. You are starting bash instead.

From https://docs.docker.com/engine/reference/run/#overriding-dockerfile-image-defaults:

> CMD (default command or options)
>
> Recall the optional COMMAND in the Docker commandline:
>
> docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
> This command is optional because the person who created the IMAGE may have already provided a default COMMAND using the Dockerfile CMD instruction. As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND.

So start your docker with

docker run -p 27017:27017 --rm --name containername imagename

Then bash in to the container with

docker exec -it containername bash 

huangapple
  • 本文由 发表于 2023年2月19日 05:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定