迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

使用新的伪 TTY 输入正在运行的 Docker 容器

作者:迹忆客 最近更新:2023/04/17 浏览次数:

容器是我们应用程序的运行版本,只有在容器运行时才能访问关键信息。 例如,我们可能想知道容器的日志存储在哪里。

为此,我们必须进入正在运行的容器以获取此特定容器的信息。 在本教程中,我们将学习可以利用的不同方法来使用新的伪 TTY 进入正在运行的 docker 容器。


创建一个新项目

我们将在本教程的所有示例中使用 Nginx 应用程序来展示如何进入正在运行的容器。

打开 WebStorm IDE 并选择“文件”>“新建”>“项目”以创建一个新项目。 在打开的窗口中,选择 Empty Project 并将项目名称从 untitled 更改为 docker-enter-container 或使用任何首选名称。

最后,按标有创建的按钮生成项目。

在当前文件夹下创建一个名为 index.html 的文件,并将以下代码复制并粘贴到该文件中。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"
          rel="stylesheet"
          integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
          crossorigin="anonymous">
</head>
<body>
<table class="table table-dark">
    <thead>
    <tr>
        <th scope="col">#</th>
        <th scope="col">First</th>
        <th scope="col">Last</th>
        <th scope="col">Handle</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <th scope="row">1</th>
        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>
    </tr>
    </tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
        crossorigin="anonymous"></script>
</body>
</html>

在此文件中,我们使用 Bootstrap 创建了一个显示表格的简单网页。 这是我们运行容器时将返回的页面。

随意更改代码以显示任何其他信息。


定义镜像

创建一个名为 Dockerfile 的文件,然后将以下 Docker 说明复制并粘贴到该文件中。

FROM nginx:1.23.1-alpine
COPY . /usr/share/nginx/html
  1. FROM - 这将设置基础图像,使用后续指令在其上创建我们的自定义镜像。 这必须是文件的第一条指令。
  2. COPY - 这会将当前目录中的所有文件和文件夹复制到镜像文件系统。 在这种情况下,我们已将所有文件和文件夹复制到 /usr/share/nginx/html 并使用此位置,因为它是此镜像的要求。

构建镜像

在计算机上按键盘快捷键 ALT+F12 打开一个新的终端窗口。 然后运行以下命令来构建带有标签 enter-container 的镜像。

~/WebstormProjects/docker-enter-container$ docker build --tag enter-container:latest .

该命令顺序执行Dockerfile指令,我们可以在终端窗口看到正在运行的指令,如下。

=> CACHED [1/2] FROM docker.io/library/nginx:1.23.1-alpine@sha256:b87c350e6c69e0dc7069093dcda226c4430f3836682af4f649f2af9  0.0s
 => [2/2] COPY . /usr/share/nginx/html

运行容器

使用相同的终端窗口,执行以下命令以运行名为 enter-container-prod 的容器。

~/WebstormProjects/docker-enter-container$ docker run --name enter-container-prod -d -p 3000:80 enter-container:latest

此命令运行一个容器,并在主机上公开端口 3000 以侦听容器上的端口 80。 查看应用程序,向浏览器上的 localhost:3000 (http://localhost:3000/) 发出请求,以在 index.html 页面上显示表格。

以下部分使用此容器来展示我们可以用来进入容器的不同方法。


使用 docker exec 命令进入正在运行的 Docker 容器

当我们想在正在运行的容器中运行一个新命令时,我们使用 exec 命令。 请注意,此命令仅在容器的主进程正在运行时运行,并且在我们重新启动容器时不会重新启动。

如果未使用 WORKDIR 指令指定工作目录,则该命令在默认目录中运行。 在同一个终端窗口中,执行以下命令进入我们上一节运行的容器。

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod /bin/sh

输出:

/ #

请注意 ,我们还使用了一个附加标志 -it,它指示 Docker 分配一个连接到容器 STDIN 的伪 TTY,从而在容器中创建一个交互式 shell。

如果我们执行 ls 命令,我们可以看到我们在容器的根目录下,如下所示。

/ # ls

输出:

bin                   etc                   mnt                   run                   tmp
dev                   home                  opt                   sbin                  usr
docker-entrypoint.d   lib                   proc                  srv                   var

我们可以用 sh 替换 /bin/sh shell 命令,允许我们在容器中创建一个交互式 shell。 这更容易记住并且可以使用,如下所示。

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod sh

使用 bash 命令替代 sh 命令

我们可以使用 docker exec 命令来替代名为 bash 的 sh 命令。 此命令还允许我们在容器中创建交互式 shell。

bash 命令的使用方式与我们在上一个示例中使用 sh 命令的方式相同。 使用 CTRL+D 退出 shell 会话并执行以下命令以使用 bash 命令进入 Docker 容器。

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod /bin/bash

我们可以用 bash 替换 /bin/bash shell 命令,允许我们在容器中创建一个交互式 shell。 这更容易记住并且可以使用,如下所示。

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod bash

请注意 ,shell 命令取决于安装在容器上的 shell。 一些容器有两个 shell 命令,而其他容器只有一个。


总结

在本教程中,我们学习了可以利用的不同方法来使用新的伪 TTY 进入 Docker 容器。 本文介绍的方法包括将 docker exec 命令与 shell bash 或 sh 命令结合使用。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

发布时间:2025/03/26 浏览次数:202 分类:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便