day02-运行第一个容器
一、CentOS Docker安装
1.1、环境选择:容器需要管理工具(interface)、runtime(JVM)和操作系统。
- 管理工具 - Docker Engine:Docker使用最广泛;
- runtime - runc:Docker的默认runtime;
- 操作系统:Docker支持64位CentOS 7及以上版本(查看CentOS版本:cat /ect/redhat-release),CentOS并非容器OS。
1.2、安装Docker
安装过程需要访问互联网,所有虚拟机必须能够上网。Docker分为开源免费的CE(Community Edition)版本和免费的EE(Enterprise Edition)版本。
手动安装:
1.2.1、卸载旧版本
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
1.2.2、安装Docker Engine-Community,使用Docker仓库进行安装
在新主机上首次安装Docker Engine-Community之前,需要设置Docker仓库。之后,可以从仓库安装和更新Docker。
安装相关工具类:yun-utils提供了yum-config-manager,并且device mapper存储驱动程序需要device-mapper-persistent-data和lvm2。
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
使用以下命令来设置稳定的仓库:
- 使用官方源地址(国内访问不到docker官方镜像)
- 国内的一些源地址(阿里云):
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装docker:
sudo yum install docker-ce
若执行命令后出现以下异常:
sudo yum install docker-ce 出现以下异常: Loaded plugins: fastestmirror base https://download-stage.docker.com/linux/centos/7/x86_64/stable/repodata/repomd.xml: [Errno 14] curl#35 - "TCP connection reset by peer" Trying other mirror. One of the configured repositories failed (Docker CE Stable - x86_64), and yum doesn't have enough cached data to continue. At this point the only safe thing yum can do is fail. There are a few ways to work "fix" this: 1. Contact the upstream for the repository and get them to fix the problem. 2. Reconfigure the baseurl/etc. for the repository, to point to a working upstream. This is most often useful if you are using a newer distribution release than is supported by the repository (and the packages for the previous distribution release still work). 3. Run the command with the repository temporarily disabled yum --disablerepo=docker-ce-stable ... 4. Disable the repository permanently, so yum won't use it by default. Yum will then just ignore the repository until you permanently enable it again or use --enablerepo for temporary usage: yum-config-manager --disable docker-ce-stable or subscription-manager repos --disable=docker-ce-stable 5. Configure the failing repository to be skipped, if it is unavailable. Note that yum will try to contact the repo. when it runs most commands, so will have to try and fail each time (and thus. yum will be be much slower). If it is a very temporary problem though, this is often a nice compromise: yum-config-manager --save --setopt=docker-ce-stable.skip_if_unavailable=true failure: repodata/repomd.xml from docker-ce-stable: [Errno 256] No more mirrors to try. https://download-stage.docker.com/linux/centos/7/x86_64/stable/repodata/repomd.xml: [Errno 14] curl#35 - "TCP connection reset by peer"
在命令行中执行报错提示命令:
yum-config-manager --save --setopt=docker-ce-stable.skip_if_unavailable=true
再次执行安装docker命令,若出现Complete!则安装成功。
sudo yum install docker-ce
验证docker安装是否成功:
启动docker: sudo systemctl start docker 验证docker: sudo docker run hello-world 则会出现以下异常: Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 9bb5a5d4561a: Pulling fs layer docker: error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/e3/e38bc07ac18e See 'docker run --help'.
此错误也是网络问题:国内无法访问dockerhub。使用vi修改 /etc/docker/daemon.json文件。
vi /etc/docker/daemon.json { "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"] }
重启Docker:配置完之后执行以下的命令,以使docker的配置文件生效
systemctl daemon-reload
systemctl restart docker
测试配置结果:
1、busybox简介
我们可以通过从镜像仓库中拉取镜像的方式来测试镜像地址是否配置成功,比如说我们可以尝试去拉取一个简单的busybox镜像来进行测试。
busybox是一个集成了一百多个最常用linux命令和工具的软件,同时它也是一个最小的linux系统,它提供该系统的主要功能,例如grep、find、mount以及telnet等但不包括一些与GUN相关的功能和选项。
2、拉取busybox
docker pull busybox
若出现以下内容,表示拉取成功
Using default tag: latest Trying to pull repository docker.io/library/busybox … latest: Pulling from docker.io/library/busybox 9e87eff13613: Pull complete Digest:
3、测试拉取的busybox镜像
[root@0001 ~]# docker run busybox echo “hello world” “hello world”
测试成功!
2、运行第一个容器
2.1、从Docker Hub下载镜像httpd镜像。镜像中已经安装好了Apache HTTP Serve。
2.2、启动httpd容器,并将容器的80端口映射到host(服务器)的80端口。
docker run -d -p 80:80 httpd
2.3、通过浏览器验证容器是否正常工作。在浏览器中输入:http://your ubuntu host ip

成功!
以下属于个人配置:

注:59.80.22.151:公网ip。88公网88端口映射到私网 88端口
配置1:公网ip的安全组“入规则”要打开tcp协议的80端口
配置2:nat网管公网59.80.22.151的88端口映射到私网 88端口。
-----------------------------------------------------------------------------------
参考:
https://mp.weixin.qq.com/s/mpcQhMyjtVzYjSmK5tyA5w
https://www.runoob.com/docker/centos-docker-install.html
https://blog.csdn.net/qq_37674858/article/details/80182336
https://blog.csdn.net/doegoo/article/details/80062132
https://blog.csdn.net/m0_38039437/article/details/99427784
------------------------------------------------------------------------------------
------------------------------------------------我是结束线--------------------------------------------------

浙公网安备 33010602011771号