Feature request - support docker container names (I'll code it!)

I’m in a position where I need to name docker containers, which is supported as of Docker 0.6.5 and docker-py as of 0.2.3. But the Ansible docker module doesn’t support it, so I’m coding it.

There are some issues around names that need to be addressed, though. The biggest is how to handle count and name(s).

The simple scenario is that a single name and no count (or a count of 1) is provided. That’s pretty easy; we check to see if a container with that name has been created and is not a ghost. If not, we deploy one. There is a potential problem where a container with the name has been created and later killed. The docker-py library will fail to create one because the ghost is still using the name, so I think we remove the ghost container and deploy another one.

That same logic basic logic can be applied when a list of names are provided and count is 1 or not provided. We’d probably check to make sure a container with each of the provided names is deployed.

The question comes in when there are one or more names and count > 1. The current logic is based on making sure than count containers of the same image, tag, and command are deployed. Here’s what makes sense to me:

If len(names) == 1 and count > 1, we deploy containers with name plus a suffixed index.

If len(names) > 1 and count > 1, I would assume that the Ansible user wants to create count containers starting with each name.

I’d kinda like a pull request on this to be found acceptable, so I thought I’d ask for comments and ideas on how to handle names best. Please chime in!

Thanks!

Rob Martin

Rob,

The use case may be slightly different for the name, however recently in the ‘rax’ module (with the 1.4 release) we introduced a “count” attribute, and when paired with “group” (probably not useful for your case), we allow printf formatting for the name.

With that functionality you could do something like web%d.example.org and it would look for existing hosts (converting the printf formatting to regex for matching), and find out what numbers were in use, and increment the “host number” accordingly using the printf formatting. This allows someone to specify the number in any location within the ‘name’ string.

I imagine something similar could be applied here.

Just some thoughts!

If len(names) > 1 and count > 1, I would assume that the Ansible user wants to create count containers starting with each name.

This maybe feels like a little too much magic for me. What if you just didn’t support the “names” argument with a count > 1? Creating a single container with one name is still trivial:

  • name: Ensure 1 instance of ubuntu Docker container is running
    docker: image=ubuntu name=foo state=present count=1

And then the user could use with_sequence to get support for multiple containers with an index-suffixed name:

  • name: Ensure 5 instances of ubuntu Docker container are running
    docker: image=ubuntu name=bar_{{ item }} state=present
    with_sequence: start=1 end=5

Just a thought. What do you think?

Forgot to mention that you could support your own list of names using with_items:

  • name: Ensure 4 named instances of ubuntu Docker container are running
    docker: image=ubuntu name={{ item }} state=present
    with_items:
  • foo
  • bar
  • baz
  • quux