Part 6 - 容器的应用1

2023-08-27
6分钟阅读时长

【版本】

当前版本号v20231127

版本修改说明
v20231127修正虚拟机名字
v20231104修正修改主机名的命令
v20230827初始化版本

任务6.1 导入 Docker hello-world 镜像并启动

【任务目的】

  • 在理解Docker容器化平台
  • 掌握Docker容器化平台中有关容器管理的常用命令

【任务环境】

  • RockyLinux

【任务资源】

  • hello-world.tar 该文件是一个 Docker 镜像,镜像会打印欢迎信息。

【任务说明】

    1. 可以离线完成本次任务。(任务用到的hello-world镜像已经拉下到到本地,不需要用docker pull hello-world命令)
    1. hello-world镜像说明
    • 2.1 hello-world镜像是Docker Hub官方提供的镜像(Docker Hub 是一个公共的 Docker 镜像存储器,其中包含了大量的镜像供用户使用。其中之一就是 hello-world 镜像)。
    • 2.2 hello-world 镜像是 Docker 团队提供的一个非常简单的示例镜像,用于验证 Docker 是否正确安装和运行。它仅包含一个很小的容器,该容器在启动后会输出一条欢迎消息。
    • 2.3 镜像详情
      • 镜像名称:hello-world
      • 镜像标签:latest
      • 镜像大小:非常小,仅几百字节
      • 镜像来源:由 Docker 团队维护并托管在 Docker Hub 上
      • 使用 hello-world 镜像非常简单。只需执行以下命令即可:docker run hello-world
      • 执行上述命令后,Docker 将从 Docker Hub 下载 hello-world 镜像(如果本地不存在),然后在容器中启动该镜像。容器启动后,将输出以下消息:
Hello from Docker!
This message shows that your installation appears to be working correctly.

这条消息表明 Docker 安装和运行正常。如果你能看到这条消息,那么你的 Docker 环境已经成功配置。 需要注意的是,hello-world 镜像不会一直运行,它只是在容器启动后输出一条消息,然后容器就会自动停止。这是因为 hello-world 镜像只用于验证 Docker 的安装和运行情况,而不是用于实际的应用程序。

【任务步骤】

  1. 运行docker虚拟机,使用 root 用户登录。

  2. SSH客户端重新登陆,提示符#左侧提示如下,下面的主机名作为任务水印,在摘图时要把自已的主机名也包含在内。

[root@999qzm ~]#
  1. 查看docker的守护进程Daemon是否处于运行(active)状态
systemctl status docker 
  1. 列出docker本地镜像
docker images
  • 默认情况下是没有镜像的。
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
说明
REPOSITORY仓库
TAG镜像标签
IMAGE ID镜像ID
CREATED创建时间
SIZE容量大小
  1. 如果可以联网的情况下,可以从仓库拉取镜像。如果不联网可以上传镜像并导入。
docker pull hello-world:lastest
  • 正常会出现以下消息
Using default tag: latest
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
  • 如果无法联网,可以上传hello-world.tar到 root 用户工作目录,并使用以下命令导入镜像。
docker load < ~/hello-world.tar 
  1. 查看所有镜像。注意观察hello-world镜像的ID。
docker images -a
  1. 引用hello-world镜像新建一个容器,观察输出结果。注意替换镜像ID
docker run 镜像ID
  • 如果输出以下内容则表示
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
  1. 列出正在运行的容器。
docker ps
  1. 列出所有的容器,包括正在运行的和已经停止的。

docker ps -h查看帮助,然后执行正确的命令,列出所有的容器。 标记出在那里看容器的当前状态,比如正在运行、停止等状态。

  1. 再次启动第8步运行的“hello-world”容器。
docker start <容器ID/容器名称>
  1. 输出容器运行后打印的信息。
docker logs <容器ID/容器名称>
  1. 删除上面运行的容器。

提示:首先明确删除容器的是那个命令,然后理解删除指定容器可以通过容器ID和容器名称来删除,具体实现同学们自主尝试。

  1. 上面一步删除了容器hello-world,我们再尝试再运行一个hello-world,并摘图说明在本地容器中那个容器是你在第13步运行起来的容器,这个容器的当前状态是什么状态,为什么是这个状态。

任务6.2 导入 Docker Tomcat 镜像并启动

【任务目的】

  • 掌握 Docker 镜像的导入
  • 掌握 Docker 容器

【任务环境】

  • VirtualBox
  • FinalShell

【任务资源】

  • tomcat.tar 该文件是一个 Docker 镜像,镜像包含一个 Tomcat(HTTP 服务器)应用。

【任务步骤】

  1. 使用root 用户登录docker虚拟机。
su
  1. 上传tomcat.tar到 /root 目录
cd ~
rz
  1. 导入 Docker 镜像
cd /root
docker load -i tomcat.tar
  • 执行完会显示
Loaded image: docker.io/tomcat:latest
  1. 查看所有 Docker 镜像
docker image ls
  • 正常应该能够看到类似以下内容:
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/tomcat    latest              b0e0b0a92cf9        2 days ago          680 MB
  1. 接下来我们将创建一个网页,并让 Docker 的 Tomcat 镜像运行在容器内,并发布该网页到 Tomcat 服务器上。

  2. 创建一个存放网页的目录

mkdir -p /root/test
  1. 创建一个静态的网页,请注意替换为你的学号。
cd /root/test
vi index.html
  • 输入以下内容
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Hello Docker</title>
	</head>
	<body>
	 Hello Docker -by 请替换为你的个人学号
	</body>
</html>
  1. 启动 Docker 的容器,并把容器 8080 端口映射到本机的 8080 端口,本地/root/test映射到容器的/usr/local/tomcat/webapps/test
docker run -d -p 8080:8080 -v /root/test:/usr/local/tomcat/webapps/test tomcat
  1. 浏览器访问http://10.0.0.70:8080/test/index.html,是否能够看到以上编写的网页信息。

  2. 查看 Docker 运行的容器,留意容器的ID(CONTAINER ID)。

docker ps
  • 正常应该能够看到类似以下信息
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
be84ccbed333        tomcat              "catalina.sh run"   31 minutes ago      Up 31 minutes       0.0.0.0:8080->8080/tcp   hardcore_thompson
  1. 终止 Tomcat 容器的运行。
docker stop <替换为上面看到的CONTAINER ID>

注:以上命令的CONTAINER ID可以只输入前面几个字母。

  1. 再次查看 Docker 运行的容器,查看是否已经没有任何容器在运行。
docker ps

【常见问题】

1. 访问 Tomcat 的页面出现 404 错误。

  • 这有可能是因为SELinux 没有关闭导致的本地目录无法映射到容器内目录。执行以下命令可以临时关闭 SELinux。
setenforce 0
  • 要永久关闭 SELinux可以执行以下语句。
vi /etc/selinux/config
  • SELINUX设置为disabled
SELINUX=disabled

2. 无法拉取 Docker 镜像

解决办法:

  • (1)请检查网络连接是否正常。
  • (2)参考任务5.3,通过修改 Docker 镜像源为国内的源来提高镜像拉取速度。

3. 容器无法启动

解决办法:请检查容器的配置和依赖项,并确保所需的端口没有被占用。

4. 容器无法访问外部网络

解决办法:请检查容器的网络配置,确保容器与宿主机在同一网络中,并且网络配置正确。

【常用命令】

Docker 查看镜像

docker images -a

Docker 保存镜像为文件

docker save 镜像ID > 镜像文件路径
  • 例子
[root@docker ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    9c7a54a9a43c   3 months ago    13.3kB
[root@docker ~]# docker save 9c7a > ~/hello-world.tar

Docker 加载镜像文件

docker load < 文件路径
  • 例子
[root@docker ~]# docker load < ~/hello-world.tar 
01bb4fce3eb1: Loading layer [==================================================>]  14.85kB/14.85kB
Loaded image ID: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
[root@docker ~]# docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
<none>       <none>    9c7a54a9a43c   3 months ago   13.3kB

Docker 查看容器

docker ps -a

Docker 创建一个新的容器

docker run 镜像ID
  • 例子
[root@docker ~]# docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
<none>       <none>    9c7a54a9a43c   3 months ago   13.3kB
[root@docker ~]# docker run 9c7a

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND    CREATED         STATUS                     PORTS     NAMES
37b44906db25   9c7a      "/hello"   7 seconds ago   Exited (0) 6 seconds ago             lucid_montalcini

Docker 启动容器

docker start 容器ID

Docker 停止容器

docker stop 容器ID

Docker 移除容器

docker container rm 容器ID
  • 例子
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
8f2e44aed9ba   hello-world   "/hello"   14 minutes ago   Exited (0) 14 minutes ago             recursing_matsumoto
[root@docker ~]# docker container rm 8f2e
8f2e
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

扫码或长按识别访问