5

In docker we have -t flag to keep containers from exiting. How can achieve the same thing in nomad?

I want to debug if I can ping one service from another, so I just want a container with curl. However, if I try to deploy the ubuntu image specifying it like below it exits and keeps restarting. What can I do so it just keeps running?

task "testubuntu" {

  driver = "docker"

  config {
      image = "ubuntu:latest"
  }

  resources {
      cpu = 500
      memory = 256
      network {
          mbits = 10
      }
  }
}
Alan Sereb
  • 2,358
  • 2
  • 17
  • 31

2 Answers2

3

Another solution would be to set a "dummy" entry point tail -f /dev/null

task "testubuntu" {

  driver = "docker"

  config {
      image = "ubuntu:latest"
      entrypoint = [
          "tail", "-f", "/dev/null",
      ]
  }

  resources {
      cpu = 500
      memory = 256
  }
}

It is particularly useful, when you have a task that errors at the container startup but there is not much useful information in the logs. This "dummy" entry point will keep container alive allowing you to get inside container and execute a real startup command with attached debugger for example.

Apart from tail -f /dev/null, you can also simply use yes as an entry point. However, it will pollute stdout and affect your logging solution if it is setup.

Ilya Kisil
  • 2,490
  • 2
  • 17
  • 31
0

Add container = true in the config stanza

task "testubuntu" {

  driver = "docker"

  config {
      image = "ubuntu:latest"
      container = true
  }

  resources {
      cpu = 500
      memory = 256
      network {
          mbits = 10
      }
  }
}
Alan Sereb
  • 2,358
  • 2
  • 17
  • 31
  • Interesting. Do you have a link to this container config parameter as I couldn't find it in [the official docs](https://www.nomadproject.io/docs/drivers/docker)?) – Ilya Kisil Jan 15 '21 at 02:31
  • @IlyaKisil Its there: `container` - Defaults to true. This option can be used to disable Nomad from removing a container when the task exits. Under a name conflict, Nomad may still remove the dead container. Search for: `container -` – Alan Sereb Jan 15 '21 at 19:14
  • Ok, I see it now) but that is a configuration for nomad client/server as part of `gc` stanza and not the job file. However, it still passes `nomad validate ...`. Didn't know that could be used in a job file or maybe it is just undocumented feature) Nomad works in mysterious ways sometimes) But if job is set to restart immediately on fail, you might end up with a lot of dangling containers, no? – Ilya Kisil Jan 16 '21 at 00:17
  • Again, I needed that just to test consul connect connectivity in development. As soon as I was done I removed the group and the task with this dangling container. In production, it might be better to find other workarounds, but it worked for me. – Alan Sereb Jan 16 '21 at 17:51
  • I'll have to try some more with this but my first attempted didn't work. However the answer from @Ilya Kisil did. – Kuberchaun Mar 13 '21 at 05:54
  • 1
    hmm, the docs (https://www.nomadproject.io/docs/drivers/docker#container) say that container already defaults to true. I tried setting it to false (thinking @alan-sereb mistyped above) but the the task doesn't start. the tail answer above seems to work well though - but seems like a huge hack – jsharpe Apr 20 '22 at 20:49