Persistent docker containers - using dockers as virtual machine


Recommended Posts

Hi,

I would like to share with community my new modification. Maybe limetech will include this option in unraid.

To the point. When complicated application, with many system dependencies has to be deployed or developed it is convenient to use virtual machine. However virtual machine has extra overtread. Also dedicated RAM or GPU has to be attached to this VM only. With docker one GPU can be used in several containers, also ram is shared dynamically. It is nothing new as openvz can do that very well. Unfortunately unraid is lacking openvz. On the other hand in microservices approach present in docker environment it is assumed that only data is persistent and whole operating system libraries, application libraries are included in image and can be erased and restored from the image at any time in any version. It works well only when application designer is aware of this.

However there are other use cases, when docker approach will fail:

  • In development, when using application that depends heavily on libraries to be in one particular version – e.g. shinobi with yolo, tensorflow + cuda.
  • Very often an effort or cost of adjusting application to work in docker philosophy is to high.
  • When migrating physical machine or virtual machine.
  • When one gpu should accelerate several applications.

For this reason I have created small feature for unraid to make container persistent. Unraid is working in agreement with docker philosophy, therefore at any parameter change in unraid’s gui or when update is released it will erase whole container and recreate it. My code makes container detached from main repo and persistent across parameter edit.

 

To make it work user.script plugin is required. Then my script should be copied to folder “/boot/config/plugins/user.scripts/scripts/persistentDockerContainers” and scheduled to be run once after array is started.

persistentDockerContainersScreen.thumb.png.47c63f4d9b365f22425d77eb5998f757.png

 

How to use my code?

You need to edit repository field, by adding “repository-“ prefix. Each container that is used as persistent should be having unique repository field !!! It is also advised that name of the container should be changed to unique, because unraid has undesirable behavior that, when another container is installed via community apps and container with the same name exists it will be deleted without any warning.

In short, to make container persistent:

1.       Change name to something unique.

2.       Set unique repository name starting with prefix “persistent-“

 

persistentDockerContainersScreen2.png.a3bbc61d07c99c2cff470234a5ada6d7.png

 

That is all folks. Give me some feedback if You find it useful please. I don’t know if I should continue posting my solutions, as it takes a lot of time. I will do it only if it is useful for community.

 

Best Regards,

Kacper

 

p.s.

For people who are reasonable not to run someones code without checking it:

 

root@nas:~# diff CreateDocker.php CreateDocker.php.org
108,126d107
<
<     // check what is name of the container to image, important when changing container name
<   if ($existing && $DockerClient->doesContainerExist($existing)) {$ContainerNameToImage = $existing; }
<   else if ($DockerClient->doesContainerExist($Name)){ $ContainerNameToImage = $Name; }
<   else{ $ContainerNameToImage = "";
<   }
<
<     // do the image
<   if ($DockerClient->doesContainerExist($ContainerNameToImage) && (strpos($Repository, "persistent-") !== false)){
<     // repository name must by unique for presistent containers - this not guaranteed if user creates several containers from one repository and makes all containers persistent
<     $oldContainerInfo = $DockerClient->getContainerDetails($ContainerNameToImage);
<     if (!empty($oldContainerInfo) && !empty($oldContainerInfo['State']) && !empty($oldContainerInfo['State']['Running'])) {
<       // attempt graceful stop of container first
<       $startContainer = true;
<       stopContainer($ContainerNameToImage);
<     }
<     $DockerClient->commitContainer($ContainerNameToImage, $Repository); // commit persistent container
<   }
<

root@nas:~# diff DockerClient.php DockerClient.php.org
772,777d771
<       public function commitContainer($id, $repo) {
<               $this->getDockerJSON("/commit?container=$id&repo=$repo", 'POST', $code);
<               $this->flushCache($this::$containersCache);
<               return $code;
<       }
<

 

persistentDockerContainers_unraid_6_9_0.zip

Edited by Kacper
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.