0

I am trying to update the image of a running docker container using the docker library for python.

To update the image, my script executes the following steps:

  • look for containers with the given image name
  • pull the image from the registry
  • create new containers with the configuration from the currently running containers
  • stop the old containers
  • start the new containers
  • remove the old containers

My problem is in step number 3. After creating new containers with the configuration from the old containers, the new containers are not having the exact same config. For Example, I testes my script with the traefik container. The resulting container has only port bindings for port 80 but the original has for ports 80 and 443.

This is the code to 'clone' the containers:

for c in containers:
    new_c = docker.APIClient().create_container(c.attrs['Config']['Image'],
                                                environment=c.attrs['Config']['Env'],
                                                host_config=c.attrs['HostConfig'],
                                                labels=c.attrs['Config']['Labels']
                                                )

Is there any way to 'clone' the container configuration or to update the container's image without using the configuration provided with docker run or inside docker-compose?

Hope someone can help me best regards

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
Luxaaa
  • 41
  • 1
  • 6
  • Can't you use the configuration in a volume? – Sergio Santiago Aug 15 '21 at 22:14
  • No because my updater should be universal for any container not for a specific one. – Luxaaa Aug 15 '21 at 22:15
  • What exactly is "the configuration"? – Klaus D. Aug 15 '21 at 22:27
  • @KlausD.All parameters which you can specify with "docker run" or within docker-compose e.g. ports (-p), labels, volumes, env variables, networks etc – Luxaaa Aug 15 '21 at 22:29
  • 1
    If you have a `docker-compose.yml` file, can you just use that? `docker-compose pull && docker-compose up -d` should recreate any containers whose images have changed upstream. – David Maze Aug 16 '21 at 01:25
  • `docker.APIClient#create_container` has a lot of parameters, you should provide the maximum you can extract from the old container's config if you want to clone. For your specific port binding issue, `ports` is the parameter you need to transfer from current container config. https://docker-py.readthedocs.io/en/stable/api.html#module-docker.api.container – zigarn Aug 16 '21 at 04:59
  • Maybe the `create_container_from_config` method would be more appropriate in your case. – zigarn Aug 16 '21 at 05:05

0 Answers0