迹忆客 EN >

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

在新的 Docker 容器中启动 Bash 终端

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

Docker 容器是打包应用程序所有依赖项的标准单元,使我们能够在任何环境中轻松运行它们。容器最近变得非常流行,现在大多数开发人员都严重依赖容器来管理他们的应用程序和依赖项。

Docker 通过在 docker 容器中启动 bash 终端,为我们提供了多种访问 shell 实例的方法。这在 docker 容器内运行某些命令时特别有用。

此外,这也可能是出于调试原因,或者你可能想检查所有配置是否正确。本文探讨了我们可用于运行容器和未运行容器的不同方法。

使用 Bash 交互式 Shell

Bash 是 Linux 系统中常见的命令处理器,它允许用户输入导致操作的命令。如果你与基于 Unix 的系统或 WSL 进行了一些交互,你可能会通过 bash 与各种命令进行交互。

同样,我们也可以直接访问 docker 容器中的 Linux 终端并执行命令,就像使用普通 Linux bash 一样。这种方法的一个优点是我们可以使用未运行的容器来执行此操作,这与诸如 docker exec 命令之类的其他命令不同。

如下所示,我们将使用来自 docker 注册表的官方 rabbitmq 映像来创建一个 docker 容器并直接访问容器内的 bash。你可以使用任何其他命令执行此操作;你要确保事先拥有该镜像。

docker pull rabbitmq
docker images

现在我们有了镜像,我们可以交互地创建 docker 容器。这意味着我们可以在运行的同时在 docker 容器内运行命令,如下所示。

$ docker run -it rabbitmq bash

输出:

root@f418a3286aae:/#

如你所见,我们现在位于 docker 容器中,并且我们成功地在新容器中运行了 bash。现在我们可以像使用真正的终端一样执行命令了。

例如,我们可以列出这个容器内的文件和目录,如下所示。

root@f418a3286aae:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f418a3286aae:/#

使用 docker exec 命令

或者,我们也可以使用 docker exec 命令在新的 docker 容器中运行 bash。但是,与之前的方法不同,此命令要求我们已经让容器运行;否则,该命令将不起作用。

使用 docker ps -a 命令确认我们的容器正在运行。如果你要使用的容器没有运行,你可能需要使用 docker start 命令后跟容器 ID 或名称来启动它。

docker ps

我们将在 -it 标签旁边使用 docker exec 命令。exec 命令允许我们在运行的容器中执行命令,而 -it 标签允许我们以交互方式打开容器。

我们可以执行如下所示。

$ docker exec -it f418a3286aae bash

输出:

root@f418a3286aae:/# ls
bin  boot  dev  etc  home  isaac  lib  lib32  lib64  libx32  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys tmp  tonyloi  usr  var

现在我们已经在这个 docker 容器中成功启动了 bash,我们可以在容器中运行各种命令。我们也可以对没有 bash 的容器使用 sh

这也是一个命令,它将打开一个基本的 shell 提示,我们可以在容器中运行我们的命令。

$ docker exec -it f418a3286aae sh

输出:

# ls
bin  boot  dev  etc  home  isaac  isaactonyloi  lib  lib32  lib64  libx32  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys  tmp  tonyloi  usr  var

使用 docker attach 命令

我们还可以使用 docker attach 命令在正在运行的 docker 容器中启动 bash。这允许我们使用容器的 ID 将本地标准输入、输出和错误流附加到正在运行的容器。

然后我们可以运行各种命令,接受输入,调试指定的容器。我们需要一个正在运行的容器来附加我们的输出、输入和错误流。

我们可以使用容器名称或 id 来实现这一点,如下所示。

代码:

$ docker container attach f418a3286aae

输出:

root@f418a3286aae:/# ls
bin  boot  dev  etc  home  isaac  isaactonyloi  lib  lib32  lib64  libx32  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys  tmp  tonyloi  usr  var
root@f418a3286aae:/#

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便