Skip to content

Instantly share code, notes, and snippets.

@lewcpe
Created January 8, 2020 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewcpe/d2095270a4a8211ec5f0cec4dae93a9b to your computer and use it in GitHub Desktop.
Save lewcpe/d2095270a4a8211ec5f0cec4dae93a9b to your computer and use it in GitHub Desktop.

ใช้ docker image แบบ non-root

หลายๆ ครั้งเราต้องการรัน docker image โดยให้ซอฟต์แวร์อ่าน/เขียนข้อมูลภายใต้สิทธิ์ของ user เราเอง ไม่ใช่ root ซึ่งจะทำให้หลังจากรันโปรแกรมเสร็จแล้ว เราก็ไม่ต้องใช้สิทธิ์ root ในการจัดการไฟล์เหล่านั้น โดยเฉพาะกรณีผู้ใช้ CentOS/RHEL 8 แล้วใช้ podman จะมีผลมากกว่า เพราะ podman ไม่ต้องการ daemon ในสิทธิ์ root อยู่แล้ว

สำรวจดูพบว่าอิมเมจแต่ละอันมีวิธีการรันบน non-root ไม่เหมือนกัน เช่น postgres นั้นใช้อาร์กิวเมนต์ user ของ docker ไปตรงๆ เลย หากเขียนเป็น docker-compose.yml จะได้ประมาณนี้ (ในกรณี uid ของผู้ใช้ปัจจุบันเป็น 1001 และ gid เป็น 1005)

version: '3.5'
services:
    postgres:
      image: postgres:10
      restart: always
      volumes:
       - "./db-data:/var/lib/postgresql/data"
      user: "1001:1005"

แม้จะมีอาร์กิวเมนต์ user แต่ไม่ใช่ทุกอิมเมจจะใช้ตรงกัน ในกรณี gogs คนสร้างอิมเมจเลือกจะเขียนสคริปต์เปลี่ยน uid/gid เอง โดยปรับค่าได้จากตัวแปร environment PUID และ PGID ดังไฟล์ docker-compose.yml

version: '3.5'
services:
    gogs:
      image: gogs/gogs:latest
      restart: always
      ports:
       - "10022:10022"
       - "3000:3000"
      environment:
       - "RUN_CROND=true"
       - "PUID=1001"
       - "PGID=1005"

แต่โดยทั่วไปแล้ว หลายอิมเมจสามารถใช้งานโดยไม่มีสิทธิ์ root ได้ สามารถสร้าง volume ในสิทธิ์ผู้ใช้ non-root มารันคอนเทนเนอร์โดยคอนฟิกเพิ่มเล็กน้อย

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment