[转]基于boot2docker部署Docker环境

本文转自:http://www.javashuo.com/article/p-uxmifybs-hx.htmljavascript

Docker轻量级的特性使得咱们能够快速打包开发环境:一处编译,处处使用。咱们能够在第一次编译好须要的开发环境,而后把镜像导出打包,只有有docker环境,即可以快速还原原来的开发环境。php

很经常使用的一个场景:换了电脑后,每次都须要从新安装PHP开发环境,浪费了不少时间。使用Docker,只须要预先编译一次,后续无需再次编译,就算从Windows换到了Mac、Linux,咱们编译好的环境依然可使用:只须要导入或者pull下来就行。html

在Ubuntu等环境,咱们能够一键安装Docker(服务端、客户端),可是在Mac、Windows环境却没法直接安装Docker服务端。这种状况下,咱们有3种选择:
一、在虚拟机安装CentOS或者Ubuntu:比较费时。
二、使用 docker-for-mac 或者 docker-for-windows(仅Windows10专业版支持)客户端,这种体积会比较大,通常300M左右。
三、使用 boot2docker,配合docker-machine客户端,轻松部署Docker环境。须要提早安装VirtualBox(约90M)。(推荐,可玩性较高)java

常规安装docker方法:nginx

curl -fsSL https://get.docker.com/ | sh
 # daocloud.io 国内镜像 curl -sSL https://get.daocloud.io/docker | sh

该方法适用于 Ubuntu,Debian,Centos 等大部分主流 Linux 发行版。git

准备工做

一、下载并安装VirtualBoxgithub

二、下载boot2docker.iso
https://github.com/boot2docker/boot2docker/releases/download/v18.01.0-ce/boot2docker.iso
建议使用迅雷下载。
为方便下载,网盘也存了一份:连接: https://pan.baidu.com/s/1i6QGIg9 密码: fsmbsql

三、下载docker-machinedocker

Mac直接使用brew下载:shell

brew install docker-machine 

或者:

curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine && \ chmod +x /usr/local/bin/docker-machine

Windows 打开 GitBash:

if [[ ! -d "$HOME/bin" ]]; then mkdir -p "$HOME/bin"; fi && \ curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe" && \ chmod +x "$HOME/bin/docker-machine.exe"

基于boot2docker.iso初始化环境

打开Bash命令行:

docker-machine create --driver virtualbox --virtualbox-boot2docker-url=/Users/yjc/.docker/machine/cache/boot2docker.iso default

其中--virtualbox-boot2docker-url手动指定了boot2docker.iso位置。若是不指定该参数,则会从网络直接下载,会很是慢。

若是没有出错,就能够进入这个虚拟机里面的Docker Linux里了:

$ docker-machine start default Starting "default"... (default) Check network to re-create if needed... (default) Waiting for an IP... Machine "default" was started. Waiting for SSH to be available... Detecting the provisioner... Waiting for SSH to be available... Detecting the provisioner... Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command. $ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.101:2376 Unknown Unable to query docker version: Get https://192.168.99.101:2376/v1.15/version: x509: certificate is valid for 192.168.99.100, not 192.168.99.101 $ docker-machine ssh default ## . ## ## ## == ## ## ## ## ## === /"""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\_______/ _ _ ____ _ _ | |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __ | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__| | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ | |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_| Boot2Docker version 18.01.0-ce, build HEAD : 0bb7bbd - Thu Jan 11 16:32:39 UTC 2018 Docker version 18.01.0-ce, build 03596f5 docker@default:~$

docker-machine命令:

Commands:
  active                Print which machine is active
  config                Print the connection config for machine
  create Create a machine env Display the commands to set up the environment for the Docker client inspect Inspect information about a machine ip Get the IP address of a machine kill Kill a machine ls List machines provision Re-provision existing machines regenerate-certs Regenerate TLS Certificates for a machine restart Restart a machine rm Remove a machine ssh Log into or run a command on a machine with SSH. scp Copy files between machines start Start a machine status Get the status of a machine stop Stop a machine upgrade Upgrade a machine to the latest version of Docker url Get the URL of a machine version Show the Docker Machine version or a machine docker version help Shows a list of commands or help for one command

建议打开VirtualBox给default配置共享目录:

默认会配置c/Users。配置共享目录前须要先:

docker-machine stop default

配置选项:自动挂载、固定分配。

若是须要手动挂载目录:

mount -t vboxsf work /work

/www是容器内挂载点。

配置好后:

docker-machine start default

之后重启电脑后只需运行上面一句便可。

拓展

在虚拟机机中安装docker-compose

方法

sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose -k chmod 777 -R /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose

重点
1.最后的-k 是免认证的请求方式,若是不加-k,会报SLL认证失败致使下载失败。
2.网络问题,常常超时,多试几回。

宿主机直接使用docker

每次都要docker-machine ssh 到虚拟机里面仍是挺麻烦的,其实能够直接在宿主机操做docker。首先须要安装docker客户端:

brew install docker

而后:

eval $(docker-machine env)

假设已经启动了docker,想进入容器(假设是yphp),每次都得:

eval $(docker-machine env) docker exec -it yphp /bin/bash

挺繁琐的。能够借助shell搞定:
.bashrc里加上:

eval $(docker-machine env); alias yphp="winpty docker exec -it yphp bash"

之后直接输入yphp就能够进入容器了。

参考资料

一、Docker学习笔记 - 飞鸿影~ - 博客园
http://www.cnblogs.com/52fhy/p/5638571.html
二、https://docs.docker.com/machine/install-machine/#install-machine-directly

 

 

版权申明:没有标明转载或特殊申明均为做者原创。本文采用如下协议进行受权,自由转载 - 非商用 - 非衍生 - 保持署名 | Creative Commons BY-NC-ND 3.0,转载请注明做者及出处。

做者:飞鸿影~

出处:http://52fhy.cnblogs.com/

相关文章
相关标签/搜索