安装docker Toolbox

win7系统需要安装docker Toolbox来使用。
docker Toolbox国内镜像下载地址:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/
安装好后桌面会多出三个图标,分别是:Oracle VM VirtualBox,Kitematic (Alpha),Docker Quickstart Terminal。
win安装docker并部署CentOS 宝塔环境 - 图1
双击Docker Quickstart Terminal图标来启动 Docker Toolbox 终端。稍等片刻后,会显示:
win安装docker并部署CentOS 宝塔环境 - 图2
代表docker环境已经安装成功。宿主主机IP为:192.168.99.100

运行hello world

在终端中输入命令:

  1. docker run hello-world

显示结果如下:

  1. $ docker run hello-world
  2. Unable to find image 'hello-world:latest' locally
  3. Pulling repository hello-world
  4. 91c95931e552: Download complete
  5. a8219747be10: Download complete
  6. Status: Downloaded newer image for hello-world:latest
  7. Hello from Docker.
  8. This message shows that your installation appears to be working correctly.
  9. ``
  10. To generate this message, Docker took the following steps:
  11. 1. The Docker Engine CLI client contacted the Docker Engine daemon.
  12. 2. The Docker Engine daemon pulled the "hello-world" image from the Docker Hub.
  13. (Assuming it was not already locally available.)
  14. 3. The Docker Engine daemon created a new container from that image which runs the
  15. executable that produces the output you are currently reading.
  16. 4. The Docker Engine daemon streamed that output to the Docker Engine CLI client, which sent it
  17. to your terminal.
  18. ``
  19. To try something more ambitious, you can run an Ubuntu container with:
  20. $ docker run -it ubuntu bash
  21. ``
  22. For more examples and ideas, visit:
  23. https://docs.docker.com/userguide/

    拉取centos7镜像

    如果需要安装最新的centos镜像,请将7改为:latest
    更多的centos镜像:https://hub.daocloud.io/repos/bf207788-d7de-4044-bdeb-521a998f748b
    在终端输入以下命令:

  24. docker pull daocloud.io/centos:7

win安装docker并部署CentOS 宝塔环境 - 图3

设置共享文件夹

首先在G:\soft\目录下,新建一个docker文件夹用作共享文件夹,然后在docker文件夹里新建一个www文件夹,用于后面安装宝塔面板。
双击打开Oracle VM VirtualBox,找到运行的虚拟机,打开设置->共享文件夹,将默认的共享文件夹修改为G:\soft\docker,勾选自动挂载:
win安装docker并部署CentOS 宝塔环境 - 图4
设置完成后重启虚拟机。
重新双击打开Docker Quickstart Terminal,通过以下命令进入虚拟机:

  1. docker-machine ssh default

切换root用户:

  1. sudo -i

查看磁盘挂载情况:

  1. df -h

win安装docker并部署CentOS 宝塔环境 - 图5
exit退回到Docker Quickstart Terminal

运行centos7容器

  1. docker run -dit --privileged=true -p 8888:8888 -p 888:888 -p 21:21 -p 3306:3306 -p 80:80 -v /docker/www:/www --name=centos7 daocloud.io/centos:7 /usr/sbin/init

-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
–name :为容器指定一个名称
-v: 绑定本地文件夹。/docker/www对应的是宿主机的G:/soft/docker/www目录 /www是容器的目录
如果提示G:\soft\Docker Toolbox\docker.exe: Error response from daemon: cgroups: cannot f<br />ind cgroup mount destination: unknown.这个错误
解决办法:
1、执行docker-machine ssh进入Linux虚拟机。
2、执行sudo mkdir /sys/fs/cgroup/systemd
3、执行sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd
4、删除容器,重新启动:

  1. docker stop <容器id>
  2. docker rm centos7
  3. docker run -dit --privileged --name=centos7 daocloud.io/centos:7 /usr/sbin/init

win安装docker并部署CentOS 宝塔环境 - 图6

进入centos容器交互界面

  1. docker exec -it centos7 /bin/bash

win安装docker并部署CentOS 宝塔环境 - 图7

安装wget

  1. yum install -y wget

    安装宝塔

  2. yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh

win安装docker并部署CentOS 宝塔环境 - 图8

构建新镜像

将安装好宝塔的容器打包为新的镜像,便于以后使用:
退出容器:

  1. ctrl+D

执行命令:

  1. docker commit -a "hj" -m "centos & bt" centos7 centos7bt

-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
centos7为容器名
centos7bt为新的镜像名
win安装docker并部署CentOS 宝塔环境 - 图9

构建新容器

  1. docker run -dit --privileged -p 8888:8888 -p 888:888 -p 21:21 -p 3306:3306 -p 80:80 --name=centos7bt centos7bt /usr/sbin/init

-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
–name :为容器指定一个名称

访问宝塔面板

http://192.168.99.100:8888
win安装docker并部署CentOS 宝塔环境 - 图10
注:如果使用宝塔面板用极速安装的方式安装软件失败时,就试试用编译安装的方式。

其他常用命令

启动容器

  1. docker start <容器ID>

    更多资料

    docker常用命令:http://www.884358.com/docker-cmds/