Repository navigation
Description
I have a base image that I use for PHP (PHP-CLI & PHP-FPM)
Would be cool if you could tell fig to inherit a previous image as a base.
The child image would then have all the attributes of the parent, and then you would be able to override the parents attributes / add attributes.
It is simply generating a different Docker Run command based on the parents command.
No extra building / pulling etc would be needed.
Current Method
ServiceA:
build: /docker/php
volumes:
- /docker/websites:/docker/websites
links:
- redis
command: php service.php
((docker run -v /docker/websites:/docker/websites --links redis:redis ae3454 php service.php))
ServiceB:
build: /docker/php
volumes:
- /docker/websites:/docker/websites
links:
- redis
command: php5-fpm -F
((docker run -v /docker/websites:/docker/websites --links redis:redis ae3454 php5-fpm -F))
Proposed Method
ServiceA:
build: /docker/php
volumes:
- /docker/websites:/docker/websites
links:
- redis
command: php service.php
((docker run -v /docker/websites:/docker/websites --links redis:redis ae3454 php service.php))
ServiceB:
inherits: ServiceA
command: php5-fpm -F
((docker run -v /docker/websites:/docker/websites --links redis:redis ae3454 php5-fpm -F))
Behind the scenes
ServiceA = {'container-id' => 'ae3454',
volumes'=>['/docker/websites:/docker/websites'],
links=>['redis:redis'], 'command'=>'php service.php'}
ServiceB = {'command'=>'php5-fpm -F'}
ServiceB = Merge ServiceB ServiceA (B overrides A)
print Service B:
{'container-id' => 'ae3454',
volumes'=>['/docker/websites:/docker/websites'],
links=>['redis:redis'], 'command'=>'php5-fpm -F'}
*Generate Docker Run Commands*
docker run -v /docker/websites:/docker/websites --links redis:redis ae3454 php service.php
docker run -v /docker/websites:/docker/websites --links redis:redis ae3454 php5-fpm -F
This will help reduce code duplication and make it easy to modify attributes just in the parent image.
Say my PHP images now required another service (MYSQL), I would simply need to add the mysql link to the first image.
At present, I would need to duplicate this change to all the other images which could lead to broken images if one is missed.
What would need deciding is what attributes get inherited, what attributes overwrite the parents and what attributed append to the parents.
Volumes - Append?
Environment variables - Append?
Ports - Don't inherit?
Activity
thaJeztah commentedon Nov 5, 2014
Inspired by "inherits"; perhaps "extends" is another alternative.
aanand commentedon Nov 5, 2014
I strongly suspect this should be a Docker-level concern, not Fig's realm. See the discussion here: moby/moby#735
thaJeztah commentedon Nov 5, 2014
hm, yes, if multiple images should be built using
FROM <image-for-other-service>However, if I understand correctly (I may be on the wrong foot here), the idea here is to re-use the image that was built for a service in another service, i.e.
Basically, this should work already if the correct image-name is referred to (but haven't tested it), but may fail if the services are started in the incorrect order.
The inherit part is not really part of the original proposal/feature request, and (again, if I understand correctly), is meant to copy the settings of another service (as specified in fig.yml), and override only those settings that are different. This request should probably be move to another issue (imo)
[-]Able to use just built images as further image bases?[/-][+]Image inheritance[/+]matthuisman commentedon Nov 5, 2014
Cleaned up my comments.
All summed up now in my first post.
Renamed issue.
aanand commentedon Nov 5, 2014
OK. I can see the value in something like an
image_fromkey, which inherits just the image.However, I don't think inheriting other configuration is a good idea - shared portable configuration should instead be moved into the Dockerfile. As for non-portable configuration like volumes and ports:
volumes_frommatthuisman commentedon Nov 6, 2014
Tested working! :)
I will attempt to create a patch / pull request.
BTW - Love the build system for fig - using a docker image to build - genius!
I've always wanted to package my own Python apps into an executable, will be copying this method.
Here is the output using my code changes
fig.yml extract
(phpfrpm inherits volumes_from and links from php)
fig build
(technically we wouldn't need to run the 2nd build command on this occasion as it ends up with same container - but if there were build differences it would be different)
fig up -d
fig ps
7 remaining items
matthuisman commentedon Nov 7, 2014
Also, having it built-in to fig gives some useful feedback if you typed something wrong
"cant find parent"
What happens if you do the YAML wrong?
Breaks the whole config?
dnephin commentedon Nov 7, 2014
I suspect errors in yaml would break the config, yes. I've found the PyYaml error messages to be quite helpful in general.
Let's see how @aanand and @bfirsh feel about it
matthuisman commentedon Nov 7, 2014
I just think if there's a way to encourage more services more easily, it will stop people being tempted to do:
phpService:
command: service phpfpm start && php myscript.php
k4nar commentedon Nov 8, 2014
I agree that if it's possible using pure YAML it should not be an option added in fig. But we should add an example in the doc explaining how to do that using YAML.
zeevallin commentedon Feb 12, 2015
The case where I'd like to use this is when I have a worker and web app running from the same code base. Currently using yaml inheritance I will end up with two images being built which is a waste of space and time.
What if there was a statement to
usethe image from another service?matthuisman commentedon Feb 13, 2015
You need 2x docker "instances" anyway.
You can't have 1x docker image doing 2x different commands unless you have some code in the image to start both commands.
The 2nd image will be the same, so will just use the cache of the first.
So, no extra space used.
All FIG does is read config files and then run the Docker commands for you in the background.
Your generated commands would be something like
docker run ae368852 bundle exec unicorn_rails
docker run ae368852 bundle exec sidekiq
ae368852 = is just a random container ID
So, using the same container, but just creating 2x "instances" of it.
Exactly what you want.
zeevallin commentedon Feb 13, 2015
@matthuisman You can in fact start start two different containers from on the same image, so I'm not quite sure what you mean.
As well as basing a fig service image on a local Dockerfile or an image from a registry, what I'm suggesting is allowing the direct use of another local service image, cut the overhead.
Instead of this:
I want this:
dnephin commentedon Sep 1, 2015
I believe this was address with
extends?zeevallin commentedon Sep 1, 2015
@dnephin Doesn't it still end up different images only difference being the last commit? I might be wrong.
dnephin commentedon Sep 1, 2015
The image tags are different, but the layers should all be the same.
If this is just about re-using the same image, I think that's covered by #963