Answers: <h1>podman pod</h1>
  <p>Docker compose is an old technology that allows packaging multiple containers into a unit. This has been largely replaced by <code>podman pod</code>, which provides everything that <code>docker compose</code> provides, except it also provides</p>
  <ul>
  <li>SystemD unit generation -- make it run as service in systemd.</li>
  <li>Kubernetes generation -- make it run on a cluster.</li>
  <li>Rootless operation</li>
  </ul>
  <p>It's also easier to use. To do what you're looking for you can do something like this. Note <code>podman pod</code> is just a shell command, no YAML needed (though there is a YAML method too).</p>
  <pre class="lang-sh prettyprint-override"><code>#!/bin/sh
  
  podman pod rm -f mongoapp;
  
  podman pod create \
    --publish 27017 \
    mongoapp;
  
  podman run   \
    --pod mongoapp \
    --detach       \
    mongo:latest;
  </code></pre>
  <p>Now you have a pod created running mongo. Anything in the pod can reach it. This is where you'd run your &quot;backend&quot;</p>
  <pre class="lang-sh prettyprint-override"><code>podman run -i    \
    --pod mongoapp \
    alpine:latest /bin/sh &lt;&lt;EOF
       apk update;
       apk add mongodb-tools;
       mongodump;
       exit;
  EOF
  </code></pre>
  <h1>Exposing Ports</h1>
  <p>Generally when you put Mongo in a container, you don't want anything to access it. You want the backend in a container communicating to it privately, and you want to provide access to the backend. To do this, we change <code>podman pod create</code> to publish the port,</p>
  <pre class="lang-sh prettyprint-override"><code>#!/bin/sh
  
  podman pod rm -f mongoapp;
  
  podman pod create \
    --publish 27017 \
    mongoapp;
  
  podman run   \
    --pod mongoapp \
    --detach       \
    mongo:latest;
  </code></pre>
  <p>And now anything outside of it can access it as if it was running on the host (if your host has <code>mongodump</code> it should just work). You can even access it from containers outside the pod (since the port is published) by using <code>host.containers.internal</code>,</p>
  <pre class="lang-sh prettyprint-override"><code>podman run -i    \
    --pod mongoapp \
    alpine:latest /bin/sh &lt;&lt;EOF
       apk update;
       apk add mongodb-tools;
       mongodump --host host.containers.internal;
       exit;
  EOF
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-05-02T03:59:47.287Z
Answers: Thank you for your answer, but I wanted to know how we could do it using docker compose only
Answers: Equation_Charmer
Answers: CC BY-SA 4.0
Answers: 2024-05-02T04:23:11.657Z
Answers: @Equation_Charmer at the least, create a mre https://en.wikipedia.org/wiki/Minimal_reproducible_example
Answers: Evan Carroll
Answers: CC BY-SA 4.0
Answers: 2024-05-01T22:42:25.827Z
Answers: 2024-05-01T22:42:25.827Z
Answers: Evan Carroll
Body: <p>I have mongodb running in the host machine (Ubuntu 23.04) in the default port. I have set the <code>bind_ip</code> to be 0.0.0.0. In my <code>docker-compose</code> file, I have added</p>
  <pre><code>extra_hosts:
        - &quot;host.docker.internal:host-gateway&quot;
  </code></pre>
  <p>And, I have my mongodb URI as <code>mongodb://host.docker.internal/mydbname</code>. However, I get <code>Server selection timed out after 30000 ms</code></p>
  <p>I tried to change <code>network_type</code> as host, after that it started working but why is this <code>host.docker.internal</code> working? Kindly please help.</p>
  <p>Edit:
  This is the docker-compose.yml</p>
  <pre><code>version: &quot;3.3&quot;
  
  services:
    web:
      image: test
      container_name: test
      restart: always
      build:
          context: .
      extra_hosts:
        - &quot;host.docker.internal:host-gateway&quot;
      ports:
         - &quot;9345:5000&quot;
      command: node app.js
      env_file:
        - backend/.env.production
        - frontend/.env.production
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2024-04-29T20:15:09.157Z
Comments: Please share the docker-compose.yml, or at least the block containing mongodb
Comments: mikequentel
Comments: CC BY-SA 4.0
Comments: 2024-04-30T07:42:02.030Z
Comments: Have added the docker-compose.yml file, however there is no MongoDB block because Im trying to connect to locally hosted MongoDB.
Comments: Equation_Charmer
Comments: CC BY-SA 4.0
Comments: 2024-05-01T20:42:23.520Z
Comments: Would you be interested in seeing a solution that doesn't use docker compose?
Comments: Evan Carroll
Tags: <docker><dockerfile><ubuntu><mongodb>
Title: Can't connect to local mongoDB from Docker Container
UserDisplayName: Equation_Charmer
ThreadText: # Can't connect to local mongoDB from Docker Container
  
  (Asked by: username_0 on 2024-04-29)
  
  I have mongodb running in the host machine (Ubuntu 23.04) in the default port. I have set the `bind_ip` to be 0.0.0.0. In my `docker-compose` file, I have added
  
  ```
  extra_hosts:
        - "host.docker.internal:host-gateway"
  
  ```
  
  And, I have my mongodb URI as `mongodb://host.docker.internal/mydbname`. However, I get `Server selection timed out after 30000 ms`
  
  I tried to change `network_type` as host, after that it started working but why is this `host.docker.internal` working? Kindly please help.
  
  Edit:
  This is the docker-compose.yml
  
  ```
  version: "3.3"
  
  services:
    web:
      image: test
      container_name: test
      restart: always
      build:
          context: .
      extra_hosts:
        - "host.docker.internal:host-gateway"
      ports:
         - "9345:5000"
      command: node app.js
      env_file:
        - backend/.env.production
        - frontend/.env.production
  
  ```
  
  **Tags:** docker, dockerfile, ubuntu, mongodb
  
  > **Comments:**
  > **username_1**: Please share the docker-compose.yml, or at least the block containing mongodb  
  > **username_0**: Have added the docker-compose.yml file, however there is no MongoDB block because Im trying to connect to locally hosted MongoDB.  
  > **username_2**: Would you be interested in seeing a solution that doesn't use docker compose?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2024-05-01. Score: 2
  
  # podman pod
  
  Docker compose is an old technology that allows packaging multiple containers into a unit. This has been largely replaced by `podman pod`, which provides everything that `docker compose` provides, except it also provides
  
  * SystemD unit generation -- make it run as service in systemd.
  * Kubernetes generation -- make it run on a cluster.
  * Rootless operation
  
  It's also easier to use. To do what you're looking for you can do something like this. Note `podman pod` is just a shell command, no YAML needed (though there is a YAML method too).
  
  ```sh
  #!/bin/sh
  
  podman pod rm -f mongoapp;
  
  podman pod create \
    --publish 27017 \
    mongoapp;
  
  podman run   \
    --pod mongoapp \
    --detach       \
    mongo:latest;
  
  ```
  
  Now you have a pod created running mongo. Anything in the pod can reach it. This is where you'd run your "backend"
  
  ```sh
  podman run -i    \
    --pod mongoapp \
    alpine:latest /bin/sh <<EOF
       apk update;
       apk add mongodb-tools;
       mongodump;
       exit;
  EOF
  
  ```
  
  # Exposing Ports
  
  Generally when you put Mongo in a container, you don't want anything to access it. You want the backend in a container communicating to it privately, and you want to provide access to the backend. To do this, we change `podman pod create` to publish the port,
  
  ```sh
  #!/bin/sh
  
  podman pod rm -f mongoapp;
  
  podman pod create \
    --publish 27017 \
    mongoapp;
  
  podman run   \
    --pod mongoapp \
    --detach       \
    mongo:latest;
  
  ```
  
  And now anything outside of it can access it as if it was running on the host (if your host has `mongodump` it should just work). You can even access it from containers outside the pod (since the port is published) by using `host.containers.internal`,
  
  ```sh
  podman run -i    \
    --pod mongoapp \
    alpine:latest /bin/sh <<EOF
       apk update;
       apk add mongodb-tools;
       mongodump --host host.containers.internal;
       exit;
  EOF
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for your answer, but I wanted to know how we could do it using docker compose only  
  > **username_2**: @username_0 at the least, create a mre https://en.wikipedia.org/wiki/Minimal_reproducible_example  
  
  ---
  
  