最近需要在uos上部署系統,考慮用docker會簡單一些,但uos默認沒有docker,於是嘗試安裝。兩種安裝方法,1是改安裝源地址然後yum或dnf安裝;2是手動下載靜態包安裝。原則上1簡單一些,但爲了更好理解docker的運行邏輯,選擇了2.
下載docker
wget -c https://download.docker.com/linux/static/stable/x86_64/docker-25.0.4.tgz
具體的文件版本可通過網址直接查看。
解壓壓縮包
tar zxvf docker-25.0.4.tgz
mv docker/* /usr/local/bin
配置docker
mv docker /etc
mkdir docker /home
編輯/etc/docker/daemon.json
{
"data-root": "/home/docker",
"dns" : [
"114.114.114.114",
"8.8.8.8"
],
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn/",
"https://hub-mirror.c.163.com/",
"https://reg-mirror.qiniu.com"
],
"log-driver":"json-file",
"log-opts": {"max-size":"500m", "max-file":"3"}
}
配置docker自啓動
創建以下文件:
containerd.service:
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=1048576
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
docker.service:
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket
[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/local/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=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.
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
docker.socket:
[Unit]
Description=Docker Socket for the API
[Socket]
# If /var/run is not implemented as a symlink to /run, you may need to
# specify ListenStream=/var/run/docker.sock instead.
ListenStream=/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
將以上三個文件放入/etc/systemd/system,然後執行如下指令:
systemctl enable containerd.service
systemctl enable docker.socket
systemctl enable docker.service
使非root用戶也能使用docker[optional]
groupadd docker
usermod -aG docker $USER # 此處$USER即爲想添加使用docker的用戶名
重啓電腦後即可正常使用docker。此時可通過如下指令運行redis和mysql容器,其中‘ASDfgh123'字串爲二者密碼,可根據需要修改。
docker run --name redis -p 6379:6379 --restart=always -d redis --requirepass ASDfgh123
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=ASDfgh123 -e MYSQL_ROOT_HOST='%' --restart=always -d mysql