how to use docker in windows

2017-08-16 11:06:29

前言
本文运行环境针对Windows 10操作系统,主要解决的痛点是各种平台开发环境的不统一以及多版本共存的问题.主要讲解如何安装docker和使用内网registry

在windows环境下,如何安装docker呢? Docker Toolbox 是一个不错的选择,你可以在Mac或者Windows上像安装其它应用一样来安装它.
下载点击这里

Docker Toolbox 安装

默认安装完成后,安装目录结构如下
boot2docker.iso
docker-compose.exe
docker-machine.exe
docker-quickstart-terminal.ico
docker.exe
installers
kitematic
start.sh
unins000.dat
unins000.exe

未避免因为墙无法下载问题,我们手动将boot2docker.iso文件拷贝到C:\Users\用户名.docker\machine\cache目录下

现在有两种启动方式,一个是图形启动,一个是命令行方式启动.
图形启动的就双击Kitematic
命令行启动就双击docker-quickstart-terminal

备注:非administrator用户,请右键-以管理员身份运行,运行如下

命令行启动如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Running pre-create checks...
Creating machine...
(default) Copying C:\Users\abc\.docker\machine\cache\boot2docker.iso to C:\Users\abc\.docker\machine\machines\default\boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Windows might ask for the permission to create a network adapter. Sometimes, such confirmation window is minimized in the taskbar.
(default) Found a new host-only adapter: "VirtualBox Host-Only Ethernet Adapter #2"
(default) Windows might ask for the permission to configure a network adapter. Sometimes, such confirmation window is minimized in the taskbar.
(default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
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 your Docker Client to the Docker Engine running on this virtual machine, run: C:\Program Files\Docker Toolbox\docker-machine.exe env default



## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/

docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Start interactive shell

到这里我们就安装完了,查看下docker版本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$ docker info
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
Server Version: 17.06.0-ce
Storage Driver: aufs
Root Dir: /mnt/sda1/var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 16
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: cfb82a876ecc11b5ca0977d1733adbe58599088a
runc version: 2d41c047c83e09a6d61d464906feb2a2f3c52aa4
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.4.74-boot2docker
Operating System: Boot2Docker 17.06.0-ce (TCL 7.2); HEAD : 0672754 - Thu Jun 29 00:06:31 UTC 2017
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 995.8MiB
Name: default
ID: G6PB:FV47:YOTF:CIHJ:GNUB:DTML:7GQ2:6GOO:5WIK:EB3J:J4WR:2DVZ
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 41
Goroutines: 50
System Time: 2017-08-14T08:10:46.381858948Z
EventsListeners: 1
Registry: https://index.docker.io/v1/
Labels:
provider=virtualbox
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

图形启动的话,会让我们登录到 Docker Hub。如果我们还没有账户或者还不想登录,可以点击 SKIP FOR NOW 继续后面的步骤.
完成之后,就会出现 Kitematic 应用程序的第一个界面.
在这里,我们会看到hello-world-nginx,如果找不到,可以在搜索区域搜索hello world nginx,我们点击 Create 来部署容器,在镜像下载完成之后,它会自动部署.我们可以在 Kitematic界面上预览web页面

主机与docker主机共享文件夹

假设我们需要共享的work文件夹,我们需要打开Oracle VM VirtualBox,选中“正在运行”状态的 default 虚拟机,进入 设置-> 共享文件夹,添加共享文件夹,选中work文件夹,勾选“自动挂载”、“固定分配”,确定.
重启完成后,通过终端连接docker主机,输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$ docker-machine.exe ssh default
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 17.06.0-ce, build HEAD : 0672754 - Thu Jun 29 00:06:31 UTC 2017
Docker version 17.06.0-ce, build 02c1d87
docker@default:~$ mount
tmpfs on / type tmpfs (rw,relatime,size=917692k)
proc on /proc type proc (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=000)
tmpfs on /dev/shm type tmpfs (rw,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda1 on /mnt/sda1 type ext4 (rw,relatime,data=ordered)
cgroup on /sys/fs/cgroup type tmpfs (rw,relatime,mode=755)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu type cgroup (rw,relatime,cpu)
cgroup on /sys/fs/cgroup/cpuacct type cgroup (rw,relatime,cpuacct)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,relatime,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,relatime,net_cls)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,relatime,perf_event)
cgroup on /sys/fs/cgroup/net_prio type cgroup (rw,relatime,net_prio)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,relatime,hugetlb)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,relatime,pids)
c/Users on /c/Users type vboxsf (rw,nodev,relatime)
work on /work type vboxsf (rw,nodev,relatime)
/dev/sda1 on /mnt/sda1/var/lib/docker/aufs type ext4 (rw,relatime,data=ordered)

可以看到配置共享文件夹work成功.

创建容器

在终端下输入以下命令,表示安装最新版本node的linux系统

1
docker pull node

之后就可以通过使用该镜像生成容器了:

1
2
3
4
#查看镜像
docker images
#生成容器
docker run -d -p 3000:3000 node

定制开发环境

这里有两种方法,一个是编写Dockerfile 发布到版本库,其他人从版本库下载本地编译;另一种是打包代码到镜像,上传到仓库,其他人从仓库下载运行.

假如我们的发布目录结构如下:

1
2
3
4
5
[root@localhost docker-node]# tree
.
├── Dockerfile
├── index.js
└── package.json

第一种方法就不多说了,这里我们来看下第二种方法,比如要发布nodejs应用,首先是要编写Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FROM centos:centos6
MAINTAINER WeiShao Project <tiger@whistle.com.cn>
ENV NODEJS_VERSION=v8.3.0
ENV PATH=/usr/local/node/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin

RUN yum -y update; yum clean all
RUN yum -y install epel-release; yum clean all
RUN yum -y install make gcc gcc-c++ && yum -y clean all
RUN yum -y install xz

RUN mkdir -p /usr/local && cd /usr/local && curl -s -L -O http://cdn.npm.taobao.org/dist/node/${NODEJS_VERSION}/node-${NODEJS_VERSION}-linux-x64.tar.xz && tar xf node-${NODEJS_VERSION}-linux-x64.tar.xz && mv node-${NODEJS_VERSION}-linux-x64 node

RUN npm install -g cnpm --registry=https://registry.npm.taobao.org
RUN cnpm install -g pm2 --registry=https://registry.npm.taobao.org

add . /usr/local/whistle/webapps/src
RUN cd /usr/local/whistle/webapps/src; npm install
EXPOSE 3000

CMD ["pm2-docker", "/usr/local/whistle/webapps/src/index.js"]

我们的index.js和package.json 如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost docker-node]# cat index.js 
var express = require('express');

// Constants
var PORT = 3000;

// App
var app = express();
app.get('/', function (req, res) {
res.send('Hello World from CentOS6 in Docker\n');
});

app.listen(PORT)
console.log('Running on http://localhost:' + PORT);


[root@localhost docker-node]# cat package.json
{
"name": "docker-centos-hello",
"private": true,
"version": "0.0.1",
"description": "Node.js Hello World app on CentOS6 using docker, created from the Node.js example on docker.io",
"author": "Wei Shao <ws@whistle.com.cn>",
"dependencies": {
"express": "3.x"
}
}

现在我们可以build我们的镜像了

1
[root@localhost docker-node]# docker build -t registry.whistle.com.cn/node:8.3.0 .

在等待一会之后就可以看到我们的镜像编译成功了,可以通过docker images查看

1
[root@localhost docker-node]# docker run -d -p 3000:3000 registry.whistle.com.cn/node:8.3.0

现在通过http://192.168.99.100:3000/ 就可以查看页面了.

如何才能让其他同事用这个镜像呢,这里我们要上传到自己的使用仓库里去
因为我们使用的是自签名证书,所以需要设置insecure-registry,执行以下操作

1
2
3
4
5
6
7
8
9
10
11
12
docker-machine ssh default
sudo vi /var/lib/boot2docker/profile

EXTRA_ARGS="
--insecure-registry registry.whistle.com.cn
"
exit
docker-machine restart

or
docker-machine ssh default "sudo sed -i '/EXTRA_ARGS=/a\--insecure-registry registry.whistle.com.cn' /var/lib/boot2docker/profile"
docker-machine restart

这样,我们就可以pull我们制作的node镜像了

1
docker push registry.whistle.com.cn/node:8.3.0

可以在浏览器访问 https://registry.whistle.com.cn/v2/_catalog 查看仓库的镜像列表

其他同事在设置了insecure-registry之后就可以执行以下命令获取相应的镜像了

1
docker pull registry.whistle.com.cn/node:8.3.0

FAQ

  • 问:运行Kitematic时,卡在waiting for an IP的命令下.
    答: 检查运行操作系统,win7下运行可能出现此问题,由于没有发布正式版,不推荐win7下使用. win7 下 Docker Quickstart Terminal可以运行正常
  • 问:如何确认当前docker虚拟机状态
    答: 在命令行里输入 docker-machine ls 查看,如果没有可以通过以下命令建立 docker-machine create –driver=virtualbox default
  • 问: 如何才能自动生成Dockerfile呢
    答: 可以通过starter来自动生成,还是建议学习下Dockerfile的相关基础知识.自动生成步骤如下
    wget https://raw.githubusercontent.com/cloud66/starter/master/install.sh
    chmod +x install.sh && ./install.sh
    cd appdir && starter -g dockerfile,service
    进行简单选择后即可在当前目录生成dockerfile文件
  • 问: 如何查看registry 和images version

    curl https://registry.whistle.com.cn/v2/_catalog

    {“repositories”:[“hello-world”,”jenkins”,”node”,”svn”]}

    curl https://registry.whistle.com.cn/v2/node/tag/list

    {“name”:”node”,”tags”:[“8.3.0”]}

ref
在Windows中玩转Docker Toolbox
allow insecure registry in host provisioned with docker-machine
Tools - Docker Registry
搭建本地私有Docker仓库
Docker私有仓库 Registry中的镜像管理


您的鼓励是我写作最大的动力

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。