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

如何为Ubuntu上的Docker容器挂载独立的第二块SATA磁盘?

Hey there, let's break down how to add that SATA disk to your Docker container while keeping both drives isolated from other containers. First off, let's clear up a key question: you'll use docker run (or runtime configuration) for this, not a Dockerfile—here's why and how to do it right.

Why Not Dockerfile?

Dockerfiles are for building portable images, and mounting physical disks is a host-specific operation. Hardcoding disk paths into an image would make it unportable across different servers, so runtime commands are the way to go here.

Step-by-Step Guide

First, let's reference your lsblk output: your server has a SATA disk (sda, 744.7G) and a PCIe NVMe disk (nvme0n1, 349.3G). The NVMe's partition nvme0n1p1 is already mounted in your container, and you want to add the SATA disk (or one of its partitions) next.

The cleanest way is to stop your current container and start a new one with both disk mounts specified. This ensures proper isolation and avoids runtime hacks.

  1. Stop the existing container (backup any critical data first if needed):

    docker stop <your-container-name>
    
  2. Start a new container with both mounts
    Use either the -v (bind mount) flag or --mount (more explicit) flag to attach both disks. Replace placeholders with your actual container/image names and disk paths:

    docker run -d \
      --name <your-container-name> \
      # Mount PCIe NVMe disk to /pcie-disk in the container
      -v /dev/nvme0n1p1:/pcie-disk \
      # Mount SATA disk partition (e.g., /dev/sda6) to /sata-disk in the container
      -v /dev/sda6:/sata-disk \
      <your-image-name>
    
    • If you want to mount the entire SATA disk instead of a partition, replace /dev/sda6 with /dev/sda (but note: you'll need to partition/format it inside the container first, which is less ideal—better to prep it on the host).
    • Add :ro at the end of the mount string (e.g., /dev/sda6:/sata-disk:ro) if you only need read access.
  3. Verify the mounts
    Jump into the container to confirm both disks are accessible:

    docker exec -it <your-container-name> lsblk
    docker exec -it <your-container-name> df -h
    

Option 2: Add Mount to a Running Container (Less Ideal)

If you really don't want to rebuild the container, you can update its configuration and restart it. This achieves the same result as Option 1 but is a bit more roundabout:

  1. Add the new mount to the container's config:
    docker container update --mount add type=bind,source=/dev/sda6,target=/sata-disk <your-container-name>
    
  2. Restart the container to apply changes:
    docker restart <your-container-name>
    

Ensuring Data Isolation

Since you're using bind mounts to attach specific disk partitions directly to your container, no other containers will have access to these disks unless you explicitly add the same mount flags when starting them. This keeps your data isolated exactly as you want.

Quick Notes

  • Format new disks first: If your SATA disk is brand new, format it on the host first (e.g., mkfs.ext4 /dev/sda6) so the container can recognize the filesystem.
  • Permissions: If the container can't write to the disk, check the filesystem permissions on the host or start the container with a user that has access (use the --user flag).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:49:35