文章

docker - 安装使用及问题

docker - 安装使用及问题

docker的安装

参考地址: https://docs.docker.com/engine/install/centos/

正常安装(能访问docker官网的情况)

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
wangfuyu]# yum install -y yum-utils  
wangfuyu]# yum-config-manager --add-repo   https://download.docker.com/linux/centos/docker-ce.repo   
wangfuyu]# yum install docker-ce docker-ce-cli containerd.io  
wangfuyu]# systemctl start docker  
wangfuyu]# docker info  
wangfuyu]# docker version  
Client: Docker Engine - Community
Version:           20.10.8
API version:       1.41
Go version:        go1.16.6
Git commit:        3967b7d
Built:             Fri Jul 30 19:55:49 2021
OS/Arch:           linux/amd64
Context:           default
Experimental:      true

Server: Docker Engine - Community
Engine:
Version:          20.10.8
API version:      1.41 (minimum version 1.12)
Go version:       go1.16.6
Git commit:       75249d8
Built:            Fri Jul 30 19:54:13 2021
OS/Arch:          linux/amd64
Experimental:     false
containerd:
Version:          1.4.9
GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
runc:
Version:          1.0.1
GitCommit:        v1.0.1-0-g4144b63
docker-init:
Version:          0.19.0
GitCommit:        de40ad0

国内源安装

修改源(CentOS-7)

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
vim CentOS-Base.repo 
# 复制粘贴如下代码并保存:
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

然后更新缓存

1
yum makecache && yum -y update

等待更新完毕后即可使用。


还可以试试以下方式

1
2
wangfuyu]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

1
2
3
4
5
6
7
[wangfuyu@wfy ~]$ cat /etc/docker/daemon.json 
{   
    "data-root":"/data/docker",
    "registry-mirrors": [
                "https://71t1c6hh.mirror.aliyuncs.com"
                        ] 
}

daemon.json 是我的阿里云镜像加速

使用时的一些问题

  • 问题1:宿主机访问容器的映射端口不通

    Connection reset by peer

    ~]# curl 127.0.0.1:8088 curl: (56) Recv failure: Connection reset by peer 常出现在宿主机访问容器映射到本地端口不通情况。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    ## 重新更换网桥docker0
    ## 停止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
      
    ## 其他流程正常尝试,发现可以用了。
    
  • 问题2:发现自己指定创建的网络network,不能访问

    No route to host

    ~]# docker container ls

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 890581e2b3f8 daocloud.io/library/nginx:1.7.6 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:8088->80/tcp shop_nginx_8088 ~]# curl 127.0.0.1:8088 curl: (7) Failed connect to 127.0.0.1:8088; No route to host

    1
    2
    
    ## 自定义网络
    docker network create --driver=bridge --subnet=172.18.1.0/24 shop_net
    

    以上可能是由于地址冲突导致,比如我本地使用网段跟默认docker的bridge同一个了。

  • 问题3: 容器中nginx报错

    upstream sent too big header while reading response header from upstream

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    ## 需要修改nginx层的配置
      
    server {
    listen 80;
    server_name blog.360club.net;
    location / {
    proxy_buffer_size 128k;
    proxy_buffers 32 32k;
    proxy_busy_buffers_size 128k;
      
                proxy_set_header Host $server_name;  
                proxy_set_header HTTP_USERIP $remote_addr;  
                proxy_set_header Connection "keep-alive";  
                proxy_cookie_domain 127.0.0.1 $server_name;  
                proxy_pass http://127.0.0.1:8083$request_uri;  
        }  
    }
    
  • 问题4:docker进行build的时候yum失败

    yum update ….cannot connect xxx.com

    1
    2
    3
    4
    5
    
    ~]# docker build -t lnp:5.3 . 
      
    yum update ....cannot connect xxx.com
      
     如果宿主能上网,build时候上不了网,可以重新创建docker0网络 (我是在虚拟机里面安装了个docker,虚拟机用nat可以上网,docker就不行了,估计是笔记本来回切换网络的问题)
    
  • 问题5: 数据卷占满了磁盘空间处理

    尤其是开发服务器或者个人pc,镜像过多,容器过多都会占用磁盘空间。

    首先在另一张大容量磁盘上创建存放位置,比如:/mnt/hgfs/www/devicemapper 或者直接停止容器后,拷贝到这里都可以。
    然后修改配置文件 /etc/systemd/system/docker.service.d/docker.conf 没有该文件则创建即可 .

    [Service] ExecStart= ExecStart=/usr/bin/dockerd –graph=”/mnt/hgfs/www/devicemapper” –storage-driver=devicemapper 保存后,启动 [root@localhost k8s]# systemctl start docker

    Warning: docker.service changed on disk. Run ‘systemctl daemon-reload’ to reload units.

    [root@localhost k8s]# systemctl daemon-reload

    [root@localhost k8s]# systemctl start docker

    !!!结果使用 docker info发现还是没有到新目录。
    删除那个配置文件,换另一方式:直接move数据做软连接吧。

    mv /var/lib/docker /mnt/hgfs/www/var_docker ln -s /mnt/hgfs/www/var_docker/ /var/lib/docker mv /mnt/hgfs/www/devicemapper /mnt/hgfs/www/var_docker/

    如果不管用的话,重启一下机器就好了,

    还可以直接安装docker之前就提前做好目录映射:

    [wangfuyu@wfy ~]$ ll /var/lib/docker lrwxrwxrwx 1 root root 13 Jul 24 13:22 /var/lib/docker -> /data/docker/

本文由作者按照 CC BY 4.0 进行授权