Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create docker image from .img file containing OS

Is it possible to convert an .img file containing an OS (Arch Linux) into a Docker image? More precisely I want to dockerize a RuneAudio Raspberry Pi image.

like image 903
Andrej Guráň Avatar asked Apr 12 '16 12:04

Andrej Guráň


1 Answers

Producing a Docker image from a full operating system image is often a sub-optimal process. The operating system image is going to include a variety of things that are simply not necessary in the Docker environment, which simply means that the resulting image is going to be unnecessarily large.

That said, if you want to try this anyway, the guestfish command from the libguestfs package makes this very simple:

guestfish --ro -a RuneAudio_rpi_0.3-beta_20141029_2GB.img -m /dev/sda5:/ tar-out / - |
docker import - runeaudio 

That will create a runeaudio docker image with the contents of the RuneAudio_rpi_0.3-beta_20141029_2GB.img disk image. Note that this will, of course, only run under Docker running on a Raspberry Pi, and the resulting image isn't necessarily going to work without further modification.

You can also accomplish the same thing by mounting the disk image locally:

losetup -P /dev/loop0 RuneAudio_rpi_0.3-beta_20141029_2GB.img
mount /dev/loop0p5 /mnt
tar -C /mnt -cf - | docker import - runeaudio
umount /mnt
losetup -d /dev/loop0

I like guestfish because it doesn't require root access, and doesn't require mucking about with loop devices and mountpoints, so there's less setup and cleanup.

like image 184
larsks Avatar answered Nov 02 '22 03:11

larsks