欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Mac OS上通过boot2docker安装使用Docker的教程

程序员文章站 2022-06-25 19:01:16
其实Docker的跨平台性到现在也一直没有做太好,包括在Mac上目前最普遍的方式还是通过boot2docker这样一个类似虚拟机的东西...这里我们就来看一下Mac OS上通过boot2docker安装使用Docke... 16-06-07...

安装
boot2docker是将docker daemon运行在虚拟机中的,所以系统中必须要有虚拟机环境:vmware、virtualbox、parallels都可以,必须要有,且使用的工具包有所不同。
本人习惯于使用parallels desktop,机子上本来装着就有,所以优先选择parallels环境。
使用parallels环境可以不用安装boot2docker,直接安装docker-machine-parallels即可。
项目地址:https://github.com/parallels/docker-machine-parallels
使用homebrew先查看下都有哪些软件:

复制代码
代码如下:

selflydemacbook-pro:~ liyd$ brew search docker

这里我们只需要安装两个软件包:docker和docker-machine-parallels,其中docker-machine会成为docker-machine-parallels的依赖而自动进行安装。
可以看到上面这三个软件包后面都有个勾,因为我本机已经安装过了。

复制代码
代码如下:

selflydemacbook-pro:~ liyd$ brew install docker-machine-parallels
selflydemacbook-pro:~ liyd$ brew install docker

创建虚拟机
只需要一个命令,

复制代码
代码如下:

selflydemacbook-pro:~ liyd$ docker-machine create --driver=parallels prl-dev

当然还有一些其它的参数,具体可以看项目主页上的说明。
第一次创建会比较慢,因为要下载boot2docker.iso。
如果提示下载失败,可以自行到github上去下载后,放到~/.docker/machine/cache目录下。

使用
创建虚拟机后,会提示你使用docker-machine env命令,然后再用eval命令:

复制代码
代码如下:

selflydemacbook-pro:~ liyd$ docker-machine env prl-dev
export docker_tls_verify="1"
export docker_host="tcp://10.211.55.11:2376"
export docker_cert_path="/users/liyd/.docker/machine/machines/prl-dev"
export docker_machine_name="prl-dev"
# run this command to configure your shell:
# eval $(docker-machine env prl-dev)
selflydemacbook-pro:~ liyd$ eval $(docker-machine env prl-dev)

至此,就可以像平常使用docker一样来*发挥了!

容器端口跳转
最新版的boot2docker建立了一个仅有网络适配器的主机提供可以接入容器的端口。
如果你运行一个有公开端口的容器,

复制代码
代码如下:

$ docker run --rm -i -t -p 80:80 nginx

然后你应该能用ip地址接入nginx服务器:

复制代码
代码如下:

$ boot2docker ip

通常,这个ip地址为192.168.59.103,但是也可能被virtualbox启用的dhcp修改。

其他
如果你好奇的话,boot2docker的缺省用户名为docker,密码为tcuser。
boot2docker管理工具提供了很多命令:

复制代码
代码如下:

$ ./boot2dockerusage: ./boot2docker [<options>]{help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|delete|download|version} [<args>]

docker-machine还有一些其它非常有用的命令,可以自行在命令行查看:
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