Docker学习笔记第一篇--安装

今天开始要真正全面系统学习Docker了 💪

学习资源依然为慕课网实战课程,购买链接:系统学习Docker 践行DevOps理念

我们学习Docker之前,先了解下什么是容器呢?

image-20181019160216227

我们从上图左侧可以看到 在一个物理主机上我们通过共享Kernel创建不同的容器,在不同的容器中运行不同的应用。

容器是APP层面的隔离,虚拟化是物理资源层面的隔离。

官方下载网站:https://docs.docker.com/install/

在Mac上安装Docker

安装很简单只需要按照官方文档来就可以了:https://docs.docker.com/docker-for-mac/install/

安装成功后我们可以在命令行下查看安装版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
➜  ~ docker --version
Docker version 18.06.1-ce, build e68fc7a

➜ ~ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:21:31 2018
OS/Arch: darwin/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:29:02 2018
OS/Arch: linux/amd64
Experimental: true

我们通过这种方式安装 直接包含了很多工具:

What the install includes: The installation provides Docker Engine, Docker CLI client, Docker Compose, >Docker Machine, and Kitematic.

其中的Kitematic是一个图形化管理工具,我们可以通过点击上面链接直接下载。

安装使用 Vagrant 、VirtualBox

Vagrant 安装参考文件:https://www.vagrantup.com/downloads.html

VirtualBox 安装参考文件:https://www.virtualbox.org/wiki/Downloads

VirtualBox是一个虚拟机管理工具,我们可以只用VirtualBox直接创建管理虚拟机。

Vagrant是一款更加方便的虚拟机管理工具。

安装成功之后,我们可以在命令行模式下进行验证:

1
2
3
4
5
6
➜  centos vagrant --help
WARNING: This command has been deprecated in favor of `vagrant cloud auth login`
Usage: vagrant [options] <command> [<args>]

-v, --version Print the version and exit.
-h, --help Print this help.

我们新建一个空目录centos,然后执行初始化虚拟机命令:

1
2
3
4
5
➜  centos vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

执行完上面命令之后会生成一个Vagrantfile文件,这个文件描述了我们将要创建一个什么样的虚拟机。

1
2
3
4
➜  centos cat Vagrantfile | grep -v '#'
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
end

文件内容描述出我们要安装的是一个centos7的虚拟机。

下面我们执行真正的安装命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  centos vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1809.01) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1809.01/providers/virtualbox.box
==> default: Box download is resuming from prior download progress
default: Download redirected to host: cloud.centos.org
==> default: Successfully added box 'centos/7' (v1809.01) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos_default_1540002362574_56438

up命令会检查本地是够存在Box,如果存在就使用本地的创建,如果不存在就从远程下载创建。

从上面我们看到创建了一个名为centos_default_1540002362574_56438的虚拟机,我们可以使用VirtualBox查看一下

image-20181020102811726

会发现已经的确存在一个名为centos_default_1540002362574_56438正在运行。

我们主机已经创建好,那怎么使用呢?

1
2
3
➜  centos  vagrant ssh
[vagrant@localhost ~]$ sudo yum update
[vagrant@localhost ~]$ exit

我们可以使用vagrant ssh进入虚拟机,执行exit退出虚拟机。

1
2
3
4
5
6
7
8
9
➜  centos vagrant status
Current machine states:

default running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

我们可以使用vagrant status查看虚拟机的状态,上面展示了更多的命令。

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
# 暂停主机 可以使用 up 唤醒
➜ centos vagrant halt
==> default: Attempting graceful shutdown of VM...

# 查看主机状态为 poweroff
➜ centos vagrant status
Current machine states:

default poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

# 删除主机
➜ centos vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

# 再次查看存在主机有哪些
➜ centos vagrant status
Current machine states:

default not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.

上面的一系列操作都是基于Vagrantfile的,我们可以根据不同的Vagrantfile创建不同的虚拟主机。

可以在官方查找到对应的Vagrantfilehttps://app.vagrantup.com/centos/boxes/7

Centos安装Docker

同样安装官方文档安装 :https://docs.docker.com/install/linux/docker-ce/centos/

删除旧版本:

1
2
3
4
5
6
7
8
9
10
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine

Install required packages

1
2
3
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

下载repo

1
2
3
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安装docker

