Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Feature: compose 'memory' #22

Merged
merged 4 commits into from
Feb 25, 2016
Merged

Conversation

mauricioklein
Copy link
Owner

This PR provides the ability to compose recognize containers started by it previously and reload them in a different context.

This is done analyzing the containers name, searching for those who has the same pattern of current compose.

This feature is optional and must be enabled explicitly on compose load method (instructions provided on README file).

…ry'.

This change modifies the container name creation to be in conformity with original Docker Compose.

So, container names are now generated following the pattern below:

       [compose directory]_[container identifier]_[sequential ID], where:

- [compose directory]: the directory where compose is being executed;
- [container identifier]: the container given name (or label, if name isn't provided);
- [sequential ID]: the next ID available for container naming (defaults to '1' if no container's running);

It was also added a new method to fetch containers by its given name.

So:
   # Query containers by its full name
   compose.get_containers_by(name: 'myawessomedir_servicefoo_1')

   # Query containers by its given name, ignoring both prefix and suffix
   compose.get_containers_by_given_name('servicefoo')
Now, the containers are started with a name that inform its origin (it means, the compose file that started it).
So, a new compose instance is now able to recognize such containers and load them into its context.

Instructions to load the previous started containers can be found on README file.
@mitcheaton1
Copy link

Would love to see this merged.

#
def get_containers_by_given_name(given_name)
@containers.select { |label, container|
container.attributes[:name].match(/\w*_#{given_name}_\d+/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say it would be safer if @dir_name from ComposeUtils could be used there instead of \w+, no? While the chance is small it is still there that another container could coincidentally use the same naming convention.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right!
I've fixed it to filter containers based on its directory name as well.
Thanks for the suggestion!

@jeroenj
Copy link
Contributor

jeroenj commented Feb 25, 2016

Looks good! 👍

Fyi: docker-compose is using labels instead of names to identify containers now (since v1.3.0 I think): docker/compose#1356.

@jeroenj
Copy link
Contributor

jeroenj commented Feb 25, 2016

A bit hackish, but in my project I'm setting a bunch of custom labels which I use to identify containers: https://github.com/playpasshq/docker-compose-api/commit/7ca4a2ebad95a2224332dee9fd6b4f01f4f335f7. (setting the labels happens on a higher level right now)

@mauricioklein
Copy link
Owner Author

@jeroenj

Indeed compose is now using label instead names to recognized containers started previously.
Docker-compose-api does the same, except if you inform a container name.
If no name is provided in compose file, label is used to create a new container name.
This was the best approach I found, given the current state of gem.
Of course, in the future we can do a major refactoring and turn this gem fully conform with original Compose, but I think that, for a very first version, it fulfills its purpose.

Let me know if you guys share the same thoughts.
Thanks!

@jeroenj
Copy link
Contributor

jeroenj commented Feb 25, 2016

@mauricioklein I agree. As it is now (including this feature) it does what I expect and does everything I need for the project I am using it for.

At some point it might be nice that it does exaclty the same as compose itself, but I think it would involve quite some work. You already did a good job on the gem right now! 👍

mauricioklein added a commit that referenced this pull request Feb 25, 2016
@mauricioklein mauricioklein merged commit 8796df5 into master Feb 25, 2016
@mauricioklein mauricioklein deleted the features/compose-container-memory branch February 26, 2016 13:03
mauricioklein added a commit that referenced this pull request Jun 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants