进入 Docker 容器的 Shell
本文将演示如何使用多种方式进入 Docker 的容器 Shell。
使用 docker exec
进入 Docker 容器的 Shell
我们需要启动并运行一个容器才能使用此命令。我们可以使用以下命令检查系统中容器的状态。
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 2 days ago Exited (0) 2 days ago epic_jackson
1c955bac1a84 ubuntu "bash" 2 days ago Exited (0) 2 days ago musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 2 days ago Exited (255) 2 days ago 80/tcp distracted_napier
如果我们没有运行容器,我们可以轻松创建一个。我们将使用 rabbitmq
基础镜像来设置一个容器。
在创建容器之前,我们将首先使用 docker pull
命令从注册表中提取基础映像,如下所示。
$ docker run -d rabbitmq
输出:
Dcad9f270643802092ab525796897c357026767863dade831e8c7d7d82c45712
现在,我们应该有一个正在运行的容器。再一次,我们可以确定使用 docker ps
命令。
$ docker ps -a
输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcad9f270643 rabbitmq "docker-entrypoint.s…" About a minute ago Up 57 seconds 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp inspiring_moore
我们将使用 -it
标签旁边的 docker exec
命令进入容器,与文件交互或执行一些调试。
exec
命令将允许我们在正在运行的容器中执行命令,而 -it
标签将使我们能够交互地打开容器。
最后,sh
命令将打开一个基本的 shell 提示符以在容器中运行我们的命令。
isaactonyloi@DESKTOP-HV44HT6:~$ docker exec -it dcad9f270643 sh
#
现在我们已经进入了 Docker 容器,我们可以在容器中运行各种命令。键入 exit
命令并从该模式按回车键返回主终端。
使用 docker container attach
进入 Docker 容器的 Shell
我们还可以使用 docker container attach
命令连接到正在运行的容器。这允许我们使用容器的 ID 将终端输出、输入和错误流附加到正在运行的容器。
然后我们可以运行各种命令,接受输入,调试指定的容器。如前所述,我们需要一个正在运行的容器来附加我们的输出、输入和错误流。
为此,我们将使用 docker ps
,如下所示。我们仍在使用上一节中的 rabbitmq
容器。
$ docker ps -a
输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcad9f270643 rabbitmq "docker-entrypoint.s…" 39 minutes ago Up 38 minutes 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp inspiring_moore
如果 docker 容器已经停止,那么在这种情况下,我们需要首先使用 docker start
命令启动它,就像我们在这里所做的那样。
$ docker start dcad9f270643
dcad9f270643
现在,如果 docker 容器没有按预期工作,我们可以运行 docker container attach
来查看容器内发生了什么。
$ docker container attach dcad9f270643
输出:
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0> Server startup complete; 3 plugins started.
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0> * rabbitmq_prometheus
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0> * rabbitmq_web_dispatch
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0> * rabbitmq_management_agent
使用安全 Shell (SSH) 进入 Docker 容器的 Shell
最后,我们还可以使用通常缩写为 SSH 的安全 shell 在容器内执行命令。但是,这是最不推荐的方式,因为它会导致基础映像膨胀以及我们可能遇到的配置问题。
此外,这种方法存在安全问题,因为我们需要自己管理密钥。我们还需要考虑到某些镜像可能本质上不支持此方法,因此可能需要进一步配置。
但是,如果我们需要使用这种方法,我们必须遵循这些步骤。
-
我们首先需要安装并启用 SSH 服务。
-
然后,我们必须检索容器的 IP 地址。
-
最后,我们使用检索到的 IP 地址 SSH 进入容器。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
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