1.下载docker-19.03.8.tgz
wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.8.tgz
2.解压 docker-19.03.8.tgz
tar -xvf docker-19.03.8.tgz
- 复制
sudo cp docker/* /usr/bin/
4.复制一下内容到 vim /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
5.加载文件 使其生效
systemctl daemon-reload
6.重启动docker
systemctl restart docker
7.开机自启docker命令
systemctl enable docker
8.重启系统检查服务是否正在运行
ps -aux | grep docker
9.检查服务是否开机启动
systemctl is-enabled docker.service
如果docker开机未自启,按照以下命令重新设置开启自启
1.将服务配置成开机启动
systemctl enable docker.service
2.启动服务
systemctl start docker.service
Docker更换国内镜像源
修改/etc/docker/daemon.json配置文件
cat << 'EOF' > /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com" ],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "50m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
容器开机启动: 创建容器时候指定restart参数:
docker run -it -p 6379:6379 --restart=always --name redis -d redis
docker自启
docker run -d --name phpmyadmin -p 80:80 --restart=always 44924:latest
对已经创建的容器用docker update 更新:
docker update --restart=always xxx
--restart具体参数值详细信息 :
no - 容器退出时,不重启容器
on-failure - 只有在非0状态退出时才从新启动容器
always - 无论退出状态是如何,都重启容器
安装docker-compose
(1)首先访问 docker-compose 的 GitHub 版本发布页面:
(2)由于服务器是 CentOS 系统,则下载 Linux 版本的 Compose:
(3)将下载下来的“docker-compose-Linux-x86_64”文件上传到服务器上,然后执行如下命令将其移动到 /usr/local/bin,并改名为“docker-compose”。
mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
(4)接着执行如下命令添加可执行权限:
chmod +x /usr/local/bin/docker-compose
(5)最后使用 docker-compose -v 命令测试是否安装成功(如果提示“没有那个文件或目录”可以重启下服务器再试试):
kail安装好docker之后 构建容器可能会报错,使用以下命令解决该问题
sudo mkdir /sys/fs/cgroup/systemd
sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd