Does docker restart use the latest image or the one the container was created with?

If I create a container using a tag/label

docker run --name some_container -d me/my_image

Update the image with

docker pull me/my_image

And restart the container with

docker restart some_container

Which version does the new container use?

In docker inspect .Image gives the id of the specific image, .Config.Image gives the label.


Solution 1:

It uses the image the container was created from. This is pretty easy to verify.

Lets take a look at the image ID for an outdated image:

$ docker images alpine:3.2                 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
alpine              3.2                 137f13b3ac76        8 seconds ago       5.254 MB

Cool, now lets create a container based off that image:

$ docker create --name test alpine:3.2 sh
1011a97c6ed5dc0249eedc133d4f98197b379a40acc43d74f212a3d49f49db09

We can see the image that container is based off of:

$ docker inspect -f '{{.Image}}' test    
137f13b3ac76e253a90cc952c2b5921c41de0f56e8a5833e96f63e6f0c94f228

Now we pull an updated alpine:3.2:

$ docker pull alpine:3.2
3.2: Pulling from library/alpine
Digest: sha256:1b42caf22e8a6c00e4e7f8c0274495b815336d549317cf694e274832aecf11ed
Status: Image is up to date for alpine:3.2

See that it has a new image ID:

$ docker images alpine:3.2                 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
alpine              3.2                 74e49af2062e        3 days ago          5.254 MB

But the container is still using the older image ID:

$ docker inspect -f '{{.Image}}' test
137f13b3ac76e253a90cc952c2b5921c41de0f56e8a5833e96f63e6f0c94f228

When you check out the images you can see the new one and the old dangling one:

$ docker images
REPOSITORY         TAG                  IMAGE ID            CREATED             VIRTUAL SIZE
<none>             <none>               137f13b3ac76        51 seconds ago      5.254 MB
alpine             3.2                  74e49af2062e        3 days ago          5.254 MB

If you tried to delete the old image you will be met with an error:

$ docker rmi 137f13b3ac76
Error response from daemon: conflict: unable to delete 137f13b3ac76 (must be forced) - image is being used by stopped container 1011a97c6ed5
Error: failed to remove images: [137f13b3ac76]

The image can be deleted once the container that is based off it is removed.