ubuntu修改docker数据目录

docker默认安装路径是在/var/lib/docker, 如果不修改默认位置,很容易导致系统盘爆满,所以需要修改docker默认路径,以及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
  • 查看系统磁盘空间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

从中可知挂载的数据盘在/data目录下,所以我们需要把docker相关的路径都放到该目录下

  • 修改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
#重新加载服务配置文件
systemctl daemon-reload
#重启docker 服务
service docker start
#查看数据目录
docker info | grep Dir
  • 修改docker启动配置文件(未起作用,姿势还不对?)
    可以设置大部分的后台进程参数,我们只需要修改配置文件指定启动参数即可
    目录/etc/default/docker
#关闭docker 服务
service docker stop

#移动数据到新的目录
mv /var/lib/docker /data/docker

# 在配置文件最后一行追加下面配置即可
DOCKER_OPTS="-g /data/docker"

#重启docker 服务
service docker start

#查看数据目录
docker info | grep Dir
  • 新版的 Docker 推荐使用 json 配置文件的方式
    修改Daemon configuration file
    参考官网:https://docs.docker.com/engine/reference/commandline/dockerd//#daemon-configuration-file

完全参数示例:

{
    "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服务
service docker restart
#查看数据目录
docker info | grep Dir
Docker Root Dir: /data/docker
  • 验证
    拉取hello-world镜像
docker pull hello-world

/data/docker/image/overlay2目录下的repositories.json文件中查看有类似如下内容,则说明,镜像已经存放到了自定义的位置

{"Repositories":{"hello-world":{"hello-world:latest":"sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e","hello-world@sha256:d1668a9a1f5b42ed3f46b70b9cb7c88fd8bdc8a2d73509bb0041cf436018fbf5":"sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e"}}}

成功!

你可能感兴趣的:(ubuntu修改docker数据目录)