安装docker
[root@lianxi ~]# yum -y install docker
启动docker
[root@lianxi ~]# systemctl start docker
下载Docker Machine二进制文件并将其解压缩到PATH
[root@lianxi ~]# curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine && chmod +x /tmp/docker-machine && sudo cp /tmp/docker-machine /usr/local/bin/docker-machine
检查安装版本
[root@lianxi ~]# docker-machine version
docker-machine version 0.12.2, build 9371605
创建一台虚拟机
命令
docker-machine create -d virtualbox VMname
如果遇到以下问题
[root@lianxi ~]# docker-machine create -d virtualbox VMname Creating CA: /root/.docker/machine/certs/ca.pem Creating client certificate: /root/.docker/machine/certs/cert.pem Running pre-create checks... Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed
and VBoxManage is in the path"
上传安装包,下载地址
链接:http://pan.baidu.com/s/1slpxVzv 密码:qrlk
安装依赖并安装 VirtualBox
[root@lianxi ~]# yum -y localinstall VirtualBox-5.1-5.1.20_114628_el7-1.x86_64.rpm
再次创建
[root@lianxi ~]# docker-machine create -d virtualbox VMname
查看新建的机器
[root@lianxi ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKERORS default - VMname Running tcp://192.168.99.100:2376 v17.0
获取新虚拟机的环境信息
[root@lianxi ~]# docker-machine env VMname export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/root/.docker/machine/machines/default" export DOCKER_MACHINE_NAME="default" # Run this command to configure your shell: # eval $(docker-machine env default)
将宿主机的环境变量导入到创建的虚拟机当中
[root@lianxi ~]# eval $(docker-machine env VMname)
查看虚拟机IP
[root@lianxi ~]# docker-machine ip VMname
查看全部虚拟机和IP
[root@lianxi ~]# docker-machine ls
进入到新创建的虚拟机
[root@lianxi ~]# docker-machine ssh VMname
下载Nginx 镜像
docker@VMname:~$ docker pull nginx
安装Nginx
docker@VMname:~$ docker run -d -p 8000:80 --name nginx01 nginx
也可以在虚拟机外安装Nginx
[root@lianxi ~]# docker-machine ssh VMnaem docker pull nginx [root@lianxi ~]# docker-machine ssh VMname run -d -p 8000:80 --name nginx01 nginx
访问nginx
[root@lianxi ~]# docker-machine ssh VMname curl -I 192.168.99.101:8000 HTTP/1.1 200 OK Server: nginx/1.13.3 Date: Sat, 02 Sep 2017 09:39:32 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 11 Jul 2017 13:06:07 GMT Connection: keep-alive ETag: "5964cd3f-264" Accept-Ranges: bytes % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 612 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
启动
[root@lianxi ~]# docker-machine start VMname
停止
[root@lianxi ~]# docker-machine stop VMname
重启
[root@lianxi ~]# docker-machine restart VMname