mongodb docker 在不同的端口上运行 mongo 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42704844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:55:11  来源:igfitidea点击:

docker run mongo image on a different port

mongodbamazon-web-servicesdockeramazon-ec2docker-compose

提问by Mahshid Zeinaly

The short question is can I run mongo from mongo:latest image on a different port than 27017 (for example on 27018?)

简短的问题是我可以从 mongo:latest image 在与 27017 不同的端口上运行 mongo(例如在 27018 上?)

If yes, how can I do this inside a docker-compose.yml file in order ro be able to type the following command:

如果是,我如何在 docker-compose.yml 文件中执行此操作,以便 ro 能够键入以下命令:

docker-compose run

The longer story:

更长的故事:

I have an app running in AWS EC2 instance. The app consists of a mongodb and a web application. Now I decided to separate part of this app into its own microservice running in the same AWS inside docker container (two containers one for another mongo and one for a web app). I think the problem is I can not have mongodb running on port 27017 and at the same time another mongodb running inside a docker container on port 27017. Right? I have this assumption because when I stop the first mongo (my app mongo), my docker mongo works.

我有一个在 AWS EC2 实例中运行的应用程序。该应用程序由一个 mongodb 和一个 Web 应用程序组成。现在我决定将这个应用程序的一部分分离到它自己的微服务中,在 docker 容器内的同一个 AWS 中运行(两个容器,一个用于另一个 mongo,一个用于 Web 应用程序)。我认为问题是我不能让 mongodb 在端口 27017 上运行,同时另一个 mongodb 在端口 27017 上的 docker 容器内运行。对吗?我有这个假设,因为当我停止第一个 mongo(我的应用程序 mongo)时,我的 docker mongo 工作。

So I am trying to make the second mongo (the one that is inside the docker container), to run in a different port and my second web app (the one inside another docker conianter), to listen to mongo on a different port. Here is my attempt to change the docker-compose file:

所以我试图制作第二个 mongo(在 docker 容器内的那个),在不同的端口和我的第二个 web 应用程序(另一个 docker conianter 内的那个)中运行,在不同的端口上监听 mongo。这是我尝试更改 docker-compose 文件的尝试:

version: '2'
services:
    webapp:
        image: myimage
        ports:
            - 3000:3000

    mongo:
        image: mongo:latest
        ports:
            - 27018:27018

And inside my new app, I changed the mongo url to:

在我的新应用程序中,我将 mongo 网址更改为:

monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url, 27018)

Well, the same if I say:

好吧,如果我说同样的话:

monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url)

But when I run docker-compose run, it still does not work, and I get the following errors:

但是当我运行 docker-compose run 时,它仍然不起作用,并且出现以下错误:

ERROR: for mongo  driver failed programming external
connectivity on endpoint: Error starting userland proxy:
listen tcp 0.0.0.0:27017: bind: address already in use

Or

或者

pymongo.errors.ServerSelectionTimeoutError:
mongo:27018: [Errno -2] Name or service not known

采纳答案by Mahshid Zeinaly

Default communication between different containers running on the same host

运行在同一主机上的不同容器之间的默认通信

I solved the problem, not by running the container in a different port though, but by learning one new feature in docker-compose version 2 and that is we do not need to specify links or networks. The newly created containers by default will be part of docker0 network and hence they can communicate with each other.

我解决了这个问题,不是通过在不同的端口运行容器,而是通过学习 docker-compose 版本 2 中的一个新功能,那就是我们不需要指定链接或网络。默认情况下,新创建的容器将成为 docker0 网络的一部分,因此它们可以相互通信。

As Mattmentioned, we can run processes inside containers on the same port. They should be isolated. So the problem can not be that the docker container and the host are using the same port. The problem is perhaps there is an attempt to forward a used port in host to another port in container.

