Saturday, September 28, 2024

Thay đổi múi giờ của container Docker Podman

-

Để thay đổi múi giờ của container Podman, bạn có thể làm theo các bước sau đây:

1. Sử dụng Tùy chọn -v hoặc --volume để Mount File timezone vào Container.

Bạn có thể mount file timezone từ host vào container bằng cách sử dụng tùy chọn -v hoặc --volume khi khởi động container. Ví dụ, để set múi giờ của container thành UTC+7, bạn có thể làm như sau:

podman run -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro <image-name>

2. Cài Đặt và Thiết Lập Timezone Bên Trong Container.

Nếu bạn không muốn mount trực tiếp, bạn cũng có thể thay đổi múi giờ bên trong container bằng cách cài đặt tzdata và thiết lập timezone:

podman run -it <image-name> /bin/bash

Sau đó, trong container:

# Cài đặt tzdata
apt-get update && apt-get install -y tzdata

# Thiết lập múi giờ thành Asia/Ho_Chi_Minh (UTC+7)
ln -sf /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime
echo "Asia/Ho_Chi_Minh" > /etc/timezone

Nếu bạn găp lỗi dưới tức là bạn đang gặp vấn đề với các dependency không được thỏa mãn khi cài đặt tzdata trong container. Để giải quyết vấn đề này, bạn cần chạy lệnh apt --fix-broken install để tự động sửa các dependency bị thiếu.

apt-get shell> update && apt-get install -y tzdata
^C
root@SOC-CEPH-PUB-INFRAS-BACKUP-15:~# podman exec -it  ceph-dailybackup_pubc06_1 bash
root@SOC-OPS-CEPH-PUB-C04-BACKUP-071:/# export http_proxy=http://10.10.16.199:3128
export https_proxy=http://10.10.16.199:3128
root@SOC-OPS-CEPH-PUB-C04-BACKUP-071:/# apt-get update && apt-get install -y tzdata
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
Get:4 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [1092 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [2542 kB]
Get:7 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [2469 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1391 kB]
Get:9 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1917 kB]
Get:10 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [2183 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [44.7 kB]
Get:12 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [51.5 kB]
Get:13 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [31.8 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [81.0 kB]
Fetched 12.2 MB in 11s (1113 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 dbus : Depends: libapparmor1 (>= 2.8.94) but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

Dưới đây là các bước cụ thể để khắc phục:

Trước tiên, hãy chạy lệnh sau để sửa các dependency bị thiếu:

apt-get update && apt-get --fix-broken install

Sau khi hoàn tất việc sửa các dependency, bạn có thể tiếp tục cài đặt tzdata:

apt-get install -y tzdata

Ví dụ hoàn chỉnh từ đầu.

Dưới đây là ví dụ hoàn chỉnh từ việc vào container, thiết lập proxy và chạy các lệnh cần thiết:

shell> podman exec -it ceph-dailybackup_pubc06_1 bash

# Trong container nếu bạn có proxy thì hãy trỏ proxy như bình thường.
export http_proxy=http://10.10.16.199:3128
export https_proxy=http://10.10.16.199:3128

apt-get update && apt-get --fix-broken install
apt-get install -y tzdata

Sau khi tzdata được cài đặt, bạn có thể thiết lập múi giờ thành Asia/Ho_Chi_Minh (UTC+7) bằng các lệnh sau:

ln -sf /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime
echo "Asia/Ho_Chi_Minh" > /etc/timezone

Cuối cùng, kiểm tra múi giờ để đảm bảo rằng nó đã được thay đổi chính xác:

shell> date
Tue Jun 18 12:29:38 AM +07 2024

Lưu ý, nếu trong quá trình cài đặt bạn nhận được lời nhắc chọn vùng mà múi giờ như dưới, thì bạn có thể bỏ qua bước ln -sf /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime nhé.

shell> apt-get install -y tzdata
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  dbus gir1.2-glib-2.0 libapparmor1 libdbus-1-3 libgirepository-1.0-1 networkd-dispatcher python3-dbus python3-gi
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 89 not upgraded.
Need to get 348 kB of archives.
After this operation, 3994 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 tzdata all 2024a-0ubuntu0.22.04 [348 kB]
Fetched 348 kB in 0s (21.3 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package tzdata.
(Reading database ... 17517 files and directories currently installed.)
Preparing to unpack .../tzdata_2024a-0ubuntu0.22.04_all.deb ...
Unpacking tzdata (2024a-0ubuntu0.22.04) ...
Setting up tzdata (2024a-0ubuntu0.22.04) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
Configuring tzdata
------------------

Please select the geographic area in which you live. Subsequent configuration questions will narrow this down by presenting a list of cities, representing the time zones in which they are located.

  1. Africa  2. America  3. Antarctica  4. Australia  5. Arctic  6. Asia  7. Atlantic  8. Europe  9. Indian  10. Pacific  11. US  12. Etc
Geographic area: 6

Please select the city or region corresponding to your time zone.

  1. Aden    7. Ashgabat  13. Barnaul     19. Chongqing  25. Dushanbe     31. Hong_Kong  37. Jerusalem  43. Khandyga      49. Macau     55. Novokuznetsk  61. Pyongyang  67. Sakhalin       73. Taipei    79. Tokyo          85. Vientiane
  2. Almaty  8. Atyrau    14. Beirut      20. Colombo    26. Famagusta    32. Hovd       38. Kabul      44. Kolkata       50. Magadan   56. Novosibirsk   62. Qatar      68. Samarkand      74. Tashkent  80. Tomsk          86. Vladivostok
  3. Amman   9. Baghdad   15. Bishkek     21. Damascus   27. Gaza         33. Irkutsk    39. Kamchatka  45. Krasnoyarsk   51. Makassar  57. Omsk          63. Qostanay   69. Seoul          75. Tbilisi   81. Ujung_Pandang  87. Yakutsk
  4. Anadyr  10. Bahrain  16. Brunei      22. Dhaka      28. Harbin       34. Istanbul   40. Karachi    46. Kuala_Lumpur  52. Manila    58. Oral          64. Qyzylorda  70. Shanghai       76. Tehran    82. Ulaanbaatar    88. Yangon
  5. Aqtau   11. Baku     17. Chita       23. Dili       29. Hebron       35. Jakarta    41. Kashgar    47. Kuching       53. Muscat    59. Phnom_Penh    65. Rangoon    71. Singapore      77. Tel_Aviv  83. Urumqi         89. Yekaterinburg
  6. Aqtobe  12. Bangkok  18. Choibalsan  24. Dubai      30. Ho_Chi_Minh  36. Jayapura   42. Kathmandu  48. Kuwait        54. Nicosia   60. Pontianak     66. Riyadh     72. Srednekolymsk  78. Thimphu   84. Ust-Nera       90. Yerevan
Time zone: 30


Current default time zone: 'Asia/Ho_Chi_Minh'
Local time is now:      Tue Jun 18 00:21:47 +07 2024.
Universal Time is now:  Mon Jun 17 17:21:47 UTC 2024.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

3. Sử dụng Biến Môi Trường TZ.

Một cách khác để thiết lập múi giờ là sử dụng biến môi trường TZ khi khởi động container:

podman run -e TZ=Asia/Ho_Chi_Minh <image-name>

Tổng Kết.

  • Mount file timezone từ host vào container: Cách này đơn giản và không cần thay đổi bên trong container.
  • Cài đặt và thiết lập timezone bên trong container: Phù hợp nếu bạn muốn thay đổi cấu hình bên trong container.
  • Sử dụng biến môi trường TZ: Dễ dàng thiết lập và thay đổi theo yêu cầu.

Hy vọng những cách trên sẽ giúp bạn thiết lập múi giờ cho container Podman của mình!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories