Difference between revisions of "Runc"

m
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
== Container Checkpoint/Restore ==
 
== Container Checkpoint/Restore ==
  
 +
Create a container rootfs
 
<pre>
 
<pre>
# Create a container rootfs
 
 
mkdir -p alpine-container/rootfs && cd alpine-container
 
mkdir -p alpine-container/rootfs && cd alpine-container
 
wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-3.22.2-x86_64.tar.gz
 
wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-3.22.2-x86_64.tar.gz
 
tar -xzf alpine-minirootfs-3.22.2-x86_64.tar.gz -C rootfs
 
tar -xzf alpine-minirootfs-3.22.2-x86_64.tar.gz -C rootfs
 
rm -f alpine-minirootfs-3.22.2-x86_64.tar.gz
 
rm -f alpine-minirootfs-3.22.2-x86_64.tar.gz
 +
</pre>
  
# Create container spec file
+
Create a container specification file:
 +
 
 +
<pre>
 
runc spec
 
runc spec
 +
</pre>
 +
 +
Set a command to run in the container:
  
# Set looper command
+
<pre>
 
sed -i 's/"sh"/"sh", "-c", "i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done"/' config.json
 
sed -i 's/"sh"/"sh", "-c", "i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done"/' config.json
 +
</pre>
 +
 +
Start the container and redirect the output/error to <code>looper.out</code> and <code>looper.out</code>:
 +
 +
<pre>
 +
sudo runc run looper 0</dev/null 1>looper.out 2>looper.err &
 +
</pre>
  
# Disable interactive terminal
+
Alternatively, you can disable the interactive terminal and run container in the background as follows:
 +
 
 +
<pre>
 
sed -i 's/"terminal": true/"terminal": false/g' config.json
 
sed -i 's/"terminal": true/"terminal": false/g' config.json
 +
sudo runc run -d looper 0</dev/null 1>/dev/null 2>/dev/null
 +
</pre>
  
# Run container in the background
+
Show running containers
sudo runc run -d looper 0</dev/null 1>/dev/null 2>/dev/null
 
  
# Show running containers
+
<pre>
 
sudo runc list
 
sudo runc list
 
sudo runc state looper
 
sudo runc state looper
Line 40: Line 56:
 
<pre>
 
<pre>
 
sudo runc restore --image-path /tmp/checkpoint --work-path /tmp/checkpoint looper
 
sudo runc restore --image-path /tmp/checkpoint --work-path /tmp/checkpoint looper
 +
</pre>
  
# Show running containers
+
Show running containers:
 +
 
 +
<pre>
 
sudo runc list
 
sudo runc list
 
sudo runc state looper
 
sudo runc state looper
Line 48: Line 67:
 
The following command can be used to terminate the running container:
 
The following command can be used to terminate the running container:
 
<pre>
 
<pre>
runc kill looper
+
sudo runc kill looper KILL
 +
sudo runc delete looper
 
</pre>
 
</pre>

Latest revision as of 18:56, 9 October 2025

This article provides an overview of CRIU integration with runc and explains how to use it.

Container Checkpoint/RestoreEdit

Create a container rootfs

mkdir -p alpine-container/rootfs && cd alpine-container
wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-3.22.2-x86_64.tar.gz
tar -xzf alpine-minirootfs-3.22.2-x86_64.tar.gz -C rootfs
rm -f alpine-minirootfs-3.22.2-x86_64.tar.gz

Create a container specification file:

runc spec

Set a command to run in the container:

sed -i 's/"sh"/"sh", "-c", "i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done"/' config.json

Start the container and redirect the output/error to looper.out and looper.out:

sudo runc run looper 0</dev/null 1>looper.out 2>looper.err &

Alternatively, you can disable the interactive terminal and run container in the background as follows:

sed -i 's/"terminal": true/"terminal": false/g' config.json
sudo runc run -d looper 0</dev/null 1>/dev/null 2>/dev/null

Show running containers

sudo runc list
sudo runc state looper

Now the container can be checkpointed:

mkdir /tmp/checkpoint
sudo runc checkpoint --image-path /tmp/checkpoint --work-path /tmp/checkpoint looper

Once the container is checkpointed it will be no longer visible in runc list.

The following command can be used to restore the container:

sudo runc restore --image-path /tmp/checkpoint --work-path /tmp/checkpoint looper

Show running containers:

sudo runc list
sudo runc state looper

The following command can be used to terminate the running container:

sudo runc kill looper KILL
sudo runc delete looper