在 CentOS 7 安装 Docker CE(Community Edition)

目录

1.1. 环境说明

1.2. 配置 yum 仓库源

1.3. 安装并启动 Docker 引擎

1.4. 配置国内 docker 仓库(镜像加速)

1.5. 验证 Docker 引擎是否安装成功

1.6. Python 镜像使用示例

1.6.1. 下载 python 镜像

1.6.2. 运行 python 镜像

1.6.3. 保存 python 镜像到本地

1.6.4. 删除 python 镜像

1.6.5. 加载本地 python 镜像文件

1.6.6. 查看镜像列表

1.7. Ubuntu 镜像使用示例

1.7.1. 下载 ubuntu 镜像

1.7.2. 运行 ubuntu 镜像

1.7.3. 保存 ubuntu 镜像到本地

 1.7.4. 删除 ubuntu 镜像

1.7.5. 加载本地 ubuntu 镜像文件

1.8. httpd 2.4 镜像使用示例

1.8.1. 开启网络转发功能

1.8.2. 下载 httpd 2.2 镜像

1.8.3. 运行 httpd 2.2 镜像

 1.8.4. 保存 httpd 2.2 镜像到本地

1.8.5. 删除 httpd 2.2 镜像

1.8.6. 加载本地 httpd 2.2 镜像文件

1.8.7. 运行 httpd 2.2 (挂载目录到容器中)

1.9. 构建自己的镜像(基于 httpd 2.2)

1.9.1. 创建 Dockerfile 文件

1.9.2. docker build 构建镜像

1.9.3. 基于新镜像 myhttpd 运行容器

1.9.4. 测试新容器运行情况

1.10. 离线安装docker引擎

1.10.1. 更新软件包

1.10.2. 安装软件包


1.1. 环境说明

System Version: CentOS Linux release 7.2.1511 (Core)
Docker Version: Docker Community Edition Latest version

1.2. 配置 yum 仓库源

# yum-config-manager \
 --add-repo \
 https://download.docker.com/linux/centos/docker-ce.repo

1.3. 安装并启动 Docker 引擎

1. 安装最新版本的 Docker 引擎和 containerd 。安装如下包:
# yum install docker-ce docker-ce-cli containerd.io
如果提示接受 GPG key ,验证特征是否匹配,如果是 060A 61C5 1B55 8A7F 742B 77AA C52F
EB6B 621E 9F35 ,选择接受。
上面的命令安装 Docker ,但并不启动它。安装的同时会创建 docker 组。但并没有添加任何用户到
docker 组。需要后期手动添加。
2. 暂时禁用和停止防火墙服务。
在本 CentOS 系统启动 docker 服务时会遇到防火墙命令失败的情况,下面先停止防火墙服务,并
暂时禁用该服务。
systemctl stop firewalld 
systemctl disable firewalld
3. 启动 Docker 服务
# systemctl start docker

1.4. 配置国内 docker 仓库(镜像加速)

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。 Docker 官方和国内很多云服
务商都提供了国内加速器服务,例如:
科大镜像: https://docker.mirrors.ustc.edu.cn/
网易: https://hub-mirror.c.163.com/
七牛云加速器: https://reg-mirror.qiniu.com
百度: https://mirror.baidubce.com
/etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)。
[root@localhost ~]# cat /etc/docker/daemon.json 
{ 
"registry-mirrors": [ 
"https://hub-mirror.c.163.com", 
"https://mirror.baidubce.com" 
 ] 
}

1.5. 验证 Docker 引擎是否安装成功

通过运行 hello-world 镜像,验证 Docker 引擎是否安装成功。
# docker run hello-world
上面的命令会从官方 docker hub 下载 hello-world 镜像并启动容器。容器运行成功,会打印消息并退出。

1.6. Python 镜像使用示例

1.6.1. 下载 python 镜像

下面的命令会从 docker hub 镜像下载 python 镜像。
# docker pull python

1.6.2. 运行 python 镜像

# docker -it python bash

执行 exit 命令可以退出并停止容器。

1.6.3. 保存 python 镜像到本地

从官网下载镜像后,使用如下命令将镜像保存到本地,即可在本地使用镜像包测试。
# docker save -o python.tar python

1.6.4. 删除 python 镜像

# docer rmi pythoh

1.6.5. 加载本地 python 镜像文件

# docker load < python.tar

1.6.6. 查看镜像列表

# docker image ls

 

1.7. Ubuntu 镜像使用示例

1.7.1. 下载 ubuntu 镜像

# docker pull ubuntu
上面的命令会从 Docker Hub 下载 ubuntu 镜像到本地。

1.7.2. 运行 ubuntu 镜像

# docker run -it ubuntu bash

 

进入 ubuntu 系统后,执行 exit 命令退出容器,并停止容器

1.7.3. 保存 ubuntu 镜像到本地

[root@localhost ~]# docker save -o ubuntu.tar ubuntu 
[root@localhost ~]# ls 
anaconda-ks.cfg Desktop Documents Downloads Music Pictures Public python.tar Templates ubuntu.tar Videos 
[root@localhost ~]# ll 
total 990132 -rw-------. 1 root root 2754 Feb 27 2019 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Feb 27 2019 Desktop drwxr-xr-x. 2 root root 6 Feb 27 2019 Documents drwxr-xr-x. 2 root root 6 Feb 27 2019 Downloads drwxr-xr-x. 2 root root 6 Feb 27 2019 Music drwxr-xr-x. 2 root root 6 Feb 27 2019 Pictures drwxr-xr-x. 2 root root 6 Feb 27 2019 Public -rw-------. 1 root root 938725376 Nov 22 01:49 python.tar drwxr-xr-x. 2 root root 6 Feb 27 2019 Templates -rw-------. 1 root root 75164672 Nov 22 06:27 ubuntu.tar drwxr-xr-x. 2 root root 6 Feb 27 2019 Videos

 1.7.4. 删除 ubuntu 镜像

[root@localhost ~]# docker rmi ubuntu
如果删除镜像时提示该镜像被容器使用,请先使用 docker container rm xxx 命令删除相应容器。

1.7.5. 加载本地 ubuntu 镜像文件

测试刚才保存的镜像文件是否可用。
[root@localhost ~]# ls 
anaconda-ks.cfg Documents Music Public Templates Videos Desktop Downloads Pictures python.tar ubuntu.tar 
[root@localhost ~]# docker load < ubuntu.tar 
9f54eef41275: Loading layer 75.16MB/75.16MB Loaded image: ubuntu:latest 
[root@localhost ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE python latest f48ea80eae5a 5 days ago 917MB ubuntu latest ba6acccedd29 5 weeks ago 72.8MB hello-world latest feb5d9fea6a5 8 weeks ago 13.3kB

1.8. httpd 2.4 镜像使用示例

1.8.1. 开启网络转发功能

/etc/sysctl.conf 文件中添加如下行:
net.ipv4.ip_forward=1
运行下面的命令重启网络服务:
systemctl restart network

1.8.2. 下载 httpd 2.2 镜像

[root@localhost ~]# docker pull httpd:2.2

1.8.3. 运行 httpd 2.2 镜像

[root@localhost ~]# docker run -itd -p 8009:80 --name web httpd:2.2
参数说明:
-i 交互
-t 控制台
-d 后台运行
-p 本机端口 : 容器端口
--name docker 容器起的名称
-v 本机文件目录 : 容器中的文件目录(挂载目录到容器中)
如果访问容器的 httpd 服务报如下错误:
[root@localhost ~]# curl 127.0.0.1:8009 
curl: (56) Recv failure: Connection reset by peer
按照如下步骤重置网卡:
# 停止 docker
systemctl stop docker
#docker0
ip link set dev docker0 down
# 删除 docker0 网桥
brctl delbr docker0
# 增加 docker0 网桥
brctl addbr docker0
# 增加网卡
ip addr add 172.16.10.1/24 dev docker0
# 启用网卡
ip link set dev docker0 up
# 重启 docker 服务
systemctl restart docker

重新启动httpd容器,并测试 http 服务:

[root@localhost ~]# docker container list -a 


[root@localhost ~]# docker container restart e28 
e28 
[root@localhost ~]# docker container list -a 

[root@localhost ~]# curl 127.0.0.1:8009 
<html><body><h1>It works!</h1></body></html>

 1.8.4. 保存 httpd 2.2 镜像到本地

[root@localhost ~]# docker save -o httpd2.2.tar httpd:2.2 
[root@localhost ~]# ls 
anaconda-ks.cfg Documents httpd2.2.tar Pictures python.tar ubuntu.tar 
website 
Desktop Downloads Music Public Templates Videos

1.8.5. 删除 httpd 2.2 镜像

[root@localhost ~]# docker rmi httpd:2.2
[root@localhost ~]# docker images

1.8.6. 加载本地 httpd 2.2 镜像文件

[root@localhost ~]# docker load < httpd2.2.tar
[root@localhost ~]# docker images

1.8.7. 运行 httpd 2.2 (挂载目录到容器中)

# 运行 httpd 2.2,并挂载本地网页程序目录到容器 
[root@localhost website]# docker run -itd -p 8008:80 --name mysite -v /root/website/:/usr/local/apache2/htdocs/ httpd:2.2
#--name自己定义

# 查看容器中的进程状态 
[root@localhost website]# docker exec mysite ps -ef | grep httpd

# 查看容器中的端口链接状态 
[root@localhost website]# docker exec mysite ss -antup | grep 80

# 测试 web 服务器 
[root@localhost website]# curl localhost:8008v

1.9. 构建自己的镜像(基于 httpd 2.2

1.9.1. 创建 Dockerfile 文件

在当前目录下创建 Dockerfile ,内容如下:
FROM httpd:2.2 
COPY ./website/ /usr/local/apache2/htdocs/
上面的 website 目录在当前路径下,目录中是自己的网页程序文件。

1.9.2. docker build 构建镜像

# 运行如下命令,在当前目录下构建新的镜像:myhttpd 
[root@localhost ~]# docker build -t myhttpd .

# 查看当前镜像列表 
[root@localhost ~]# docker images | grep myhttpd

1.9.3. 基于新镜像 myhttpd 运行容器

[root@localhost ~]# docker run -itd -p 8007:80 --name newsite myhttpd
#--name之前自己设置的名字

1.9.4. 测试新容器运行情况

[root@localhost ~]# docker run -itd -p 8007:80 --name newsite myhttpd
[root@localhost ~]# curl localhost:8007

1.10. 离线安装docker引擎

1.10.1. 更新软件包

下载下图中的 rpm 包,并执行更新操作
# rpm -Uvh *.rpm

 

1.10.2. 安装软件包

安装下图中的软件包:
# rpm -ivh *.rpm

 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 1. 卸载旧版本的Docker 如果您已经安装了旧版本的Docker,需要先卸载旧版本的Docker。 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine 2. 安装必要的软件包 sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 3. 添加Docker的官方GPG密钥 sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo 4. 安装Docker CE sudo yum install docker-ce docker-ce-cli containerd.io 5. 启动Docker sudo systemctl start docker 6. 验证Docker是否安装成功 sudo docker run hello-world 如果您看到了以下输出,则说明Docker已经成功安装并运行: Hello from Docker! This message shows that your installation appears to be working correctly. 7. 设置Docker开机自启 sudo systemctl enable docker 现在,您已经成功地在CentOS 7上安装Docker CE。 ### 回答2: CentOS7是一种常见的Linux操作系统。Docker CE是一种流行的容器化平台。本文将介绍如何在CentOS7上安装Docker CE。 1. 升级和安装必要的软件包 首先,更新系统并安装必要的软件包: sudo yum update sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2. 添加Docker CE的yum软件源 添加Docker CE的yum软件源到系统中: sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 3. 安装Docker CE 运行以下命令以安装最新版本的Docker CE: sudo yum install docker-ce 4. 启动Docker服务 启动Docker服务: sudo systemctl start docker 使用以下命令验证Docker是否已正确安装: sudo docker run hello-world 这将启动一个测试容器并输出一些信息。 5. 设置Docker服务自启动 配置Docker服务在机器启动时自动启动: sudo systemctl enable docker 完成以上步骤后,便成功将Docker CE安装CentOS7上,并可以使用您的容器运行镜像。 注:如果您使用的是具有安全策略和防火墙规则的Linux,需要调整这些规则才能运行Docker和容器。 ### 回答3: CentOS 7是一种流行的Linux操作系统版本,适用于服务器和云计算环境。Docker CE是一个开源的容器平台,可以轻松地打包应用程序和依赖项,并运行在任何地方,而不必担心与操作系统的兼容性问题。在本文中,我们将向您介绍如何在CentOS 7上安装Docker CE。 步骤1: 升级系统 在开始安装之前,我们必须要确保我们使用的是最新的软件包和依赖项。运行以下命令可以升级CentOS 7系统。 sudo yum update -y 步骤2: 安装必备软件包 在安装Docker CE之前,我们必须先安装一些必备的软件包,比如yum-utils,用于管理Docker软件包的特定仓库。 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 步骤3: 添加docker-ce的仓库 Docker CE软件包不在默认的CentOS 7存储库中。因此,我们需要手动添加Docker CE仓库。通过运行以下命令可以添加Docker CE官方仓库到CentOS 7系统中。 sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 步骤4: 安装Docker CE 添加完Docker CE仓库之后,我们可以安装Docker CE了。以下命令可以安装Docker CE。 sudo yum install -y docker-ce 安装完成后,我们可以通过以下命令启动Docker CE服务,并使其在系统启动时自动启动。 sudo systemctl start docker sudo systemctl enable docker 步骤5: 验证Docker CE 安装完成后,我们可以使用以下命令来验证Docker是否成功安装docker version 这会显示Docker CE的版本号和相关信息。 至此,CentOS 7上的Docker CE安装已成功完成。现在您可以开始构建和管理容器了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Goat_1 3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值