Docker容器上的时间同步和编码问题深入

/ 默认分类 / 没有评论 / 592浏览

大多dockerfile在build的时候遇到问题之后我先注释掉出问题的那一步,然后先build出一个出问题之前的镜像,run起来,进入容器实地手动的看下问题所在

时间同步问题和编码问题作为最最基础的环境问题,值得深入下

编码问题

alpine 编码

alpine对中文友好,自带en_US.UTF-8编码,如下

root@pa6:~# docker run -tid --name test_alpine alpine:latest /bin/sh
77e25fe295819f387409691726e248a9992c1c38cd861559962ecafb0b058089
root@pa6:~# docker exec -ti test_alpine /bin/sh
/ # 汉字测试
/bin/sh: 汉字测试: not found

解决方案

只需要在Dockerfile指定下

ENV LANG=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8

ubuntu 编码

没有en_US.UTF-8编码,意味着不能输入和输出中文,而我们的程序大多是在和中文打交道

root@pa6:~# docker run -tid --name test_ubuntu ubuntu:latest /bin/sh
084c690730d4fef12ab74bde7e249ed4668354c2928b9df2d33f781eaafb0b1c
root@pa6:~# docker exec -ti test_ubuntu /bin/bash
root@084c690730d4:/# locale -a
C
C.UTF-8
POSIX

解决方案

需要先安装locales命令

RUN apt-get update \
  && apt-get -y install  locales \
  && locale-gen en_US.UTF-8 
ENV LANG=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8

时间问题

大多数分布式程序的运行,是建立在时间同步的基础上

线上容器手动同步时间(example)

一次在ubuntu为base的容器上的时间同步过程

root@a072a5b0a5c4:/# dpkg-reconfigure tzdata
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 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
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   3. Antarctica  5. Arctic Ocean  7. Atlantic Ocean  9. Indian Ocean    11. System V timezones  13. None of the above
  2. America  4. Australia   6. Asia          8. Europe          10. Pacific Ocean  12. US
Geographic area: 6

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

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


Current default time zone: 'Asia/Shanghai'
Local time is now:      Mon May 20 14:10:26 CST 2019.
Universal Time is now:  Mon May 20 06:10:26 UTC 2019.

root@a072a5b0a5c4:/# date  #时间正确
Mon May 20 14:10:30 CST 2019

ubuntu上的时间

系统会默认读取/etc/timezone以确定系统的时区

/etc/localtime的软连接是/usr/share/zoneinfo/localtime

以下是我在传统主机上查看所得

root@pa6:~# ll /usr/share/zoneinfo/localtime
lrwxrwxrwx 1 root root 14 Nov 10  2017 /usr/share/zoneinfo/localtime -> /etc/localtime

dpkg-reconfigure -f noninteractive 免交互设置时间,其实一般遇到这种问题,一查一个准,准有免交互解决方案

解决方案

ARG TZ="Asia/Shanghai"
ENV TZ ${TZ}
ADD Shanghai /etc/localtime # shanghai是主机上的“shanghai“ 文件
RUN apt-get -y install tzdata && echo 'Asia/Shanghai' >/etc/timezone && dpkg-reconfigure -f noninteractive tzdata

alpine上的时间

alpine自动读取/etc/timezone指定的时区并切换

解决方案

ARG TZ="Asia/Shanghai"
ENV TZ ${TZ}
ADD Shanghai /etc/localtime # shanghai是主机上的“shanghai“ 文件
RUN apk add --no-cache  tzdata && echo 'Asia/Shanghai' >/etc/timezone

好了,到这里我就有点爱alpine了,尽管她不久前刚刚出了安全漏洞事件