2.2 容器内安装程序运行环境
基础镜像 ubuntu:24.04 并没有安装 Python,但已经安装了 apt 命令。我们可以使用 apt 命令来安装 python3。
安装步骤如下:
apt update命令更新 apt 源列表。apt install python3命令安装 Python3- 解压缩
mywebsite.tar.gz的网页内容到容器的/root/文件夹下。
1. 更新 apt 源列表
root@26371130cbef:/# apt update
Get:1 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:3 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [1108 kB]
... 此处内容省略
9 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@26371130cbef:/#
2. 安装 Python3
root@26371130cbef:/# apt install python3
Reading package lists... Done
Building dependency tree... Done
... 此处内容省略
After this operation, 30.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y # 此处输入 y 同意。
Unpacking libreadline8t64:amd64 (8.2-4build1) ...
Selecting previously unselected package libsqlite3-0:amd64.
... 此处内容省略
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. Arctic 5. Asia 6. Atlantic 7. Australia 8. Europe 9. Indian 10. Pacific 11. Etc 12. Legacy
Geographic area: 5 # 此处输入 5 选择位于亚洲
Please select the city or region corresponding to your time zone.
1. Aden 10. Bahrain 19. Chongqing 28. Harbin 37. Jerusalem 46. Kuala_Lumpur 55. Novokuznetsk 64. Qyzylorda 73. Tashkent 82. Ust-Nera
2. Almaty 11. Baku 20. Colombo 29. Hebron 38. Kabul 47. Kuching 56. Novosibirsk 65. Riyadh 74. Tbilisi 83. Vientiane
3. Amman 12. Bangkok 21. Damascus 30. Ho_Chi_Minh 39. Kamchatka 48. Kuwait 57. Omsk 66. Sakhalin 75. Tehran 84. Vladivostok
4. Anadyr 13. Barnaul 22. Dhaka 31. Hong_Kong 40. Karachi 49. Macau 58. Oral 67. Samarkand 76. Tel_Aviv 85. Yakutsk
5. Aqtau 14. Beirut 23. Dili 32. Hovd 41. Kashgar 50. Magadan 59. Phnom_Penh 68. Seoul 77. Thimphu 86. Yangon
6. Aqtobe 15. Bishkek 24. Dubai 33. Irkutsk 42. Kathmandu 51. Makassar 60. Pontianak 69. Shanghai 78. Tokyo 87. Yekaterinburg
7. Ashgabat 16. Brunei 25. Dushanbe 34. Istanbul 43. Khandyga 52. Manila 61. Pyongyang 70. Singapore 79. Tomsk 88. Yerevan
8. Atyrau 17. Chita 26. Famagusta 35. Jakarta 44. Kolkata 53. Muscat 62. Qatar 71. Srednekolymsk 80. Ulaanbaatar
9. Baghdad 18. Choibalsan 27. Gaza 36. Jayapura 45. Krasnoyarsk 54. Nicosia 63. Qostanay 72. Taipei 81. Urumqi
Time zone: 69 # 此处是选择时区。选择 69. Shanghai
Current default time zone: 'Asia/Shanghai'
Local time is now: Sun Jun 22 01:16:47 CST 2025.
Universal Time is now: Sat Jun 21 17:16:47 UTC 2025.
Run 'dpkg-reconfigure tzdata' if you wish to change it.
Setting up netbase (6.4) ...
... 此处内容省略
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
root@26371130cbef:/#
然后退出容器
root@26371130cbef:/# exit
exit
weimingze@mzstudio:~$
3. 解压缩网页内容到容器的 /root/ 文件夹下
在终端使用 docker cp 命令将 mywebsite.tar.gz 的内容解压缩到 容器的 /root/ 文件夹下。
weimingze@mzstudio:~$ cat mywebsite.tar.gz | sudo docker cp - my_ubuntu:/root/
[sudo] password for weimingze:
Successfully copied 0B to my_ubuntu:/root/
在容器内查看结果,然后退出容器。
root@26371130cbef:/# cd root/
root@26371130cbef:~# ls
mywebsite
root@26371130cbef:~# ls -l
total 4
drwxrwxrwx 2 501 dialout 4096 Jun 21 21:26 mywebsite
root@26371130cbef:~# cd mywebsite/
root@26371130cbef:~/mywebsite# ls -l
total 8
-rwxrwxrwx 1 501 dialout 309 Jun 21 21:25 index.html
-rwxrwxrwx 1 501 dialout 293 Jun 21 21:26 second_page.html
root@26371130cbef:~/mywebsite# exit
exit
weimingze@mzstudio:~$
从上面可见 mywebsite 文件夹已经存在了。
至此,容器内部的文件系统准备完毕。