Updating docker images with small changes using commits

守給你的承諾、 提交于 2019-12-04 02:57:51

Here's how to update an existing image with docker commit.

  1. launch a container with the image you want to modify:

      docker run -t -i IMAGE /bin/bash
    

    Note that you'll probably want to access some host files/directory to import changes in the container:

      docker run -t -i -v /host/location:/mnt/share IMAGE /bin/bash
    

    Then quit with Ctrl-D or exit.

    If you want to automate this in a script, you'll need to get the container id for the next step. And you'll want to issue directly commands instead of calling an interactive session of bash:

      container_id=$(docker run -d -v /host/location:/mnt/share IMAGE /bin/bash -c "
            ## any bash code
            rsync -av --delete --exclude .git /mnt/share /my/app/
            cd /my/app
            ./autogen.sh
        ")
    
  2. Commit your modified container filesystem as a new image:

       docker commit CONTAINER_ID IMAGE_NAME
    

    Note: you could want to use the same IMAGE_NAME than the one you've first launched the container with. This will effectively update your image.

Additional concerns:

  • Any modification carried on a previous image should try to minimize the new layer created upon last image. Rules probably depends if you are using BTRFS (block level modifications will actually be in the 'layer'), or AUFS (file level modifications). The best would be to avoid replacing whole source files with the same files (avoid cp -a, git checkout-index, favor rsync or git checkout).

  • You'll need to install some tools on your VM so that you can make your updates (probably git, rsync ...). But don't forget you could also provide scripts (or even complete tools), thanks to the mounted host volume.

  • The created image is not orthodox and does not come from a Dockerfile. You should probably rebuild a full new image quite regularly from an official Dockerfile. Or at least try to minimize layering by having all your images based directly on one official image.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!