# Docker 使用 Ubuntu 镜像

**URL:** <https://www.isharkfly.com/t/docker-ubuntu/15739>\
**Category:** DevOps\
**Tags:** ubuntu, docker, linux\
**Created:** [May 13, 2024, 1:52am UTC](https://www.isharkfly.com/t/docker-ubuntu/15739 "2024-05-13T01:52:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 13, 2024, 1:52am UTC](https://www.isharkfly.com/t/docker-ubuntu/15739/1 "2024-05-13T01:52:48Z")

</div>

下面以 `ubuntu:24.04` 为例，演示如何使用该镜像安装一些常用软件。

首先使用 `-ti` 参数启动容器，登录 `bash`，查看 `ubuntu` 的发行版本号。

```bash
PS C:\WorkDir\Beth-Israel-Lahey-Health\Source-Code\SVN> docker run -ti ubuntu:24.04 /bin/bash
Unable to find image 'ubuntu:24.04' locally
24.04: Pulling from library/ubuntu
49b384cc7b4a: Pull complete
Digest: sha256:3f85b7caad41a95462cf5b787d8a04604c8262cdcdf9a472b8c52ef83375fe15
Status: Downloaded newer image for ubuntu:24.04
root@0f38683a6f14:/# cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
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"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
root@0f38683a6f14:/#

```

当试图直接使用 `apt` 安装一个软件的时候，会提示 `E: Unable to locate package curl`。

```bash
root@0f38683a6f14:/# apt install curl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package curl
root@0f38683a6f14:/#

```

这并非系统不支持 `apt` 命令。

Docker 镜像在制作时为了精简清除了 `apt` 仓库信息，因此需要先执行 `apt update` 命令来更新仓库信息。

更新信息后即可成功通过 `apt` 命令来安装软件。

```bash
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [89.7 kB]
Get:3 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [31.0 kB]
Get:4 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [11.8 kB]
Get:5 http://archive.ubuntu.com/ubuntu noble-updates InRelease [89.7 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble-backports InRelease [89.7 kB]
Get:7 http://archive.ubuntu.com/ubuntu noble/main amd64 Packages [1808 kB]
Get:8 http://archive.ubuntu.com/ubuntu noble/restricted amd64 Packages [117 kB]
Get:9 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [331 kB]
Get:10 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages [19.3 MB]
Get:11 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [20.1 kB]
Get:12 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [33.0 kB]
Get:13 http://archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [4224 B]
Fetched 22.2 MB in 35s (637 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
root@0f38683a6f14:/#

```

首先，安装 `curl` 工具。

```bash
root@7d93de07bf76:/# apt install curl

```

在安装的过程中需要同意下，单击 y 继续即可。

接下来，再安装 `apache` 服务。

```bash
root@0f38683a6f14:/# apt install -y apache2

```

启动这个 `apache` 服务，然后使用 `curl` 来测试本地访问。

```bash
root@0f38683a6f14:/# apt install apache2
 * Restarting Apache httpd web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
                                                                                                                 [OK]
root@0f38683a6f14:/#

```

因为我们没有映射外部端口，所以容器中运行的 Apache 还没有办法被容器外访问。

配合使用 `-p` 参数对外映射服务端口，可以允许容器外来访问该服务。

所以，我们可以在当前的命令行工具中，运行命令：

```auto
curl localhost

```

 ![2024-05-12_21-52-26](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/2/25f7c568e7301723a5767ba5091eec05577c572c.png)

控制台工具应该能够返回一个 html 超文本文件，表示访问成功。
