Skip to content

Instantly share code, notes, and snippets.

@ihatem
Last active February 11, 2022 08:18
Show Gist options
  • Save ihatem/8275f5eb2cef9492b7ff4862456bd12d to your computer and use it in GitHub Desktop.
Save ihatem/8275f5eb2cef9492b7ff4862456bd12d to your computer and use it in GitHub Desktop.
Convert a VirtualBox ova file to a Docker image

Requirements

Steps

1. Backup VM

  • Select VM
  • Export Appliance (⌘ + E)

2. Extract files from .ova file

$ tar -xvf <file.ova>

3. Convert .vmdk to .img

$ qemu-img convert -f vmdk -O raw <file.vmdk> dev-ubuntu18.img

4. Create an .xz image for Docker with guestfish

  • get into image
$ guestfish -a /root/$PWD/dev-ubuntu18.img --ro
  • run guestfish
$ ><fs> run
  • list filesystems
$ ><fs> list-filesystems
/dev/sda1: ext4
/dev/VolGroup/lv_root: ext4
/dev/VolGroup/lv_swap: swap
  • mount your *_root to /
$ ><fs> mount /dev/VolGroup/lv_root /
  • create an .xz image
$ ><fs> tar-out / - | xz --best >> /root/<absolute_path_to_current_directory>/dev-ubuntu18.xz
  • exit
$ ><fs> exit

5. Import image to docker

$ cat dev-ubuntu18.xz | docker import - dev-ubuntu18

6. Run docker image

$ docker run -it -p 8080:80 -v ~/Projets/PlateformeDEV/:/data/apache/base/dev dev-ubuntu18 bash
  • params -p: port -v: mount host folder to docker image <host_folder>:<docker_path_to_mount_to>

7. Run services

  • apache
$ service apache2 start

if you get this error

mktemp: failed to create directory via template `/var/lock/apache2.XXXXXXXXXX'

do that:

$ mkdir /run/lock
$ service apache2 start

source: https://askubuntu.com/a/308046

  • mysql
$ service mysql start
  • memcached
$ service memcached start

8. Commit changes to the docker image

  • exit bash
$ exit
  • list docker containers to get the ID
$ docker ps -a
  • commit changes
$ docker commit <CONTAINER_ID> dev-ubuntu18

9. Add alias to make it simple

  • add an alias to your (.zshrc|.profile|.bash_profile) file
[...]
# Docker ubuntu virtual machine
alias vm-ubuntu18="docker run -it -p 8080:80 -v ~/Projets/PlateformeDEV/:/data/apache/base/dev dev-ubuntu18 bash"
@Graywolf9
Copy link

Hi! Thank you so much! This is great! How do you deal with the init system? (systemd)

@ihatem
Copy link
Author

ihatem commented Feb 11, 2022

@Graywolf9 this is very experimental, I don't recommend doing that. My docker was very instable. The best way to migrate from a vm to a docker container is identifying the main cores of your server and create the container from scratch. Sorry for not answering the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment