docker 容器入门(一)

写在前面的话

每篇一句

岁寒,然后知松柏之后雕也。

社区版容器安装,运行第一个容器

环境介绍

os 内核版本不低于3,注意操作系统要是主要维护版本,没有EOL。
我常见到的容器所运行的os环境主要是Ubuntu 及Centos,下面以centos7为例。

1
2
3
4
[root@localhost yum.repos.d]# uname -a
Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost yum.repos.d]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

镜像源配置

使用国内的镜像源可以极大加速软件安装速度和成功率。

配置操作系统基本软件和扩展软件源

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
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls -l
总用量 36
-rw-r--r--. 1 root root 1664 11月 30 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 11月 30 2016 CentOS-CR.repo
-rw-r--r--. 1 root root 649 11月 30 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 11月 30 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 11月 30 2016 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 11月 30 2016 CentOS-Sources.repo
-rw-r--r--. 1 root root 2893 11月 30 2016 CentOS-Vault.repo
-rw-r--r-- 1 root root 957 12月 28 2016 epel.repo
-rw-r--r-- 1 root root 1056 12月 28 2016 epel-testing.repo
[root@localhost yum.repos.d]# mkdir repobak
[root@localhost yum.repos.d]# mv *.repo repobak/
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: base epel extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: (28, 'Connection timed out after 30001 milliseconds')
正在尝试其它镜像。
base | 3.6 kB 00:00
epel | 4.3 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
Determining fastest mirrors
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
元数据缓存已建立

里面http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on 原因是使用阿里云服务器,会优先从mirrors.aliyuncs.com,不占用云服务器公网流量,要知道按量的模式可是得收费的。而我是笔记本电脑上的一台虚拟机,最终访问出去用的是所在电信的一个公网ip,当然访问不到阿里内网。

配置社区版容器源

1
2
3
4
5
[root@localhost yum.repos.d]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

老版本容器卸载(假设你之前没安装过容器,可跳过此步骤)

1
2
3
4
5
6
7
[root@localhost yum.repos.d]# yum remove docker docker-common docker-selinux docker-engine
已加载插件:fastestmirror, langpacks
参数 docker 没有匹配
参数 docker-common 没有匹配
参数 docker-selinux 没有匹配
参数 docker-engine 没有匹配
不删除任何软件包

安装所依赖软件

yum-utils提供 yum-config-manager管理能力, device-mapper-persistent-data和lvm2是存储驱动。

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

社区版容器安装

默认安装最近最新版本。

1
yum install docker-ce -y

特定版本安装
列出有哪些版本

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost yum.repos.d]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror, langpacks
可安装的软件包
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
* extras: mirrors.aliyun.com
* epel: mirrors.aliyun.com
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable

下面以安装17.06.1版本为例

1
[root@localhost yum.repos.d]# yum install docker-ce-17.06.1.ce

运行第一个容器

尝试运行第一个容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost yum.repos.d]# systemctl start docker
[root@localhost yum.repos.d]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2017-10-22 14:14:07 CST; 8s ago
Docs: https://docs.docker.com
Main PID: 7963 (dockerd)
Memory: 16.2M
CGroup: /system.slice/docker.service
├─7963 /usr/bin/dockerd
└─7969 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-inte...
10月 22 14:14:07 localhost.localdomain dockerd[7963]: time="2017-10-22T14:14:07.858203582+08:00" level=info ...ock"
10月 22 14:14:07 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost yum.repos.d]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%3Apull&service=registry.docker.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

配置容器国内源

运行第一个容器hello-world,本地没有容器镜像,默认从国外docker公司拉取镜像,拉取超时失败。
所以我们配置国内源,下面以DaoCloud为例,得先注册账号。

1
2
3
4
5
6
[root@localhost yum.repos.d]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://8b3fddac.m.daocloud.io
docker version >= 1.12
{"registry-mirrors": ["http://8b3fddac.m.daocloud.io"]}
Success.
You need to restart docker to take effect: sudo systemctl restart docker
[root@localhost yum.repos.d]# systemctl restart docker

其实就是配置了下面这个json 配置文件,重启docker 服务后生效。

1
2
[root@localhost yum.repos.d]# cat /etc/docker/daemon.json
{"registry-mirrors": ["http://8b3fddac.m.daocloud.io"]}

重新运行第一个容器

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
[root@localhost yum.repos.d]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
5b0f327be733: Pull complete
Digest: sha256:a69dda95faa021234ab2d92118e9539d0175b46587803e32435ea2e534f2db06
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

至此第一个容器就运行起来了,如果你的操作系统是其他版本,可以参考容器官网文档

----纸上得来终觉浅绝知此事要躬行----
最好的赞赏是您的阅读!
0%