Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker run mongo image on a different port

The short question is can I run mongo from mongo:latest image on a different port than 27017 (for example on 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 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.

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:

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:

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:

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
like image 745
Mahshid Zeinaly Avatar asked Mar 09 '17 20:03

Mahshid Zeinaly


People also ask

How do I make Docker run on another port?

When Docker creates a container from the packages it downloads or from a dockerfile, it assigns the ports to it. To change the ports, you need to delete the existing container first, then re-create it.

How can I access a MongoDB container from another container?

If you need to access the MongoDB server from another application running locally, you will need to expose a port using the -p argument. Using this method, you will be able to connect to your MongoDB instance on mongodb://localhost:27017 . You can try it with Compass, MongoDB's GUI to visualize and analyze your data.

How use MongoDB Docker image?

Running MongoDB in a Docker Container You can pull the latest MongoDB image and run it in a Docker container. For production, the application can connect to a cloud-hosted database using the MongoDB Atlas or MongoDB Enterprise Server. This will pull the latest official image from Docker Hub.

Can Docker run multiple ports?

In your Dockerfile , you can use the verb EXPOSE to expose multiple ports.


2 Answers

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

services:
  mongo:
    image: 'mongo:latest'
    command: mongod --port 27018
    ports:
        - '27018:27018'
like image 111
Vince Bowdren Avatar answered Oct 23 '22 02:10

Vince Bowdren


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 case, 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 host also maps ports 27018 and 27019 to the container mongo instances, both listening on 27017 inside the container.

Each containers has its own network namespace 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 network which allows connections between the container and also name resolution for each service

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:27017 will then work.

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

like image 37
Matt Avatar answered Oct 23 '22 02:10

Matt