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?

2

Best Answer


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 morecontainers that bypasses the Union File System. Data volumes provideseveral 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 volumeinitialization. (Note that this does not apply when mounting a hostdirectory.)
  • 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 intoa new image. This allows you debug a container by running aninteractive shell, or to export a working dataset to another server.Generally, it is better to use Dockerfiles to manage your images in adocumented and maintainable way.

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

$ docker psID IMAGE COMMAND CREATED STATUS PORTSc3f279d17e0a ubuntu:12.04 /bin/bash 7 days ago Up 25 hours197387f1b436 ubuntu:12.04 /bin/bash 7 days ago Up 25 hours$ docker commit c3f279d17e0a svendowideit/testimage:version3f5283438590d$ docker imagesREPOSITORY TAG ID CREATED SIZEsvendowideit/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

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 snapshotrun,

docker run -i -t <IMAGE ID> /bin/bashdocker ps -adocker start <CONTAINER ID>docker exec -ti <CONTAINER ID> bash