Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an image from a container running in Kubernetes?

I want to be able to perform something like docker commit on a container running inside a Kubernetes pod.

Volume snapshots do not meet my criteria because I want to snapshot the complete state of the container, not just the data mounted under some path.

I need this because I provide (trusted) users root access to containers and I want to be able to provide a way to roll the containers back without losing any packages they have installed (or any other changes they have made) before the last snapshot.

It appears a related feature request was made but nothing came of it.

I am using Google Kubernetes Engine but I am prepared to migrate to another provider that easily supports this.

like image 225
neprune Avatar asked Nov 08 '22 07:11

neprune


1 Answers

Run the bash shell inside a container named guest:

[root@host ~]# docker run -i -t --name guest oraclelinux:6.6 /bin/bash
[root@guest ~]#

If you use a web proxy, edit the yum configuration on the guest as described in the Oracle Linux Administrator's Guide for Release 7.

Install the httpd package:

[root@guest ~]# yum install httpd

If required, create the web content to be displayed under the /var/www/html directory hierarchy on the guest.

Exit the guest by using the docker stop command on the host:

[root@host ~]# docker stop guest
guest

Create the image mymod/httpd with the tag v1 using the ID of the container that you stopped:

[root@host ~]# docker commit -m "ol6 + httpd" -a "A N Other" \
`docker ps -l -q` mymod/httpd:v1
8594abec905e6374db51bed1bfb208804cfb60d96b285efb897db581a01676e9

Use the -m and -a options to document the image and its author. The command returns the full version of the new image's ID.

If you use the docker images command, the new image now appears in the list:

  [root@host ~]# docker images
    REPOSITORY    TAG         IMAGE ID       CREATED       VIRTUAL SIZE
    mymod/httpd   v1          8594abec905e   2 minutes ago 938.5 MB
    oraclelinux   6           9ac13076d2b5   5 days ago    319.4 MB
    oraclelinux   6.6         9ac13076d2b5   5 days ago    319.4 MB
    oraclelinux   latest      073ded22ac0f   5 days ago    265.2 MB
    oraclelinux   7           073ded22ac0f   5 days ago    265.2 MB
    oraclelinux   7.0         073ded22ac0f   5 days ago    265.2 MB

Remove the container named guest.

    # docker rm guest
    guest

You can now use the new image to create a container that works as a web server, for example:

 # docker run -d --name newguest -p 8080:80 mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND
 7afbbefec5191f632e149f85ae10ed0ba88f1c545daad18cb930e575ef6a3e63
like image 102
James Knott Avatar answered Nov 14 '22 21:11

James Knott