Docker部署安装教程

Docker部署安装

首先我们可以去官网进行学习:

Docker官网链接:

/

一、部署安装

1、卸载旧的docker版本

先检查环境,使用纯净的安装环境,卸载原来安装过的docker

代码语言:bash复制
yum remove docker \

                  docker-client \

                  docker-client-latest \

                  docker-common \

                  docker-latest \

                  docker-latest-logrotate \

                  docker-logrotate \

                  docker-engine

2、下载yum安装工具

代码语言:bash复制
 yum install -y yum-utils -y

3、下载docker-ce的仓库文件,方便后面的安装

代码语言:bash复制
yum-config-manager   --add-repo   .repo

4、安装 docker

代码语言:bash复制
yum -y install docker-ce docker-ce-cli containerd.io -y 

docker-ce 核心软件 引擎

docker-ce-cli 客户端工具,给用户使用的客户端工具集client

containerd.io 管理容器运行时的工具。docker相关的组件

5、安装成功的界面

代码语言:bash复制
作为依赖被升级:

  audit.x86\_64 0:2.8.5-4.el7                       audit-libs.x86\_64 0:2.8.5-4.el7

  policycoreutils.x86\_64 0:2.5-34.el7

完毕!

6、启动和开机启动

代码语言:bash复制
[root@localhost yum.repos.d]# systemctl restart docker

[root@localhost yum.repos.d]# systemctl enable docker

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

7、测试 Docker 是否安装正常

代码语言:bash复制
sudo docker run hello-world

二、安装遇到的问题

1、Could not resolve host: vault.centos; Unknown error"正在尝试其它镜像

代码语言:bash复制
[root@localhost yum.repos.d]#  yum install -y yum-utils -y

已加载插件:fastestmirror

libxml2-python-2.9.1-6.el7\_9.6 FAILED                                          MB  00:00:03 ETA

.9.2009/updates/x86\_64/Packages/libxml2-python-2.9.1-6.el7\_9.6.x86\_64.rpm: [Errno 14] curl#6 - "Could not resolve host: vault.centos; Unknown error"

正在尝试其它镜像。

(4/5): python-chardet-2.2.1-3.el7.noarch.rpm                             | 227 kB  00:00:44

Error downloading packages:

  libxml2-python-2.9.1-6.el7\_9.6.x86\_64: [Errno 256] No more mirrors to try.

问题解决:加域名

代码语言:bash复制
less /etc/resolv.conf

# Generated by NetworkManager

nameserver 114.114.114.114

nameserver 8.8.8.8

nameserver 211.155.23.88

nameserver 211.155.27.88

2、"Failed to connect to 2a03:2880:f12d:83:face:b00c:0:25de: 网络不可达"

安装仓库源还会遇到问题,因为是docker源是国外的,所以会出现的问题:

代码语言:bash复制
[root@localhost yum.repos.d]# yum-config-manager   --add-repo   .repo

已加载插件:fastestmirror

adding repo from: .repo

grabbing file .repo to /etc/yum.repos.d/docker-ce.repo

Could not fetch/save url .repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl#7 - "Failed to connect to 2a03:2880:f12d:83:face:b00c:0:25de: 网络不可达"

解决办法:安装国内的阿里源

代码语言:bash复制
[root@localhost yum.repos.d]# yum-config-manager --add-repo   .repo

已加载插件:fastestmirror

adding repo from: .repo

grabbing file .repo to /etc/yum.repos.d/docker-ce.repo

repo saved to /etc/yum.repos.d/docker-ce.repo

3、"Could not resolve host: mirrorlist.centos; 未知的错误"

这个报错是说我们无法从线上镜像获取东西,

代码语言:bash复制
[root@localhost ~]# yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

Could not retrieve mirrorlist /?release=7&arch=x86\_64&repo=os&infra=stock error was

14: curl#6 - "Could not resolve host: mirrorlist.centos; 未知的错误"

 One of the configured repositories failed (未知),

 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=<repoid> ...

     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 <repoid>

        or

            subscription-manager repos --disable=<repoid>

     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=<repoid>.skip\_if\_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86\_64

解决办法可以参考:

1、验证yum是否正确安装了:

代码语言:bash复制
yum --help

2、验证配置resolv.conf是否配置

在resolv文件里面添加以下内容

代码语言:bash复制
vi /etc/resolv.conf

3、检查网络配置是否正常

ping一下看看是不是网络链接正常,可以ping百度:ping www.baidu

4、检查有没有配置/etc/resolv.conf

代码语言:bash复制
vi /etc/resolv.conf

如果都没有问题,使用yum update来测试看是否成功。yum update会下载很多依赖库,重新配置yum环境,慢的话需要几分钟时间,耐心等待。

4、出现错误failure_ repodata_repomd.xml from docker-ce-stable_ Errno 256 No more mirrors to try.

出现该错的原因可能是因为设置了yum源导致的,我们可以通过重新配置yum源解决问题。切换到/etc/yum.repos.d目录下,删除到只剩CentOS_Base.Repo这一个文件,可以把其他文件先备份一份保存到本地:

代码语言:bash复制
[root@localhost ~]# cd /etc/yum.repos.d

[root@localhost yum.repos.d]# 

[root@localhost yum.repos.d]# ll

总用量 44

-rw-r--r-- 1 root root 1664 11月 23 2020 CentOS-Base.repo

-rw-r--r-- 1 root root 1309 11月 23 2020 CentOS-CR.repo

-rw-r--r-- 1 root root  649 11月 23 2020 CentOS-Debuginfo.repo

-rw-r--r-- 1 root root  314 11月 23 2020 CentOS-fasttrack.repo

-rw-r--r-- 1 root root  630 11月 23 2020 CentOS-Media.repo

-rw-r--r-- 1 root root 1331 11月 23 2020 CentOS-Sources.repo

-rw-r--r-- 1 root root 8515 11月 23 2020 CentOS-Vault.repo

-rw-r--r-- 1 root root  616 11月 23 2020 CentOS-x86\_64-kernel.repo

-rw-r--r-- 1 root root 1919 5月  28 02:12 docker-ce.repo

下载新的CentOS-Base.repo 到/etc/yum.repos.d/:

代码语言:bash复制
## 根据自己的版本选择相应的命令

  CentOS 5

    wget -O /etc/yum.repos.d/CentOS-Base.repo .repo

    或者

    curl -o /etc/yum.repos.d/CentOS-Base.repo 

  CentOS 6

    wget -O /etc/yum.repos.d/CentOS-Base.repo .repo

    或者

    curl -o /etc/yum.repos.d/CentOS-Base.repo .repo

  CentOS 7

    wget -O /etc/yum.repos.d/CentOS-Base.repo .repo

    或者

    curl -o /etc/yum.repos.d/CentOS-Base.repo .repo

5、 问题解决之后 清除yum 更新缓存

清除yum

代码语言:bash复制
[root@localhost yum.repos.d]# yum clean all

已加载插件:fastestmirror

正在清理软件源: base extras updates

Cleaning up list of fastest mirrors

更新缓存

代码语言:bash复制
[root@localhost yum.repos.d]# yum makecache

已加载插件:fastestmirror

Determining fastest mirrors

base                                                                     | 3.6 kB  00:00:00

extras                                                                   | 2.9 kB  00:00:00

updates                                                                  | 2.9 kB  00:00:00

(1/10): base/x86\_64/group\_gz                                             | 153 kB  00:00:10

(2/10): base/x86\_64/primary\_db                                           | 6.1 MB  00:00:05

(3/10): base/x86\_64/other\_db                                             | 2.6 MB  00:00:02

(4/10): extras/x86\_64/primary\_db                                         | 253 kB  00:00:03

(5/10): extras/x86\_64/other\_db                                           | 154 kB  00:00:00

(6/10): extras/x86\_64/filelists\_db                                       | 305 kB  00:00:04

(7/10): base/x86\_64/filelists\_db                                         | 7.2 MB  00:00:22

(8/10): updates/x86\_64/filelists\_db                                      |  15 MB  00:00:19

(9/10): updates/x86\_64/other\_db                                          | 1.6 MB  00:00:02

(10/10): updates/x86\_64/primary\_db                                       |  27 MB  00:00:23

元数据缓存已建立

三、docker里面的概念

1、镜像image

就是软件单元:里面包含app软件代码,微型的操作系统,相关的库 ---》镜像理解为软件也可以。

镜像是人制作出来的,里面的程序的源代码和其他的环境。

2、镜像仓库

image repository --集中存放镜像的地方

存放镜像的网站 :github docker hub

3、容器:container

容器就是把我们的镜像放在里面运行。

例如:hub.docker Hub

DaoCloud 国内的道云公司提供的镜像下载的地方。

软件安装的方式:docker起运行某个软件的容器,颠覆了传统的软件的安装的方式。

四、拉取镜像

1、启动一个nginx的容器

查看本机里有哪些docker 镜像

代码语言:bash复制
[root@localhost yum.repos.d]# docker images

REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

① 需要去官方hub.docker 镜像仓库里面拉一个nginx镜像

docker pull <仓库>

代码语言:bash复制
docker pull nginx 不指定版本 默认下载最新版本 名字为latest

也可以指定版本:

代码语言:bash复制
docker pull nginx:1.21.6

② 启动nginx容器

代码语言:bash复制
docker run --name sc-nginx-1 -p 8090:80 -d  nginx:latest
代码语言:bash复制
> docker run 是启动容器的命令

> --name 是指定启动的容器的名字,可以自定义

> -p 8090:80 端口映射,访问本机的8090端口,会通过nat映射到容器里的80端口,nat功能是靠我们的linux内核完成的 port端口

> -d 将容器在后台启动运行,deamon 守护进程 

> nginx:latest 使用的具体的镜像和版本

也可以启动多个,名字需要改一下

代码语言:bash复制
docker run --name sc-nginx-2 -p 8091:80 -d nginx:latest

③ 查看容器进程

代码语言:bash复制
docker ps  

2、去真实机器里面访问nginx服务

浏览器中输入本机ip+端口:https://192.168.2.130:8090 可以直接访问到容器里面的nginx

以上步骤就说明安装成功了且成功的拉取了nginx镜像。

五、到别的镜像仓库里下载镜像

代码语言:bash复制
docker pull daocloud.io/library/golang:1.7.6-alpine3.5