# Containers in the same network namespace issue

**URL:** <https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032>\
**Category:** Nomad\
**Created:** [September 17, 2024, 3:00pm UTC](https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032 "2024-09-17T15:00:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenAgai](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/benagai/32/43028_2.png) [@BenAgai](https://discuss.hashicorp.com/u/BenAgai)\
**Post date:** [September 17, 2024, 3:00pm UTC](https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032/1 "2024-09-17T15:00:58Z")

</div>

Hi,  
I hope this is the correct place to ask the question about an issue I have with nomad, please let me know if it is not.

I wanted to ask if the following scenario is supported in nomad.  
I created a docker network on a VM, lets name the network “ben-network”.  
I created a nomad job that contains a task group with a constraint to be deployed on my VM.  
The task group contains two tasks where I want both tasks to use “ben-network” and share the same network namespace.

How can I do that with nomad?

According to the documentation I read, if I want to use a specific docker network, for example “ben-network”, I need to do this through the network configuration of the underlying task and not in the group’s configuration.  
When I do this, I can’t make the tasks in the group share net namespace, as in order to do that one task need to use network of “container\_name” which is not known in advance as nomad is the one determining the container name during deployment.

I would appreciate any advice on the matter,  
Thanks in advance!  
Ben Agai

---

<div class="post-metadata">

**Author:** ![BenAgai](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/benagai/32/43028_2.png) [@BenAgai](https://discuss.hashicorp.com/u/BenAgai)\
**Post date:** [September 19, 2024, 8:55am UTC](https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032/3 "2024-09-19T08:55:53Z")

</div>

@zesismark that is what I ask.  
I’m not sure if your expecting me to answer you or if your also joining my question.

---

<div class="post-metadata">

**Author:** ![Ranjandas](https://avatars.discourse-cdn.com/v4/letter/r/90ced4/32.png) [@Ranjandas](https://discuss.hashicorp.com/u/Ranjandas)\
**Post date:** [September 21, 2024, 11:41am UTC](https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032/4 "2024-09-21T11:41:52Z")

</div>

Hi @BenAgai,

Welcome to the HashiCorp Forums!

I got some ideas from an old mailing list [thread](https://groups.google.com/g/nomad-tool/c/P7Kd9q6LW7I) and tried the following approach, which is working for me.

The trick is to add a task with a lifecycle block (to prestart and of type sidecar) and then use this task’s name with the allocation ID to attach to its network namespace. I am not sure whether there is a better way or if there are any caveats. Please try this and let me know if works for you.

```auto
job "example" {

  group "cache" {
    network {
      port "db" {
        to = 6379
      }
    }
    task "init" {
      lifecycle {
        hook = "prestart"
        sidecar = true
      }
      driver = "docker"

      config {
        image = "gcr.io/google_containers/pause-arm64:3.1"
        network_mode = "hello" # this is my user-network

      }
    }

    task "redis" {
      driver = "docker"

      config {
        image = "redis:7"
        ports = ["db"]
        auth_soft_fail = true
        network_mode = "container:init-${NOMAD_ALLOC_ID}"
      }

      identity {
        env = true
        file = true
      }

      resources {
        cpu = 500
        memory = 256
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![BenAgai](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/benagai/32/43028_2.png) [@BenAgai](https://discuss.hashicorp.com/u/BenAgai)\
**Post date:** [September 27, 2024, 9:12am UTC](https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032/5 "2024-09-27T09:12:45Z")

</div>

Hi @Ranjandas ,  
I didn’t get a chance yet to test this, but once I’ll get there I will let you know.

Thanks!

---

<div class="post-metadata">

**Author:** ![BenAgai](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/benagai/32/43028_2.png) [@BenAgai](https://discuss.hashicorp.com/u/BenAgai)\
**Post date:** [October 16, 2024, 9:21am UTC](https://discuss.hashicorp.com/t/containers-in-the-same-network-namespace-issue/70032/6 "2024-10-16T09:21:34Z")

</div>

Hi @Ranjandas ,  
I checked your solution and it works 🙂  
Thanks!
