<p>I have an applications Docker image which starts a mongodb instance on a random port. When I create a kubernetes Pod with application image; application gets successfully initialized and a mongodb instance gets up on a random port as <strong>localhost:port</strong> without any error.</p>

<p>However, when I create a Kubernetes Deployment; the same application initialization fails inside the container with error "mongodb can not be started as <strong>localhost:port</strong> can not be accessed".</p>

<p>If anyone can explain, why application initialization is failing with K8-Deployment but not with K-8Pod? And, How can I resolve this problem?</p>

<pre><code>apiVersion: apps/v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: my-app:v1
    ports:
    - containerPort: 8888 # Apps exposed port

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-dep
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:v1
        ports:
        - containerPort: 8888 # Apps exposed port
</code></pre>

<p>Thanks</p>

Jaraws
<p>Instead of listening on <code>localhost:port</code>, you should try configure MongoDB to listening on <code>0.0.0.0:port</code>. This has helped me when having a similar issue with another app.</p>

<h2>Configure MongoDB to listen to all interfaces</h2>

<p>Your <code>mongod.conf</code></p>

<pre><code># /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1
</code></pre>

<p>change to this</p>

<pre><code># /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
# bind_ip = 127.0.0.1
</code></pre>
