title: Connecting to docker mongo db image
body: <p>I'm trying to use <a href="https://hub.docker.com/_/mongo" rel="nofollow noreferrer">these instructions</a> to try to run the mongodb docker image in a container.</p>
  
  <p>I'm able to run the image:</p>
  
  <pre><code>(virtual) anonymous@anonymous:~/PycharmProjects/PostDekko/climbticks-flaskmongoangul$ sudo docker container ls
  CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                    NAMES
  431a9d693f48        mongo:bionic                         "docker-entrypoint.s…"   24 minutes ago      Up 24 minutes       27017/tcp                climbticks-mongodb
  f12d227dcdd8        climbticks-flaskmongoangul_web_dev   "python3.8 index.py"     3 hours ago         Up 3 hours          0.0.0.0:4000-&gt;4000/tcp   climbticks-flaskmongoangul_web_dev_1
  </code></pre>
  
  <p>My project is in the 2nd container, and the mongodb image is <code>container_id 431a9d693f48</code>. I can also access the logs, so I know it runs.</p>
  
  <p>From their instructions:</p>
  
  <blockquote>
    <p>The MongoDB server in the image listens on the standard MongoDB port,
    27017, so connecting via Docker networks will be the same as
    connecting to a remote mongod. The following example starts another
    MongoDB container instance and runs the mongo command line client
    against the original MongoDB container from the example above,
    allowing you to execute MongoDB statements against your database
    instance:</p>
  </blockquote>
  
  <pre><code>&gt; $ docker run -it --network some-network --rm mongo mongo --host
  &gt; some-mongo test
  </code></pre>
  
  <blockquote>
    <p>... where some-mongo is the name of your original mongo container.</p>
  </blockquote>
  
  <p>However.... I don't understand what my "network" is supposed to be. I think that the network argument, from docker's perspective, means a network id. If I run:</p>
  
  <pre><code>(virtual) anonymous@anonymous:~/PycharmProjects/PostDekko/climbticks-flaskmongoangul$ sudo docker network ls
  NETWORK ID          NAME                DRIVER              SCOPE
  7fbed634dbdf        bridge              bridge              local
  0843e5ae6173        host                host                local
  6b3c8eb4b216        none                null                local
  cc3d76f1f109        web_dev             bridge              local
  </code></pre>
  
  <p>I get 4 network ids. web_dev is my project's (webserver) container. If I try any of the 4, I get:</p>
  
  <pre><code>(virtual) anonymous@anonymous:~/PycharmProjects/PostDekko/climbticks-flaskmongoangul$ sudo docker run -it --network cc3d76f1f109 --rm mongo mongo --host climbticks-mongodb test
  MongoDB shell version v4.2.7
  connecting to: mongodb://climbticks-mongodb:27017/test?compressors=disabled&amp;gssapiServiceName=mongodb
  2020-05-31T01:55:08.771+0000 E  QUERY    [js] Error: couldn't connect to server climbticks-mongodb:27017, connection attempt failed: HostNotFound: Could not find address for climbticks-mongodb:27017: SocketException: Host not found (authoritative) :
  connect@src/mongo/shell/mongo.js:341:17
  @(connect):2:6
  2020-05-31T01:55:08.779+0000 F  -        [main] exception: connect failed
  2020-05-31T01:55:08.779+0000 E  -        [main] exiting with code 1
  </code></pre>
  
  <p>I also tried various expressions for localhost (0.0.0.0, localhost, etc.) even though that didn't make sense to me.</p>
  
  <p>So.... how do I find what the network is supposed to be?</p>
  
  <p>EDIT: although if I connect to my DB in the shell:</p>
  
  <pre><code>sudo docker exec -it climbticksmongodb bash
  </code></pre>
  
  <p>then I can talk to it. So the DB is live. I really just need to figure the network issue, it seems.</p>