Migrer de boot2docker vers docker-machine


Ça fait un moment qu’à chaque utilisation de la commande boot2docker j’ai un joli message d’avertissement :

  WARNING: The 'boot2docker' command line interface (not to be confused with
  'boot2docker' the operating system) is officially deprecated.

  Please switch to Docker Machine (https://docs.docker.com/machine/) ASAP.

  Docker Toolbox (https://docker.com/toolbox) is the recommended install method.

Je me suis dit qu’il était temps de finalement faire la migration et de tester quelques goodies !

Désinstallation boot2docker

C’est la partie simple,

> boot2docker delete
> brew uninstall boot2docker

Installation de docker-machine

Là encore brew nous sauve la vie,

> brew install docker-machine

docker-machine va nous permettre de provisionner docker dans des machines virtuelles (qu’elles soient locales ou dans le cloud) et il se charge de configurer le client docker pour parler avec ces machines.

Pour reproduire l’utilisation de boot2docker il faut créer un environnement en utilisant le driver virtualbox :

> docker-machine create --driver virtualbox dev

L’argument dev est le nom que de la machine, on peut y mettre ce qu’on veut.

Creating CA: /Users/jcgay/.docker/machine/certs/ca.pem
Creating client certificate: /Users/jcgay/.docker/machine/certs/cert.pem
Running pre-create checks...
(dev) Image cache directory does not exist, creating it at /Users/jcgay/.docker/machine/cache...
(dev) No default Boot2Docker ISO found locally, downloading the latest release...
(dev) Latest release for github.com/boot2docker/boot2docker is v1.9.1
(dev)
(dev) Boot2Docker v1.9.1 has a known issue with AUFS.
(dev) See here for more details: https://github.com/docker/docker/issues/18180
(dev) Consider specifying another storage driver (e.g. 'overlay') using '--engine-storage-driver' instead.
(dev)
(dev) Downloading /Users/jcgay/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.9.1/boot2docker.iso...
(dev) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(dev) Copying /Users/jcgay/.docker/machine/cache/boot2docker.iso to /Users/jcgay/.docker/machine/machines/dev/boot2docker.iso...
(dev) Creating VirtualBox VM...
(dev) Creating SSH key...
(dev) Starting the VM...
(dev) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect Docker to this machine, run: docker-machine env dev

On peut bien entendu configurer un certains nombre d’options (CPU, mémoire, etc). La liste complète est disponible dans la documentation du driver VirtualBox.

On voit que docker-machine se charge de télécharger un ISO boot2docker, il nous indique aussi la commande à exécuter pour configurer le client docker.

> docker-machine env dev
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/jcgay/.docker/machine/machines/dev"
export DOCKER_MACHINE_NAME="dev"
# Run this command to configure your shell:
# eval $(docker-machine env dev)

Allez on finit donc par un :

> eval $(docker-machine env dev)

Et voilà docker fonctionne comme avant !

> docker-machine stop dev

Les volumes

Je dois certainement mal m’y prendre mais c’est vraiment la fonctionnalité qui me pose tout le temps problème, à chaque fois que j’essaie de démarrer un conteneur en mappant un volume sur ma machine, j’ai des problèmes de droit 🤔

Par exemple avec mongo:

> docker run -v /Users/jcgay/data/mongo:/data/db mongo
[initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=968af53e81ec
[initandlisten] db version v3.2.1
[initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
[initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
[initandlisten] allocator: tcmalloc
[initandlisten] modules: none
[initandlisten] build environment:
[initandlisten]     distmod: debian71
[initandlisten]     distarch: x86_64
[initandlisten]     target_arch: x86_64
[initandlisten] options: {}
[initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

ou encore mysql:

    > docker run -v /Users/jcgay/tmp/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=plop mysql      

Initializing database
[Warning] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
[ERROR] InnoDB: Operating system error number 13 in a file operation.
[ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
[ERROR] InnoDB: Operating system error number 13 in a file operation.
[ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
[ERROR] InnoDB: Cannot open datafile './ibdata1'
[ERROR] InnoDB: Could not open or create the system tablespace. If you tried to add new data files to the system tablespace, and it failed here, you should now edit innodb_data_file_path in my.cnf back to what it was, and remove the new ibdata files InnoDB created in this failed attempt. InnoDB only wrote those files full of zeros, but did not yet use them in any way. But be careful: do not remove old data files which contain your precious data!
[ERROR] InnoDB: InnoDB Database creation was aborted with error Cannot open a file. You may need to delete the ibdata1 file before trying to start up again.
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[ERROR] Failed to initialize plugins.
[ERROR] Aborting

Apparemment le partage de volume ne fonctionne que si le partage de stockage avec VirtualBox se fait avec l’utilisateur root. Il y a un long thread ouvert chez boot2docker.

Activer NFS pour le mapping de volumes

Le projet docker-machine-nfs permet de remplacer le partage VirtualBox par du NFS. En bonus, les performances de lecture/écriture devraient être meilleures.

Après installation, il faut activer NFS sur la machine désirée :

> docker-machine-nfs dev
[INFO] Configuration:

	- Machine Name: dev
	- Shared Folder: /Users
	- Force: false

[INFO] machine presence ... 			OK
[INFO] machine running ... 			    OK
[INFO] Lookup mandatory properties ...  OK

	- Machine IP: 192.168.99.100
	- Network ID: vboxnet4
	- NFSHost IP: 192.168.99.1

[INFO] Configure NFS ...

 !!! Sudo will be necessary for editing /etc/exports !!!
						
[INFO] Configure Docker Machine ... 	OK
[INFO] Restart Docker Machine ... 		OK
[INFO] Verify NFS mount ... 			OK

--------------------------------------------

 The docker-machine 'dev'
 is now mounted with NFS!

 ENJOY high speed mounts :D

--------------------------------------------

Bon et en fait chez moi, ce n’est toujours pas mieux 😱

    > docker run -v /Users/jcgay/tmp/mongo:/data/db mongo                                        
    chown: changing ownership of `/data/db': Operation not permitted

    > docker run -v /Users/jcgay/tmp/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=plop mysql      
    chown: changing ownership of '/var/lib/mysql/': Operation not permitted

On peut modifier la configuration du mapping pour changer les droits, voir le ticket #17)

> docker-machine-nfs dev --force --nfs-config="-alldirs -maproot=0"

Et enfin ça fonctionne ! 🎆🚀

Docker survitaminé

Maintenant que docker-machine s’occupe de tout, on peut tenter d’utiliser d’autres drivers. On peut essayer xhyve un hyperviseur natif pour OS X et ainsi se passer de VirtualBox.

> brew install xhyve docker-machine-driver-xhyve

Oui il faut être root…

> sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
> sudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

On crée la machine :

> docker-machine create xhyve --driver xhyve --xhyve-experimental-nfs-share

Toutes les options sont documentées sur la page officielle du driver.

Je ne me suis pas amusé à comparer les performances des deux solutions mais pour le moment je n’ai pas rencontré de soucis avec cette dernière.


comments powered by Disqus