写在前面
之前写过一篇关于docker安装的博客,那种方式安装有很多缺点。运行docker和使用docker的时候会产生多个进程,占用Linux主机的资源。于是,我找到了新的方式安装docker。
重要的三个文件
/usr/lib/systemd/system/docker.service
/usr/lib/systemd/system/docker.socket
/usr/lib/systemd/system/containerd.service
开始安装docker
添加docker用户
groupadd docker
下载docker
https://download.docker.com/linux/static/stable/x86_64/docker-20.10.7.tgz
解压
tar xzvf docker-20.10.7.tgz
复制文件到/usr/bin/ 目录下
cp docker/* /usr/bin/
建议把docker文件复制到/usr/bin/目录下,如果移动到别的目录有点问题,systemctl脚本启动不起来。
创建docker.socket文件
cat > /usr/lib/systemd/system/docker.socket << 'EOF'
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
EOF
创建containerd.service文件
cat > /usr/lib/systemd/system/containerd.service << 'EOF'
# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=1048576
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
EOF
创建docker.server文件
tee /usr/lib/systemd/system/docker.service <<-'EOF'
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service
[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 --graph=/data/docker -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# 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
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# 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
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
EOF
启动重要的参数:/usr/bin/dockerd --graph=/data/docker -H fd:// --containerd=/run/containerd/containerd.sock
启动docker容器
#重新加载配置文件
systemctl daemon-reload
#启动
systemctl start docker
#设置开机启动
systemctl enable docker.service
#查看docker服务状态
systemctl status docker
#查看docker版本
docker -v
#查看docker容器里的镜像
docker images
#查看容器数据文件存储路径
docker info |grep "Docker Root"
镜像导入
[root@ncayu docker-images]# docker load -i minio-latest.tar
[root@ncayu docker-images]# docker load -i mysql-5.7.tar
[root@ncayu docker-images]# docker load -i redis-6.2.3.tar
docker进程展示
安装docker-compose1.25.0
curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 添加可执行权限
sudo chmod +x /usr/local/bin/docker-compose
# 添加快捷启动连接
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# 测试安装结果
docker-compose --version
标签:bin,容器,systemd,compose,service,usr,docker,安装
From: https://www.cnblogs.com/ncayu2025/p/17621147.html