1
$ sudo yum install docker-ce

启动docker服务

1
$ sudo systemctl start docker

查看docker版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@hongshaorou ~]# docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:03 2018
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:25:29 2018
OS/Arch: linux/amd64
Experimental: false

运行一个测试docker

1
2
3
[root@hongshaorou ~]# sudo docker run hello-world
For more examples and ideas, visit:
https://docs.docker.com/get-started/

上面就是一个完整的安装步骤。

Docker Machine的本地使用

什么是Docker Machine呢?

我们看下文档:https://docs.docker.com/machine/overview/

image-20181020160950591

之前我们在Mac上安装Docker的时候 Docker Machine也是同步安装好的。

1
2
➜  docker_learn docker-machine version
docker-machine version 0.15.0, build b48dc28d

使用 Docker Machine可以直接创建一个安装好了Docker的虚拟机。

1
2
3
4
5
6
7
8
9
➜  docker_learn docker-machine create demo
Creating CA: /Users/yanglianzeng/.docker/machine/certs/ca.pem
Creating client certificate: /Users/yanglianzeng/.docker/machine/certs/cert.pem
Running pre-create checks...
(demo) Image cache directory does not exist, creating it at /Users/yanglianzeng/.docker/machine/cache...
(demo) No default Boot2Docker ISO found locally, downloading the latest release...

Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env demo

Boot2Docker ISO是一个安装好DockerISO文件,就是根据这个ISO去创建虚拟机的。

如果你直接执行上面的命令,会等好久。。。

IOS文件下载好放到/Users/xxx/.docker/machine/cache目录下再次执行上面的命令就很快了。

Vagrant一样我们可以查看创建了哪些虚拟机。

1
2
3
➜  docker_learn docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
demo - virtualbox Running tcp://192.168.99.100:2376 v18.06.1-ce

同样我们可以在VirtualBox看到有一个名为demo的虚拟机

image-20181020164818102

如何进去新建的虚拟机呢?

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_learn docker-machine ssh demo
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e - Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a

# 查看是否安装了docker
docker@demo:~$ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:20:43 2018
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:28:38 2018
OS/Arch: linux/amd64
Experimental: false

# 退出
docker@demo:~$ exit

我们看下Docker Machine有哪些命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
mount Mount or unmount a directory from a machine with SSHFS.
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

下面我们学习下Docker Machine的其他知识

当我们在本机运行Docker version的时候

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜  docker_learn docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:21:31 2018
OS/Arch: darwin/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:29:02 2018
OS/Arch: linux/amd64
Experimental: true

我们看到本地的ClientServer对应的系统版本号。

当我们退出本地Docker服务的时候再次看下

1
2
3
4
5
6
7
8
9
10
➜  docker_learn docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:21:31 2018
OS/Arch: darwin/amd64
Experimental: false
Error response from daemon: Bad response from Docker engine

因为我们把本地的Docker服务停掉,因此连接不上本地的Server端。

现在我们把服务停掉之后无法连接服务端了,但是我们可以使用刚才使用Docker Machine创建的虚拟机作为服务端。

我们登上demo看下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
➜  docker_learn docker-machine ssh demo

docker@demo:~$ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:20:43 2018
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:28:38 2018
OS/Arch: linux/amd64
Experimental: false

demo环境变量拷到本地

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

# 拷贝环境变量到本地
➜ docker_learn docker-machine env demo
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/yanglianzeng/.docker/machine/machines/demo"
export DOCKER_MACHINE_NAME="demo"
# Run this command to configure your shell:
# eval $(docker-machine env demo)

# 执行设置变量
➜ docker_learn eval $(docker-machine env demo)

# 查看版本
➜ docker_learn docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:21:31 2018
OS/Arch: darwin/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:28:38 2018
OS/Arch: linux/amd64
Experimental: false

这样,我们可以通过Docker Machine在本地只装一个客户端,连接到远程的服务端就行了。

我们也可以在本地直接在远程 主机上创建一个安装好Docker的机器。

如果想要在云端创建就需要各种driver了,参考官方文档:https://docs.docker.com/machine/get-started-cloud/#examples

可以在这个网站直接体验Dockerhttps://labs.play-with-docker.com/

知识就是财富
如果您觉得文章对您有帮助, 欢迎请我喝杯水!