Syncing docker images

2019-07-10 10:26发布

问题:

I have 2 machines(separate hosts) running docker and I am using the same image on both the machines. How do I keep both the images in sync. For eg. suppose I make changes to the image in one of the hosts and want the changes to reflect in the other host as well. I can commit the image and copy the image over to the other host. Is there any other efficient way of doing this??

回答1:

Some ways I can think of:

1. with a Docker registry

the workflow here is:

  • HOST A: docker commit, docker push
  • HOST B: docker pull

2. by saving the image to a .tar file

the workflow here is:

  • HOST A: docker save
  • HOST B: docker load

3. with a Dockerfile and by building the image again

the workflow here is:

  • provide a Dockerfile together with your code / files required
  • everytime your code has changed and you want to make a release, use docker build to create a new image.
  • from the hosts that you want to take the update, you will have to get the updated source code (maybe by using a version control software like Git), and then docker build the image

4. CI/CD pipeline

you can see a video here: docker.com/use-cases/cicd


Keep in mind that containers are considered to be ephemeral. This means that updating an image inside another host will then require:

  1. to stop and remove any old container (running with the outdated image)
  2. to run a new one (with the updated image)

I quote from: Best practices for writing Dockerfiles

General guidelines and recommendations

Containers should be ephemeral

The container produced by the image your Dockerfile defines should be as ephemeral as possible. By “ephemeral,” we mean that it can be stopped and destroyed and a new one built and put in place with an absolute minimum of set-up and configuration.



回答2:

You can perform docker push to upload you image to docker registry and perform a docker pull to get the latest image from another host.

For more information please look at this



标签: docker