2.Docker的安装

 

在CentOS 7上安装Docker。

1. 前提条件

Docker支持以下的CentOS版本:

  • CentOS 7 (64-bit)
  • CentOS 6.5 (64-bit) 或更高的版本

目前,CentOS 仅发行版本中的内核支持 Docker。

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

2. yum安装gcc相关

1
2
# yum -y install gcc
# yum -y install gcc-c++

3. 卸载旧版本

1
2
3
4
5
6
7
8
9
# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

4. 安装Docker

1
# yum install docker

5. 启动Docker

1
# systemctl start docker

开机自启:

1
# systemctl enable docker 

6. 查看版本

1
# docker version

7.1 测试运行hello-world

由于本地没有hello-world这个镜像,所以会下载一个hello-world的镜像,并在容器内运行。

1
# docker run hello-world
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
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
0e03bdcc26d7: Already exists
Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
Status: Downloaded newer image for docker.io/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.
(amd64)
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://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

7.2 测试运行nginx

将80端口映射为8080,或者80:80还是原先的80端口,不可以不写。

1
# docker run -p 8080:80 -d docker.io/nginx 
1
2
3
4
5
6
7
8
9
10
11
Unable to find image 'docker.io/nginx:latest' locally
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
d121f8d1c412: Already exists
ebd81fc8c071: Already exists
655316c160af: Already exists
d15953c0e0f8: Already exists
2ee525c5c3cc: Already exists
Digest: sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Status: Downloaded newer image for docker.io/nginx:latest
b666386fc45b5668e8863038d573f6593b3a3ccf7b3568741982907096e0a77b

测试:
http://主机ip:8080/

8. 卸载Docker

1
2
3
# systemctl stop docker
# yum -y remover docker
# rm -rf /var/lib/docker