Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: how to clone container and its data into a new one

Tags:

docker

jenkins

Is there a way to clone a container and its data into a new one with different starting parameters?

At the moment I'm only able to start a new cloned container (from custom image) WITHOUT the data.

I tell you what I have to do: I started a "docker-jenkins" container with some starting parameters and then configured it, but now I noticed that I forgot some important starting parameters so I wanna restart the same container adding more starting parameters...

The problem is (if I understand well) that I cannot modify the starting parameters of existing running container, so my idea is to start a cloned one (data INCLUDED) with different parameters but I don't understand how to do it...

Can someone help me?

like image 843
Enricosoft Avatar asked Oct 29 '16 10:10

Enricosoft


2 Answers

1. Using volumes

If your sole point is to persist your data you need to use Volumes.

A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:

  • Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization. (Note that this does not apply when mounting a host directory.)
  • Data volumes can be shared and reused among containers.
  • Changes to a data volume are made directly.
  • Changes to a data volume will not be included when you update an image.
  • Data volumes persist even if the container itself is deleted.

Source:

https://docs.docker.com/engine/tutorials/dockervolumes/

Essentially you map a folder from your machine to one into your container. When you kill the container and spawn a new instance (with modified parameters) your volume (with the existing data) is re-mapped.

Example:

docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins

Source:

https://hub.docker.com/_/jenkins/


2. Using commit to create snapshots

A different route is to make use of the docker commit command.

It can be useful to commit a container’s file changes or settings into a new image. This allows you debug a container by running an interactive shell, or to export a working dataset to another server. Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way.

The commit operation will not include any data contained in volumes mounted inside the container. https://docs.docker.com/engine/reference/commandline/commit/

$ docker ps
ID                  IMAGE               COMMAND             CREATED             STATUS              PORTS
c3f279d17e0a        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours
197387f1b436        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours
$ docker commit c3f279d17e0a  svendowideit/testimage:version3
f5283438590d
$ docker images
REPOSITORY                        TAG                 ID                  CREATED             SIZE
svendowideit/testimage            version3            f5283438590d        16 seconds ago      335.7 MB

It is also possible to commit with altered configuration:

docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" c3f279d17e0a  svendowideit/testimage:version4
like image 125
dbalakirev Avatar answered Sep 19 '22 16:09

dbalakirev


To clone a container in docker, you can use docker commit and create a snapshot the container

Use docker images to view the docker image REPOSITORY and TAG names. Use docker ps -a to view the available containers and note the CONTAINER ID of the container of which a snapshot is to be created.

use docker commit <CONTAINER ID> <REPOSITORY>:<TAG> to create snapshot and save it as an image. Again use docker images to view the saved image.

To access saved snapshot run,

docker run -i -t <IMAGE ID> /bin/bash
docker ps -a
docker start <CONTAINER ID>
docker exec -ti <CONTAINER ID> bash
like image 35
alex devassy Avatar answered Sep 19 '22 16:09

alex devassy