正如马特提到的,我们可以在同一个端口上的容器内运行进程。他们应该被隔离。所以问题不可能是docker容器和主机使用的是同一个端口。问题可能是尝试将主机中使用的端口转发到容器中的另一个端口。

Below is a working docker-compose file:

下面是一个有效的 docker-compose 文件:

version: '2'
services:
    webapp:
        image: myimage
        ports:
            - 3000:3000

    mongo:
        image: mongo:latest

I looked at the mongo:latest docker file in github, and realized they exposed 27017. So we do not need to change the port or forward host ports to the running mongo container. And the mongo url can stay on the same port:

我查看了 github 中的 mongo:latest docker 文件,发现他们暴露了 27017。所以我们不需要更改端口或将主机端口转发到正在运行的 mongo 容器。并且 mongo url 可以保持在同一个端口上:

monog_url = 'mongodb://mongo:27017'
client = MongoClient(monog_url, 27017)

Docker run an image on a different port

Docker 在不同的端口上运行镜像

So the above solution solved the problem, but as for the question title 'docker run mongo image on a different port', the simplest way is just to change the docker-compose to:

所以上面的解决方案解决了问题,但是对于问题标题'docker run mongo image on a different port',最简单的方法就是将docker-compose更改为:

version: '2'
services:
web:
    image: myimage

mongo:
    image: mongo:latest
    command: mongod --port 27018

Mongo is now running on 27018 and the following url is still accessible inside the web:

Mongo 现在在 27018 上运行,以下 url 仍然可以在 web 中访问:

monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url, 27018)

回答by Vince Bowdren

You can tell MongoDB to listen on a different portin the configuration file, or by using a command-line parameter:

您可以告诉 MongoDB侦听配置文件中的不同端口,或使用命令行参数:

services:
  mongo:
    image: 'mongo:latest'
    command: mongod --port 27018
    ports:
        - '27018:27018'

回答by Matt

You can run processes inside a container and outside on the same port. You can even run multiple containers using the same port internally. What you can't do is map the one port from the host to a container. Or in your, map a port that is already in use to a container.

您可以在容器内部和外部在同一端口上运行进程。您甚至可以在内部使用同一个端口运行多个容器。你不能做的是将一个端口从主机映射到一个容器。或者在您的,将已在使用的端口映射到容器。

For example, this would work on your host:

例如,这适用于您的主机:

services:
    webapp:
        image: myimage
        ports:
            - '3000:3000'

    mongo:
        image: 'mongo:latest'
        ports:
            - '27018:27017'

    mongo2:
        image: mongo:latest
        ports:
            - '27019:27017'

The host mongo listens on 27017. The also host maps ports 27018 and 27019 to the container mongo instances, both listening on 27017 inside the container.

主机 mongo 侦听 27017。主机也将端口 27018 和 27019 映射到容器 mongo 实例,两者都侦听容器内的 27017。

Each containers has it's own network name space and has no concept of what is running in another container or on the host.

每个容器都有自己的网络命名空间,并且不知道在另一个容器或主机上运行的是什么。

Networks

网络

The webapp needs to be able to connect to the mongo containers internal port. You can do this over a container networkwhich allows connections between the container and also name resolution for each service

webapp 需要能够连接到 mongo 容器的内部端口。您可以通过容器网络执行此操作,该网络允许容器之间的连接以及每个服务的名称解析

services:
    webapp:
        image: myimage
        ports:
          - '3000:3000'
        networks:
          - myapp
        depends_on:
          - mongo

    mongo:
        image: 'mongo:latest'
        ports:
          - '27018:27017'
        networks:
          - myapp

networks:
    myapp:
        driver: bridge

From your app the url mongo://mongo:27017will then work.

从您的应用程序中,该 urlmongo://mongo:27017将起作用。

From your host need to use the mapped port and an address on the host, which is normally localhost: mongo://localhost:27018

从您的主机需要使用映射端口和主机上的地址,通常是localhostmongo://localhost:27018