Updating docker images with small changes using commits

前端 未结 1 972
时光说笑
时光说笑 2021-02-20 17:44

I want to leverage caching/layering of docker images to save bandwidth, disk space, and time spent.

Let say:

  • I\'ve a web-app docker image installed and dep
相关标签:
1条回答
  • 2021-02-20 18:22

    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.

    0 讨论(0)
提交回复
热议问题