docker run mongo image on a different port docker run mongo image on a different port docker docker

docker run mongo image on a different port


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'


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.

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 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:          - myappnetworks:    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


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.

As Matt mentioned, 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.

Below is a working docker-compose file:

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:

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

Docker run an image on a different port

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:

version: '2'services:web:    image: myimagemongo:    image: mongo:latest    command: mongod --port 27018

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

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