Skip to content

Image inheritance #610

Closed
Closed

Description

@matthuisman

#625

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

  1. thaJeztah commented on Nov 5, 2014

    @thaJeztah
    Member

    Inspired by "inherits"; perhaps "extends" is another alternative.

  2. aanand commented on Nov 5, 2014

    @aanand

    I strongly suspect this should be a Docker-level concern, not Fig's realm. See the discussion here: moby/moby#735

  3. thaJeztah commented on Nov 5, 2014

    @thaJeztah
    Member

    I strongly suspect this should be a Docker-level concern

    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.

    serviceA:
      build: ./service
    
    serviceB:
      image: <image that was built for service A>
    
    serviceC:
      image: <image that was built for service A>
    
    

    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)

  4. changed the title [-]Able to use just built images as further image bases?[/-] [+]Image inheritance[/+] on Nov 5, 2014
  5. matthuisman commented on Nov 5, 2014

    @matthuisman
    Author

    Cleaned up my comments.
    All summed up now in my first post.
    Renamed issue.

  6. aanand commented on Nov 5, 2014

    @aanand

    OK. I can see the value in something like an image_from key, 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:

    • you can already share volumes with volumes_from
    • you never want to share ports config, because they'll clash
  7. matthuisman commented on Nov 6, 2014

    @matthuisman
    Author

    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)

    php:
      build: /docker/BUILDS/php
      volumes_from:
        - volumes
      links:
        - redis
        - mysql
    
    phpfpm:
      inherits: php
      command: php5-fpm -F
    

    fig build

    Building php...
    .......
    Step 9 : CMD php --help
     ---> Using cache
     ---> e8c443fb0a8d
    Successfully built e8c443fb0a8d
    Building phpfpm...
    .......
    Step 9 : CMD php --help
     ---> Using cache
     ---> e8c443fb0a8d
    Successfully built e8c443fb0a8d
    

    (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

    Creating docker_volumes_1...
    Creating docker_mysql_1...
    Creating docker_redis_1...
    Creating docker_php_1...
    Creating docker_phpfpm_1...
    Creating docker_nginx_1...
    

    fig ps

    docker_php_1       php --help                       Exit 0
    docker_phpfpm_1    php5-fpm -F                      Up
    
  8. 7 remaining items

  9. matthuisman commented on Nov 7, 2014

    @matthuisman
    Author

    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?

  10. dnephin commented on Nov 7, 2014

    @dnephin

    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

  11. matthuisman commented on Nov 7, 2014

    @matthuisman
    Author

    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

  12. k4nar commented on Nov 8, 2014

    @k4nar

    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.

  13. zeevallin commented on Feb 12, 2015

    @zeevallin

    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 use the image from another service?

    web: &app
      build: .
      command: bundle exec unicorn_rails
      environment:
        RAILS_ENV: development
        REDIS_URL: redis://redis/0
        DATABASE_URL: postgres://postgres/dev
    
    worker:
      <<: *app
      use: web
      command: bundle exec sidekiq
  14. matthuisman commented on Feb 13, 2015

    @matthuisman
    Author

    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.

  15. zeevallin commented on Feb 13, 2015

    @zeevallin

    @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:

    CONTAINER ID        IMAGE                       NAMES
    8acd16f92923        project_sidekiq:latest      project_sidekiq_1
    92c327b5d762        project_web:latest          project_app_1
    

    I want this:

    CONTAINER ID        IMAGE                       NAMES
    8acd16f92923        project_web:latest          project_sidekiq_1
    92c327b5d762        project_web:latest          project_app_1
    
  16. dnephin commented on Sep 1, 2015

    @dnephin

    I believe this was address with extends ?

  17. zeevallin commented on Sep 1, 2015

    @zeevallin

    @dnephin Doesn't it still end up different images only difference being the last commit? I might be wrong.

  18. dnephin commented on Sep 1, 2015

    @dnephin

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions