docker默認(rèn)安裝路徑是在/var/lib/docker
, 如果不修改默認(rèn)位置,很容易導(dǎo)致系統(tǒng)盤爆滿,所以需要修改docker默認(rèn)路徑,以及image的下載路徑
- 查看docker信息:
docker info
root@VM-16-14-ubuntu ~# docker info
Client:
Debug Mode: false
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 19.03.5
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk s yslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.15.0-54-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 15.51GiB
Name: VM-16-14-ubuntu
ID: D7N4:VCZP:Y67T:JAYB:M7EH:FS42:WH5B:FKFV:CSTN:MY3K:GCC5:HG5O
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: No swap limit support
- 查看系統(tǒng)磁盤空間
df -h
root@VM-16-14-ubuntu ~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 7.8G 4.0K 7.8G 1% /dev
tmpfs 1.6G 7.8M 1.6G 1% /run
/dev/vda1 99G 6.3G 89G 7% /
tmpfs 7.8G 24K 7.8G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/vdb1 98G 828M 93G 1% /data
tmpfs 1.6G 0 1.6G 0% /run/user/114
tmpfs 1.6G 0 1.6G 0% /run/user/0
從中可知掛載的數(shù)據(jù)盤在/data
目錄下,所以我們需要把docker相關(guān)的路徑都放到該目錄下
- 修改docker.service配置文件(未起作用,姿勢不對?)
可用winscp或vim 打開/etc/systemd/system/multi-user.target.wants/docker.service
在ExecStart的下一行增加一行--graph=/data/docker
指向要自定義的目錄
[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 -H fd:// --containerd=/run/containerd/containerd.sock
--graph=/data/docker
#重新加載服務(wù)配置文件
systemctl daemon-reload
#重啟docker 服務(wù)
service docker start
#查看數(shù)據(jù)目錄
docker info | grep Dir
- 修改docker啟動配置文件(未起作用,姿勢還不對?)
可以設(shè)置大部分的后臺進程參數(shù),我們只需要修改配置文件指定啟動參數(shù)即可
目錄/etc/default/docker
#關(guān)閉docker 服務(wù)
service docker stop
#移動數(shù)據(jù)到新的目錄
mv /var/lib/docker /data/docker
# 在配置文件最后一行追加下面配置即可
DOCKER_OPTS="-g /data/docker"
#重啟docker 服務(wù)
service docker start
#查看數(shù)據(jù)目錄
docker info | grep Dir
- 新版的 Docker 推薦使用 json 配置文件的方式
修改Daemon configuration file
參考官網(wǎng):https://docs.docker.com/engine/reference/commandline/dockerd//#daemon-configuration-file
完全參數(shù)示例:
{
"authorization-plugins": [],
"data-root": "",
"dns": [],
"dns-opts": [],
"dns-search": [],
"exec-opts": [],
"exec-root": "",
"experimental": false,
"features": {},
"storage-driver": "",
"storage-opts": [],
"labels": [],
"live-restore": true,
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file":"5",
"labels": "somelabel",
"env": "os,customer"
},
"mtu": 0,
"pidfile": "",
"cluster-store": "",
"cluster-store-opts": {},
"cluster-advertise": "",
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5,
"default-shm-size": "64M",
"shutdown-timeout": 15,
"debug": true,
"hosts": [],
"log-level": "",
"tls": true,
"tlsverify": true,
"tlscacert": "",
"tlscert": "",
"tlskey": "",
"swarm-default-advertise-addr": "",
"api-cors-header": "",
"selinux-enabled": false,
"userns-remap": "",
"group": "",
"cgroup-parent": "",
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 64000,
"Soft": 64000
}
},
"init": false,
"init-path": "/usr/libexec/docker-init",
"ipv6": false,
"iptables": false,
"ip-forward": false,
"ip-masq": false,
"userland-proxy": false,
"userland-proxy-path": "/usr/libexec/docker-proxy",
"ip": "0.0.0.0",
"bridge": "",
"bip": "",
"fixed-cidr": "",
"fixed-cidr-v6": "",
"default-gateway": "",
"default-gateway-v6": "",
"icc": false,
"raw-logs": false,
"allow-nondistributable-artifacts": [],
"registry-mirrors": [],
"seccomp-profile": "",
"insecure-registries": [],
"no-new-privileges": false,
"default-runtime": "runc",
"oom-score-adjust": -500,
"node-generic-resources": ["NVIDIA-GPU=UUID1", "NVIDIA-GPU=UUID2"],
"runtimes": {
"cc-runtime": {
"path": "/usr/bin/cc-runtime"
},
"custom": {
"path": "/usr/local/bin/my-runc-replacement",
"runtimeArgs": [
"--debug"
]
}
},
"default-address-pools":[
{"base":"172.80.0.0/16","size":24},
{"base":"172.90.0.0/16","size":24}
]
}
我們要修改的是data-root
如果在/etc/docker
目錄下沒有daemon.json
文件,需要先新建一個
在里面填寫:
{
"data-root":"/data/docker"
}
#重啟docker服務(wù)
service docker restart
#查看數(shù)據(jù)目錄
docker info | grep Dir
Docker Root Dir: /data/docker
- 驗證
拉取hello-world鏡像
docker pull hello-world
在/data/docker/image/overlay2
目錄下的repositories.json
文件中查看有類似如下內(nèi)容,則說明,鏡像已經(jīng)存放到了自定義的位置
{"Repositories":{"hello-world":{"hello-world:latest":"sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e","hello-world@sha256:d1668a9a1f5b42ed3f46b70b9cb7c88fd8bdc8a2d73509bb0041cf436018fbf5":"sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e"}}}
成功!