Docker 1.9.0 and above
Use volume API
docker volume create --name hello
docker run -d -v hello:/container/path/for/volume container_image my_command
This means that the data-only container pattern must be abandoned in favour of the new volumes.
Actually the volume API is only a better way to achieve what was the data-container pattern.
If you create a container with a -v volume_name:/container/fs/path
Docker will automatically create a named volume for you that can:
- Be listed through the
docker volume ls
- Be identified through the
docker volume inspect volume_name
- Backed up as a normal directory
- Backed up as before through a
--volumes-from
connection
The new volume API adds a useful command that lets you identify dangling volumes:
docker volume ls -f dangling=true
And then remove it through its name:
docker volume rm <volume name>
As @mpugach underlines in the comments, you can get rid of all the dangling volumes with a nice one-liner:
docker volume rm $(docker volume ls -f dangling=true -q)
# Or using 1.13.x
docker volume prune
Docker 1.8.x and below
The approach that seems to work best for production is to use a data only container.
The data only container is run on a barebones image and actually does nothing except exposing a data volume.
Then you can run any other container to have access to the data container volumes:
docker run --volumes-from data-container some-other-container command-to-execute
- Here you can get a good picture of how to arrange the different containers.
- Here there is a good insight on how volumes work.
In this blog post there is a good description of the so-called container as volume pattern which clarifies the main point of having data only containers.
Docker documentation has now the DEFINITIVE description of the container as volume/s pattern.
Following is the backup/restore procedure for Docker 1.8.x and below.
BACKUP:
sudo docker run --rm --volumes-from DATA -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
- --rm: remove the container when it exits
- --volumes-from DATA: attach to the volumes shared by the DATA container
- -v $(pwd):/backup: bind mount the current directory into the container; to write the tar file to
- busybox: a small simpler image - good for quick maintenance
- tar cvf /backup/backup.tar /data: creates an uncompressed tar file of all the files in the /data directory
RESTORE:
# Create a new data container
$ sudo docker run -v /data -name DATA2 busybox true
# untar the backup files into the new container᾿s data volume
$ sudo docker run --rm --volumes-from DATA2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
data/
data/sven.txt
# Compare to the original container
$ sudo docker run --rm --volumes-from DATA -v `pwd`:/backup busybox ls /data
sven.txt
Here is a nice article from the excellent Brian Goff explaining why it is good to use the same image for a container and a data container.
Docker has a default entrypoint which is /bin/sh -c
but does not have a default command.
When you run docker like this:
docker run -i -t ubuntu bash
the entrypoint is the default /bin/sh -c
, the image is ubuntu
and the command is bash
.
The command is run via the entrypoint. i.e., the actual thing that gets executed is /bin/sh -c bash
. This allowed Docker to implement RUN
quickly by relying on the shell's parser.
Later on, people asked to be able to customize this, so ENTRYPOINT
and --entrypoint
were introduced.
Everything after ubuntu
in the example above is the command and is passed to the entrypoint. When using the CMD
instruction, it is exactly as if you were doing docker run -i -t ubuntu <cmd>
. <cmd>
will be the parameter of the entrypoint.
You will also get the same result if you instead type this command docker run -i -t ubuntu
. You will still start a bash shell in the container because of the ubuntu Dockerfile specified a default CMD: CMD ["bash"]
As everything is passed to the entrypoint, you can have a very nice behavior from your images. @Jiri example is good, it shows how to use an image as a "binary". When using ["/bin/cat"]
as entrypoint and then doing docker run img /etc/passwd
, you get it, /etc/passwd
is the command and is passed to the entrypoint so the end result execution is simply /bin/cat /etc/passwd
.
Another example would be to have any cli as entrypoint. For instance, if you have a redis image, instead of running docker run redisimg redis -H something -u toto get key
, you can simply have ENTRYPOINT ["redis", "-H", "something", "-u", "toto"]
and then run like this for the same result: docker run redisimg get key
.
Best Solution
Not sure if it helps under windows. But for Mac see:
boot2docker together with VirtualBox Guest Additions
How to mount /Users into boot2docker
https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c