Skip to content

DOC: Unexpected data loss when recreate containers #2912

Closed

Description

@otbe

Hi everyone,

Im not sure if this is a docker, docker-compose or a "me" bug. But Im not getting my head around this for several hours.

My simple docker-compose.yml

version: "2"
services:
  app:
    image: nginx:latest
    volumes_from:
      - storage
  storage:
    image: ubuntu:latest
    volumes:
      - /var/data

I bootstrap it with $ docker-compose up -d.

Now I jump into "app" via docker exec -it docker_app_1 bash and add a test file within my new created volume.

root@8f5f850c30c7:/# touch /var/data/test
root@8f5f850c30c7:/# ls /var/data
test

Seems to work. Shutdown via $ docker-compose stop. Now I add a second volume to my storage container:

version: "2"
services:
  app:
    image: nginx:latest
    volumes_from:
      - storage
  storage:
    image: ubuntu:latest
    volumes:
      - /var/data
      - /my/host/folder:/var/data2

This time this volume is backed by a host directory. Rerun:

$ docker-compose up -d
Recreating docker_storage_1
Recreating docker_app_1

Okay so far. Recreated my storage container, but should preserve existing volumes (docs). Lets check this:

$ docker exec -it docker_app_1 bash
root@8f5f850c30c7:/# ls /var/data

Oh my data is gone, but why? This is not the only time my data gots "lost" the last days.
Its not 100% clear when this will happen. Simple running $ docker-compose up -d --force-recreate twice kills my data also. After the first launch my data is still there and after the second one its lost. Is this expected behavior? Recreate process seems to be broken in compose 1.6.0 :(

This happens on my local environment (OS X, docker 1.10.0, compose 1.6.0) and on my ubuntu-backed vServer (docker 1.10.1, compose 1.6.0).

Thanks for any advice

System:

$ docker-compose -v
docker-compose version 1.6.0, build d99cad6

$ docker version
Client:
 Version:      1.10.0
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   590d5108
 Built:        Thu Feb  4 18:18:11 2016
 OS/Arch:      darwin/amd64

Server:
 Version:      1.10.0
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   590d5108
 Built:        Thu Feb  4 19:55:25 2016
 OS/Arch:      linux/amd64

Activity

  1. dmp1ce commented on Feb 14, 2016

    @dmp1ce

    I'm experiencing something similar here with --force-recreate: MariaDB/mariadb-docker#42

  2. changed the title [-]DOC: Unexpected data loss when changing docker-compose.yml[/-] [+]DOC: Unexpected data loss when recreate containers[/+] on Feb 14, 2016
  3. otbe commented on Feb 14, 2016

    @otbe
    Author

    Changed title and text. Seems to be recreate related...

  4. dmp1ce commented on Feb 14, 2016

    @dmp1ce

    I hope this one gets sorted out soon. Unexpected data loss is not fun.

  5. added this to the 1.6.1 milestone on Feb 14, 2016
  6. dnephin commented on Feb 14, 2016

    @dnephin

    I see what's happening.

    We made this change (#2706) in 1.6.0. If a volume doesn't have a name it should be a "host volume" so not copied over. The problem is, docker engine seems to returning container volumes without including the name.

    The first time a volume is copied over the name exists, so the copy works just fine. The second time, the inspect looks like this:

      {
        "Propagation": "rprivate",
        "RW": true,
        "Mode": "rw",
        "Destination": "/var/data",
        "Source": "/var/lib/docker/volumes/b449ba245cef3a3f509dc41e59ba6b2e6b094702e3498077c4acaffc22f6a31b/_data"
      },

    No name! but if I inspect the volume:

    $ docker volume inspect b449ba245cef3a3f509dc41e59ba6b2e6b094702e3498077c4acaffc22f6a31b
    [
        {
            "Name": "b449ba245cef3a3f509dc41e59ba6b2e6b094702e3498077c4acaffc22f6a31b",
            "Driver": "local",
            "Mountpoint": "/var/lib/docker/volumes/b449ba245cef3a3f509dc41e59ba6b2e6b094702e3498077c4acaffc22f6a31b/_data"
        }
    ]
    

    Clearly there is a name.

    So there is an issue here, I think it might be a bug in the docker engine, at the very least there's a integration issue between compose and the engine. There may be a better way for us to re-attach the volume so that it continues to be copied over.

  7. dnephin commented on Feb 14, 2016

    @dnephin

    I'll prepare a PR for this on Tuesday, but I think this patch might fix it (still need to run the test suite against it):

    diff --git a/compose/service.py b/compose/service.py
    index 9dd2865..75000ae 100644
    --- a/compose/service.py
    +++ b/compose/service.py
    @@ -911,7 +911,7 @@ def get_container_data_volumes(container, volumes_option):
    
         for volume in set(volumes_option + image_volumes):
             # No need to preserve host volumes
    -        if volume.external:
    +        if volume.external and not volume.is_named_volume:
                 continue
    
             mount = container_mounts.get(volume.internal)
    @@ -925,7 +925,7 @@ def get_container_data_volumes(container, volumes_option):
                 continue
    
             # Copy existing volume from old container
    -        volume = volume._replace(external=mount['Source'])
    +        volume = volume._replace(external=mount['Name'])
             volumes.append(volume)
    
         return volumes
  8. dmp1ce commented on Feb 14, 2016

    @dmp1ce

    Thank you @dnephin

    That patch seems to fix the issues I had with --force-recreate when using mariadb.

  9. dnephin commented on Feb 14, 2016

    @dnephin

    The good news is that the data isn't gone, the volume is just detached from the container. A workaround to restore the data would be to do this:

    version: '2'
    volumes:
      data:
        external:
          name: <volume id>
    
    services:
      storage:
        ...
        volumes:
          - data:/var/data
    

    The difficult part is finding the volume id. docker volume ls lists them. If you only have a few it should be easy to find. If you have many, you might need to run a find over the files in /var/lib/docker/volumes/.

  10. dmp1ce commented on Feb 14, 2016

    @dmp1ce

    Does this effect docker-compose run? I'm trying to do a simple test to see attached volume with docker-compose run and it isn't working for me. docker-compose run should attach to previous volumes, shouldn't it?

    $ cat docker-compose.yml
    ubuntu:
      image: ubuntu
      volumes:
        - /data
      command: bash -c "date >> /data/testing && ls -alh /data"
    $ docker-compose up
    Creating mariadbinit_ubuntu_1
    Attaching to mariadbinit_ubuntu_1
    ubuntu_1 | total 8.0K
    ubuntu_1 | drwxr-xr-x  2 root root 4.0K Feb 14 19:44 .
    ubuntu_1 | drwxr-xr-x 22 root root  273 Feb 14 19:44 ..
    ubuntu_1 | -rw-r--r--  1 root root   29 Feb 14 19:44 testing
    mariadbinit_ubuntu_1 exited with code 0
    $ docker-compose run --rm ubuntu bash -c "sleep 1 && ls -alh /data"
    total 4.0K
    drwxr-xr-x  2 root root 4.0K Feb 14 19:45 .
    drwxr-xr-x 22 root root  273 Feb 14 19:45 ..

    Edit: Apparently not. My test doesn't work for docker-compose 1.4.0 either.

  11. dmp1ce commented on Feb 14, 2016

    @dmp1ce

    Here is a simple test for anyone who wants to manually reproduce this:

    $ cat docker-compose.yml
    ubuntu:
      image: ubuntu
      volumes:
        - /data
      command: bash -c "date >> /data/testing && ls -alh /data && cat /data/testing"
    $ docker-compose up --force-recreate 
    $ docker-compose up --force-recreate 

    You should see dates concatenated over and over again. With this issue the dates will start over after the second command.

  12. dnephin commented on Feb 14, 2016

    @dnephin

    No, volumes are not copied on run, only with up. Run should still use the volumes_from of another container, if one is defined.

  13. self-assigned this
    on Feb 16, 2016
  14. mc0e commented on Feb 22, 2016

    @mc0e

    is this related to #1576 ?

  15. mc0e commented on Feb 22, 2016

    @mc0e

    The workaround in #2912 (comment) is useful, but also seems to be inviting trouble. If that data volume is not set up properly, it could wind up getting deleted. For all that docker is not good at doing that automatically (still the case? for how long?), it's likely to be part of a manual cleanup effort.

    Better to move the data to a more permanent external volume location.

  16. shin- commented on Feb 22, 2016

    @shin-

    It is being fixed in the upcoming release, see #2934.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions