You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 18.04中Docker Swap支持失效,如何启用Swap核算?

问题:Ubuntu 18.04中Docker Swap支持失效,如何启用Swap核算?

Ubuntu 18.04似乎破坏了Docker的Swap支持。该功能在Ubuntu 16.04中运行正常,但今早我搭建的Ubuntu 18.04新服务器上无法正常工作。请问有人知道如何在Ubuntu 18.04中启用Docker的Swap核算吗?

我的Docker信息如下:

root@proxima:~# docker info 
Containers: 0 
Running: 0 
Paused: 0 
Stopped: 0 
Images: 0 
Server Version: 18.05.0-ce 
Storage Driver: btrfs 
Build Version: Btrfs v4.15.1 
Library Version: 102 
Logging Driver: json-file 
Cgroup Driver: cgroupfs 
Plugins: 
 Volume: local 
 Network: bridge host macvlan null overlay 
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog 
Swarm: inactive 
Runtimes: runc 
Default Runtime: runc 
Init Binary: docker-init 
containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88 
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 
init version: 949e6fa 
Security Options: 
 apparmor 
 seccomp 
 Profile: default 
Kernel Version: 4.15.0-20-generic 
Operating System: Ubuntu 18.04 LTS 
OSType: linux 
Architecture: x86_64 
CPUs: 8 
Total Memory: 31.33GiB 
Name: proxima 
ID: HFCK:DE5O:VDGY:PK3Z:WXNP:OF4P:CGIV:Z3XE:TM7F:VIMG:7ZUH:CXNW 
Docker Root Dir: /ozone/docker 
Debug Mode (client): false 
Debug Mode (server): 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 

我的/etc/fstab配置:

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/md2        /               ext4    errors=remount-ro,discard 0       1
/dev/md1        /boot           ext4    errors=remount-ro,discard 0       1
/dev/sda3       swap            swap    defaults        0       0
/dev/sdb3       swap            swap    defaults        0       0
/dev/sda4       /ozone          btrfs   defaults,noatime,ssd,compress=zstd,commit=120 0       0
proc            /proc           proc    defaults        0       0
sysfs           /sys            sysfs   defaults        0       0

解决方案

Ubuntu 18.04里Docker的swap限制支持失效,核心原因是systemd默认未启用swap的cgroup控制器,再加上GRUB配置里缺少必要的内核参数。咱们一步步来修复:

  1. 修改GRUB配置,开启内存与swap的cgroup支持
    编辑/etc/default/grub文件,找到GRUB_CMDLINE_LINUX这一行,添加cgroup_enable=memory swapaccount=1参数。如果原本这一行已有其他参数,直接用空格拼接进去即可,修改后示例如下:

    GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
    
  2. 更新GRUB并重启系统
    执行命令让GRUB配置生效,然后重启服务器:

    sudo update-grub
    sudo reboot
    
  3. 验证修复效果
    重启完成后,再次运行docker info,如果原本的WARNING: No swap limit support提示消失,就说明swap核算功能已经成功启用。

额外补充:从你的Docker信息来看,当前用的是cgroupfs作为cgroup驱动,这在Ubuntu 18.04下是没问题的。如果后续遇到其他cgroup相关问题,可以考虑切换到systemd驱动——只需要编辑/etc/docker/daemon.json,添加"exec-opts": ["native.cgroupdriver=systemd"],然后重启Docker服务即可,但当前的swap问题不需要做这一步。


内容的提问来源于stack exchange,提问作者Mascarpone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:28:06