4

I have four containers that was node ,redis, mysql, and data. when i run docker-compose rm,it will remove all of my container that include the container data.my data of mysql is in the the container and i don't want to rm the container data.

why i must rm that containers?

Sometime i must change some configure files of node and mysql and rebuild.So ,I must remove containers and start again.

I have searched using google again over again and got nothing.

jamlee
  • 1,234
  • 1
  • 13
  • 26

3 Answers3

3

As things stand, you need to keep your data containers outside of Docker Compose for this reason. A data container shouldn't be running anyway, so this makes sense.

So, to create your data-container do something like:

docker run --name data mysql echo "App Data Container"

The echo command will complete and the container will exit immediately, but as long as you don't docker rm the container you will still be able to use it in --volumes-from commands, so you can do the following in Compose:

db:
  image: mysql
  volumes-from: 
     - data

And just remove any code in docker-compose.yml to start up the data container.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • "As long as you don't `docker rm`": that is my current issue, by the way: what happen when you rm your container (but not the volume): how to run a *new* data volume container, and still have it reference your old (now stranded) volume? – VonC Apr 10 '15 at 12:58
  • You have to figure out where it is. If you run `docker inspect -f {{.Volumes}} CONTAINER` you'll see the directory for your volume. But in general, you don't want to be playing with orphan volumes. Have a look at https://github.com/cpuguy83/docker-volumes for some more tools. – Adrian Mouat Apr 10 '15 at 13:33
  • "in general, you don't want to be playing with orphan volumes." Do you have a choice though? Your container is gone, and you *have* to restore a new container (same name) pointing to the old data. – VonC Apr 10 '15 at 13:45
  • @VonC Just don't delete data containers :) And make back-ups, which you can then use to populate new data containers. – Adrian Mouat Apr 10 '15 at 13:48
  • "Just don't delete data containers": that is exactly the issue I am facing right now. Due to some stringent policies, *all* containers are removed (without the -v). And then re-declared. The expectation is to find back its data (which sits, undisturbed, in `/var/lib/docker/vfs/dir`). I'll post a question on Stack Overflow next week. – VonC Apr 10 '15 at 13:52
  • @VonC Ok. Deleting data containers is a bit silly, but why can't you just use specific dirs on the host i.e. use the -v HOST_DIR:CONTAINER_DIR syntax? – Adrian Mouat Apr 10 '15 at 14:02
  • Because that ties you to the HOST (which, depending on the host environment like Windows, won't offer you to mount a "portable path": I describe that issue in http://stackoverflow.com/a/29554135/6309). The best practice is Data Volume Container (as opposed to a simple data volume mounted from the host). A Data Volume Container can be backed up easily, but here I am not concerned with backup. I am concerned with restoring the data to its container when said container is recreated (with the same name as before). – VonC Apr 10 '15 at 14:06
  • @AdrianMouat @VonC `docker run --name data -v /var/lib/mysql mysql echo "App Data Container"`, the option `"-v /var/lib/mysql"` could be ignored? .And if i use the -v HOST_DIR:CONTAINER_DIR syntax,my mysql container will exit soon. So i find the solution here http://blog.tutum.co/2014/05/27/containerize-your-database-volume-with-tutum-mysql-images/. – jamlee Apr 10 '15 at 14:26
  • @VonC ok, but you're going to find this tricky - it's not a normal use case. – Adrian Mouat Apr 10 '15 at 15:08
  • jamlee - yeah, you can ignore the -v as it's declared in the Dockerfile, so it's implicit anyway. I don't understand the rest of your comment, but that blog is pretty much saying the same thing I just did (except it wrongly uses an ubuntu image as a data container). – Adrian Mouat Apr 10 '15 at 15:09
  • @Adrian it just is some problem about docker privileged .thank for your help. I have solve the problem with your help. – jamlee Apr 25 '15 at 14:07
1

An alternative to docker-compose, in Go (https://github.com/michaelsauter/crane), let's you create contianer groups -- including overriding the default group so that you can ignore your data containers when rebuilding your app.

Given you have a "crane.yaml" with the following containers and groups:

containers:
    my-app:
       ...
    my-data1:
       ...
    my-data2:
       ...
groups:
    default:
        - "my-app"
    data:
        - "my-data1"
        - "my-data2"

You can build your data containers once:

# create your data-only containers (safe to run several times)
crane provision data   # needed when building from Dockerfile
crane create data

# build/start your app.
crane lift -r  # similar to docker-compose build && docker compose up

# Force re-create off your data-only containers...
crane create --recreate data

PS! Unlike docker-compose, even if building from Dockerfile, you MUST specify an "image" -- when not pulling, this is the name docker will give the image locally! Also note that the container names are global, and not prefixed by the folder name the way they are in docker-compose.

Note that there is at least one major pitfall with crane: It simply ignores misplaced or wrongly spelled fields! This makes it harder to debug that docker-compose yaml.

Sindre Myren
  • 999
  • 6
  • 8
0

@AdrianMouat Now , I can specify a *.yml file when I starting all container with the new version 1.2rc of docker-compose (https://github.com/docker/compose/releases). just like follows:

file:data.yml

data: image: ubuntu volumes: - "/var/lib/mysql" thinks for your much useful answer

jamlee
  • 1,234
  • 1
  • 13
  • 26