DEV Community

Phongphan Phienphanich
Phongphan Phienphanich

Posted on

2 3

ประสบการณ์ย้าย Container ข้าม partition

HDD100%

บังเอิญ (จริงๆ ก็คิดไว้แล้วว่าต้องมีวันนี้) วันที่ Docker Host ที่วางไว้ใช้สำหรับให้น้องๆ ได้ Dev เกิด Harddisk เต็มแต่ติดปัญหาที่ว่า แต่ละตัวก็ไม่ใช่ Docker ที่จะ Commit แล้วสร้างขึ้นใหม่ได้ (ทั้งข้อมูลก็ไม่ควรที่จะเป็น docker image แต่อย่างใด แต่ก็ลบไม่ได้) สิ่งที่คิดได้อย่างเดียวคือ "ย้าย" มันไปไว้ Harddisk ลูกอื่นซะ

แต่ยังไงหละที่จะไม่ทำให้ข้อมูลหาย หรือย้ายไปแล้วยังสามารถใช้งานได้อยู่ อ่านๆไปอ่านๆ ก็ตกลงปลงใจที่จะใช้ประยุกต์จาก https://github.com/moby/moby/issues/3127 ดังนี้

ขั้นแรก ปิด Docker ก่อนเลย

sudo service docker stop

จากนั้นสร้าง Directory ปลายทาง ที่ต้องการซึ่งจะอยู่บน HDD อีกลูก (ในที่นี้สมมุติไปที่ /somewhere/extra) แล้วจัดการย้ายไฟล์

sudo mkdir -p /somewhere/extra
sudo rsync -av --progress /var/lib/docker/* /somewhere/extra/

จากนั้นเพิ่ม parameter ให้ dockerd เพื่อที่จะอ่านค่าที่อยู่ใหม่โดยทำได้ 2 วิธีคือ แก้ไขไฟล์ /etc/default/docker โดยเพิ่ม

DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 -g /mnt/extra/docker"

หรือหากใครใช้ไฟล์ /etc/docker/daemon.json ก็สามารถแก้ไขไฟล์นี้โดยเพิ่ม

{
  ...
  "data-root": "/somewhere/extra",
  ...
}

สวดมนต์และภาวนา จากนั้นก็เปิด Docker ได้เลย

sudo service docker start

หากว่าทุกอย่างกลับมาอย่างปกติ ก็ลบ /var/lib/docker ออกได้เลยครับ

ไม่ยาก แต่ลุ้นโคตรๆ

May the force be with you. :)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay