title: docker compose connecting mongodb container with node js container
body: <p>i have a sharelatex container running. This docker compose file contains a mongo and redis container.</p>
  
  <p>Here is the <strong>sharelatex docker compose</strong>:</p>
  
  <pre><code>version: '2'
  services:
      sharelatex:
          restart: always
          image: rigon/sharelatex-full
          #image: sharelatex/sharelatex
          container_name: sharelatex
          networks:
              - test-network
          depends_on:
              - mongo
              - redis
          privileged: true
          links:
              - mongo
              - redis
          volumes:
              - ./sharelatex_data:/var/lib/sharelatex
              - /var/run/docker.sock:/var/run/docker.sock
          environment:
              SHARELATEX_MONGO_URL: mongodb://mongo/sharelatex
              SHARELATEX_REDIS_HOST: redis
              SHARELATEX_APP_NAME: ShareLaTeX
              SHARELATEX_SITE_URL: https://latex.tkwant.de
  
     mongo:
          restart: always
          image: mongo
          container_name: mongo
  
          expose:
             - 27017
          volumes:
              - ./mongo_data:/data/db
          networks:
              - test-network
      redis:
          restart: always
          image: redis
          container_name: redis
  
          networks:
              - test-network
          expose:
              - 6379
          volumes:
              - ./redis_data:/data
  
  networks:
      test-network:
        external: true
  </code></pre>
  
  <p>I want to create a node application which needs mongodb, too.
  How can i connect these two container?
  I read about network and tried out docker network but without success.</p>
  
  <p>This is my <strong>node docker compose</strong>:</p>
  
  <pre><code>version: '3.5'
  services:
    app:
      container_name: app
      restart: always
      build: .
      ports:
        - '3001:3000'
      networks:
        - test-network
  networks:
      test-network:
        driver: external
  </code></pre>
  
  <p>and here my <strong>index.js</strong>:</p>
  
  <pre><code>// Connect to MongoDB
  mongoose
    .connect(
      'mongodb://mongo:27017/test2',
      { useNewUrlParser: true }
    )
    .then(() =&gt; console.log('MongoDB Connected'))
    .catch(err =&gt; console.log("errorErrorError"));
  </code></pre>
  
  <p>I am open for all answers... 
  running mongo in an own container or create a docker network. But I dont know what is the best or the easiest.</p>
  
  <p>Update 1:</p>
  
  <p>First Problem: Sharelatex does not work anymore --> sharelatex is now in another network. My nginx reverse proxy does not find sharelatex container anymore. Second Problem is: when i want to start node docker i get this error (dont know why but it want to create a new network): Creating network "dockernodemongo_test-network" with driver "external" ERROR: plugin "external" not found </p>