To 'clone' a container, you'll have to make an image of that container first, you can do so by "committing" the container. Docker will (by default) pause all processes running in the container during commit to preserve data-consistency.
For example;
docker commit --message="Snapshot of my container" my_container my_container_snapshot:yymmdd
Commit my_container
as an image called my_container_snapshot
, and tag it yymmdd
. The "message" is optional and allows adding some extra info to the image.
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
my_container_snapshot yymmdd 9496ac4a89a6 5 seconds ago 85.1 MB
To view the "message" we added earlier, inspect the image using docker inspect [imagename]
, and to get just the message provide a --filter
option. In this case:
$ docker inspect --format="{{.Comment}}" my_container_snapshot:yymmdd
Snapshot of my container
You can start a new container from this snapshot;
docker run --name=my_clone my_container_snapshot:yymmdd
This will start a new container called "my_clone", using the snapshot we created earlier.
When dealing with database containers, most containers use a volume
for data storage. Data in volumes
is not considered part of the containers filesystem and will therefore not be included when committing a
container (nor will it be when using docker export
or docker save
).
To check if a container uses volumes and where they are located, use docker inspect
. To get just the information about 'volumes', we're providing a --filter
;
$ docker inspect --format="{{json .Volumes}}" my_container
{"/var/lib/mysql":"/var/lib/docker/vfs/dir/8274456ae711beb73de3ba0d969931c260a1bf063088f436fb08eb79b546d21d"}
This container, running mysql, is using a volume for /var/lib/mysql
, and is stored inside /var/lib/docker/vfs/dir/8274456ae711beb73de3ba0d969931c260a1bf063088f436fb08eb79b546d21d
on the host filesystem.
To export data inside a volume, use a utility container, for example, to backup the /var/lib/mysql data directory of the example above, do something like this;
docker run --rm --volumes-from=my_container busybox tar cvf - /var/lib/mysql | gzip > mysql-data.tar.gz
This instruction will;
- start a debian container
- connect to the volumes of
my_container
and mount those volumes at the same location as insidemy_container
(ie,/var/lib/mysql
) - Tar + Gzip the
/var/lib/mysql
directory to a file calledmysql-data.tar.gz
in the current directory on the host - Perform harakiri (it's a "self destructing" container; setting
--rm
will destroy the container after the command has completed)
To export a database, again use a utility container containing the tool to backup your data, for example, to backup a mysql database;
docker run --rm --link=my_container:db mysql:latest mysqldump --host=db -u root my_database > my_database_dump.sql
This instruction will;
- start a mysql container (but doesn't start the mysql server, because we're providing a different command)
- establish a link to
my_container
and name that linkdb
- add an entry to
/etc/hosts
inside the container for thedb
link. This enables us to connect to the linked container by usingdb
as hostname. - do a
mysqldump
of the database in the linked container (usingdb
as hostname) - export the dump to a file called
my_database_dump.sql
in the current directory - delete the container after the command has completed.
[write]