title: docker run mongo image on a different port
body: <p>The short question is can I run mongo from mongo:latest image on a different port than 27017 (for example on 27018?)</p>
  
  <p>If yes, how can I do this inside a docker-compose.yml file in order ro be able to type the following command:</p>
  
  <pre><code>docker-compose run
  </code></pre>
  
  <p>The longer story:</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>version: '2'
  services:
      webapp:
          image: myimage
          ports:
              - 3000:3000
  
      mongo:
          image: mongo:latest
          ports:
              - 27018:27018
  </code></pre>
  
  <p>And inside my new app, I changed the mongo url to:</p>
  
  <pre><code>monog_url = 'mongodb://mongo:27018'
  client = MongoClient(monog_url, 27018)
  </code></pre>
  
  <p>Well, the same if I say:</p>
  
  <pre><code>monog_url = 'mongodb://mongo:27018'
  client = MongoClient(monog_url)
  </code></pre>
  
  <p>But when I run docker-compose run, it still does not work, and I get the following errors:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>pymongo.errors.ServerSelectionTimeoutError:
  mongo:27018: [Errno -2] Name or service not known
  </code></pre>