Docker 笔记(部署)

# 镜像加速

编辑 /etc/default/docker 文件

CentOS 系统的配置文件是 /etc/docker/daemon.json Ubuntu 系统的配置文件是 /etc/default/docker

{
  "debug": true,
  "experimental": true,
  "registry-mirrors": [
    "https://9zh8kwqg.mirror.aliyuncs.com",
    "http://hub-mirror.c.163.com"
  ]
}
1
2
3
4
5
6
7
8
sudo systemctl daemon-reload
sudo systemctl restart docker
1
2

# 免 sudo

如果还没有 docker group 就添加一个:

sudo groupadd docker
1

将用户加入该 group 内。然后退出并重新登录就生效啦。

sudo gpasswd -a ${USER} docker
1

重启 docker 服务

sudo service docker restart
1

切换当前会话到新 group 或者重启 X 会话

newgrp - docker
1

注意: 最后一步是必须的,否则因为 groups 命令获取到的是缓存的组信息,刚添加的组信息未能生效,所以 docker images 执行时同样有错。

# 管理面板(portainer)

docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer-ce --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
1
2

装完访问 9000 即可。

# CentOS 7 附录

# CentOS 7 安装后无法联网

ip addr # 查看网卡名称
# 编辑 /etc/sysconfig/network-scripts/网卡名称,设置 ONBOOT=YES
systemctl restart network
1
2
3

# CentOS 7 关闭 SELinux

  • 查看命令getenforce

  • 临时关闭setenforce 0

  • 永久关闭【enforcing—->disabled 或者 permissive—->disabled】

vim /etc/selinux/config

SELINUX=enforcing改为SELINUX=disabled设置后保存,需要重启才能生效。

# CentOS 7 关闭防火墙

sudo systemctl stop firewalld.service       # 停止firewall
sudo systemctl disable firewalld.service    # 禁止firewall开机启动
1
2

# CentOS 8 安装 docker

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
dnf config-manager --add-repo=http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install -y containerd.io
sudo yum install -y --nobest docker-ce docker-ce-cli
sudo systemctl start docker
sudo systemctl enable docker
1
2
3
4
5
6
7
8
9

# CentOS 8 提示 Container.io 版本不匹配

https://download.docker.com/linux/fedora/31/x86_64/stable/Packages/ 下载需要的版本。

# 也可以从阿里镜像的rpm安装
dnf install http://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/Packages/containerd.io-<version>.el8.x86_64.rpm
1
2

# CentOS 8 安装 docker-compose

sudo yum install -y epel-release yum-utils
sudo yum-config-manager --enable epel
pip3 install --user docker-compose
1
2
3

# 复制(迁移)文件保留权限

rsync -rvzpgo --progress
1

# CentOS 8 docker-ce.repo

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/$releasever/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg

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