Skip to content

Instantly share code, notes, and snippets.

@ca4ti
Forked from ihatem/convert_ova_docker.md
Created August 31, 2024 11:36
Show Gist options
  • Save ca4ti/c7dde2d552b3cf5644f595c9bc4482db to your computer and use it in GitHub Desktop.
Save ca4ti/c7dde2d552b3cf5644f595c9bc4482db 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment