[TOC]

文章参考:https://yeasy.gitbook.io/docker_practice/image/pull

概述

Ubuntu 是基于 Debian 的 Linux 操作系统。

Ubuntu安装

访问 Ubuntu 镜像库地址: https://hub.docker.com/_/ubuntu?tab=tags&page=1。

可以通过 Sort by 查看其他版本的 Ubuntu。默认是最新版本 ubuntu:latest 。

拉取Ubuntu

1
2
3
$ docker pull ubuntu
# 或者
$ docker pull ubuntu:latest

查看本地镜像

1
2
3
4
(base)  frewen@FreweniMacBookPro  ~  docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 5d2df19066ac 3 weeks ago 63.1MB
alpine/git latest 22d84a66cda4 3 months ago 43.6MB

在上图中可以看到我们已经安装了ubuntu18.04。

运行容器

1
docker run -itd --name ubuntu-test ubuntu:18.04

安装成功

最后我们可以通过 docker ps 命令查看容器的运行信息:

1
docker ps

安装Ubuntu18.04

docker安装完成之后的界面

image-20230218165222479

需要到官网注册docker账号,docker账号注册完成之后登录

去官网注册账号后进行Ubuntu的镜像下载。

去dockerhub进行Ubuntu下载

image-20230218165848593

下载地址:https://hub.docker.com/_/ubuntu

我们在Tags这个界面直接搜索18.04

image-20230218170720457

执行如下安装脚本:

1
docker pull ubuntu:18.04

安装完成之后,如下输出:

1
2
3
4
5
6
(base)  frewen@FreweniMacBookPro  ~/03.ProgramSpace/29.ProgramSkills/17.Docker/01.WorkSpace  docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
72d9f18d70f3: Pull complete
Digest: sha256:a3765b4d74747b5e9bdd03205b3fbc4fa19a02781c185f97f24c8f4f84ed7bbf
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
image-20230218171018319

安装过后在应用中登录自己注册的账号就会出现在Images目录下,显示已经存在ubuntu了,点击运行。就可以将Docker的Ubuntu跑起来了。

运行起来是这样的

image-20230218171222947

运行

有了镜像后,我们就能够以这个镜像为基础启动并运行一个容器。以上面的 ubuntu:18.04 为例,如果我们打算启动里面的 bash 并且进行交互式操作的话,可以执行下面的命令。

1
docker run  -it -v /Users/frewen:/home/frewen/ ubuntu:18.04 /bin/bash

两个目录代表本地目录和Ubuntu目录的一一映射,我们可以在Mac系统下编写代码,同步就会呈现在Ubuntu上。

docker run 就是运行容器的命令,具体格式我们会在容器一节进行详细讲解,我们这里简要的说明一下上面用到的参数。

-it:这是两个参数,一个是 -i:交互式操作,一个是 -t 终端。我们这里打算进入 bash 执行一些命令并查看返回结果,因此我们需要交互式终端。

--rm:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动 docker rm。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用 --rm 可以避免浪费空间。

ubuntu:18.04:这是指用 ubuntu:18.04 镜像为基础来启动容器。

bash:放在镜像名后的是 命令,这里我们希望有个交互式 Shell,因此用的是 bash

运行如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
docker run  -it -v /Users/frewen:/home/frewen/ ubuntu:18.04 /bin/bash
root@ed584a46da83:/# pwd
/
root@ed584a46da83:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@ed584a46da83:/# cd home/frewen/
root@ed584a46da83:/home/frewen#
root@ed584a46da83:/home/frewen# ls
01.WorkSpace 03.ProgramSpace 05.FrewenSpace 07.FantasySpace Applications Documents Library Music NutstoreCloudBridge Public
02.ProjectsSpace 04.CarrerSpace 06.TianLanSpace 08.CloudSpace Desktop Downloads Movies 'Nutstore Files' Pictures baidu
root@ed584a46da83:/home/frewen# ls -ll
total 0
drwxr-xr-x 3 root root 96 Jun 19 2022 01.WorkSpace
drwxr-xr-x 6 root root 192 May 18 2022 02.ProjectsSpace
drwxr-xr-x 11 root root 352 Feb 18 08:30 03.ProgramSpace
drwxr-xr-x 3 root root 96 Dec 4 04:10 04.CarrerSpace
drwxr-xr-x 4 root root 128 Oct 7 08:05 05.FrewenSpace
drwxr-xr-x 5 root root 160 Nov 30 03:20 06.TianLanSpace
drwxr-xr-x 2 root root 64 May 3 2022 07.FantasySpace
drwxr-xr-x 5 root root 160 May 12 2022 08.CloudSpace
drwx------ 8 root root 256 Oct 9 14:07 Applications
drwx------ 5 root root 160 Feb 18 08:01 Desktop
drwx------ 8 root root 256 Feb 17 03:16 Documents
drwx------ 8 root root 256 Feb 18 08:44 Downloads
drwx------ 105 root root 3360 Feb 12 11:10 Library
drwx------ 6 root root 192 Feb 12 02:57 Movies
drwx------ 5 root root 160 Apr 10 2022 Music
drwxr-xr-x 3 root root 96 Nov 19 04:08 'Nutstore Files'
drwxr-xr-x 2 root root 64 Nov 19 04:08 NutstoreCloudBridge
drwx------ 6 root root 192 Jul 2 2022 Pictures
drwxr-xr-x 5 root root 160 May 12 2022 Public
drwxr-xr-x 2 root root 64 Feb 18 03:03 baidu

查看系统信息

1
2
3
4
5
6
7
8
9
10
11
12
13
root@ed584a46da83:/home/frewen/02.ProjectsSpace/02.Projects/01.WorkSpace# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Ubuntu18.04安装git

执行安装命令,提示:

1
2
3
4
5
root@ed584a46da83:/home/frewen/02.ProjectsSpace/02.BaiduProjects/01.WorkSpace# apt-get install git
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package git

解决方法:

1
2
sudo apt-get update
sudo apt-get upgrade

执行如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
root@ed584a46da83:/home/frewen/02.ProjectsSpace/02.BaiduProjects/01.WorkSpace# apt-get update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [3190 kB]
Ign:5 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages
Get:6 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [1426 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [23.8 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1595 kB]
Err:8 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Connection failed [IP: 185.125.190.39 80]
Get:9 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [30.8 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3610 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [1466 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2369 kB]
Fetched 27.0 MB in 1min 1s (443 kB/s)
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease Connection failed [IP: 185.125.190.39 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.
root@ed584a46da83:/home/frewen/02.ProjectsSpace/02.BaiduProjects/01.WorkSpace# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
libpam-modules libpam-modules-bin libpam-runtime libpam0g
4 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 384 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpam0g amd64 1.1.8-3.6ubuntu2.18.04.6 [54.3 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpam-modules-bin amd64 1.1.8-3.6ubuntu2.18.04.6 [40.3 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpam-modules amd64 1.1.8-3.6ubuntu2.18.04.6 [253 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpam-runtime all 1.1.8-3.6ubuntu2.18.04.6 [37.1 kB]
Fetched 384 kB in 7s (58.7 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 4051 files and directories currently installed.)
Preparing to unpack .../libpam0g_1.1.8-3.6ubuntu2.18.04.6_amd64.deb ...
Unpacking libpam0g:amd64 (1.1.8-3.6ubuntu2.18.04.6) over (1.1.8-3.6ubuntu2.18.04.4) ...
Setting up libpam0g:amd64 (1.1.8-3.6ubuntu2.18.04.6) ...
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.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /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
(Reading database ... 4051 files and directories currently installed.)
Preparing to unpack .../libpam-modules-bin_1.1.8-3.6ubuntu2.18.04.6_amd64.deb ...
Unpacking libpam-modules-bin (1.1.8-3.6ubuntu2.18.04.6) over (1.1.8-3.6ubuntu2.18.04.4) ...
Setting up libpam-modules-bin (1.1.8-3.6ubuntu2.18.04.6) ...
(Reading database ... 4051 files and directories currently installed.)
Preparing to unpack .../libpam-modules_1.1.8-3.6ubuntu2.18.04.6_amd64.deb ...
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.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /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
Unpacking libpam-modules:amd64 (1.1.8-3.6ubuntu2.18.04.6) over (1.1.8-3.6ubuntu2.18.04.4) ...
Setting up libpam-modules:amd64 (1.1.8-3.6ubuntu2.18.04.6) ...
(Reading database ... 4051 files and directories currently installed.)
Preparing to unpack .../libpam-runtime_1.1.8-3.6ubuntu2.18.04.6_all.deb ...
Unpacking libpam-runtime (1.1.8-3.6ubuntu2.18.04.6) over (1.1.8-3.6ubuntu2.18.04.4) ...
Setting up libpam-runtime (1.1.8-3.6ubuntu2.18.04.6) ...
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.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /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
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...