- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
两者都可以在容器中执行命令。两者都可以分离容器。
那么 docker exec 和 docker attach 的真正区别是什么?
最佳答案
2015:有一个 commit PR添加到文档中:
Note: This command (
attach
) is not for running a new process in a container.See:docker exec
.
“Docker. How to get bash\ssh inside runned container ( run -d
)?”的答案说明了区别:
(docker >= 1.3) If we use
docker attach
, we can use only one instance of shell.
So if we want to open new terminal with new instance of container's shell, we just need to rundocker exec
if the docker container was started using
/bin/bash
command, you can access it using attach, if not then you need to execute the command to create a bash instance inside the container usingexec
.
如 this issue 中所述:
- Attach isn't for running an extra thing in a container, it's for attaching to the running process.
- "
docker exec
" is specifically for running new things in a already started container, be it a shell or some other process.
同样的问题补充道:
While
attach
is not well named, particularly because of the LXC commandlxc-attach
(which is more akindocker exec <container> /bin/sh
, but LXC specific), it does have a specific purpose of literally attaching you to the process Docker started.
Depending on what the process is the behavior may be different, for instance attaching to/bin/bash
will give you a shell, but attaching to redis-server will be like you'd just started redis directly without daemonizing.
2022 年更新:使用来自 Containers 101: attach vs. exec - what's the difference? 的“ Ivan Velichko ”(2021 年 12 月)查看更多信息:
摘录:
Difference between attach and logs
On the diagram above,
docker attach
streams the container's logs back to the terminal.
However, thedocker logs
command does a similar thing.
So, what's the difference?The logs command provides various options to filter the logs while attach in that regard acts as a simple tail.
But what's even more important is that the stream established by the logs command is always unidirectional and connected to the container's logs, not the container's stdio streams directly.The logs command simply streams the content of the container's logs back to your terminal, and that's it.
So, regardless of how you created your container (interactive or non-interactive, controlled by a pseudo-terminal or not), you cannot accidentally impact the container while using the logs command.However, when attach is used:
- If a container was created in the interactive mode (
-i
), everything you type in the terminal after attach-ing to the container will be sent to its stdin.- You can (intentionally or accidentally) send a signal to the container - for instance, hitting ctrl+c on your end while attached sends
SIGINT
to the container.What does exec command do
The exec command is actually a totally different story.
In the case of attach, we were connecting our terminal to an existing container (read, process).
However, the
exec
commandstarts a totally new container!
In other words,exec
is a form of therun
command (which itself is just a shortcut forcreate
+start
).
Bart提醒我们the comments那 docker exec
在 running 容器中运行新命令。不是“全新的”。
关于docker - docker attach 和 docker exec 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960686/
我正在尝试发送一封带有附件和 html 内容的邮件。我知道如何分别发送 html 内容和附件,但是是否可以同时发送 html 和附件? 这是我尝试过的: public static void send
所以我真的很难弄清楚什么时候应该附加到一个对象,什么时候不应该附加到一个对象。首先,这是我的(非常简化的)对象模型的小图。 在我的 DAL 中,我每次执行与数据相关的操作时都会创建一个新的 DataC
更新:Docker 0.9.0 现在使用 libcontainer,从 LXC 转移参见:Attaching process to Docker libcontainer container 我正在运
我按照此页面上的说明进行操作: https://developers.facebook.com/docs/plugins/share-button/#settings 我得到一个工作共享对话框,但是当
我有一个现有代码可以正确下载和处理一些电子邮件。 要处理的电子邮件必须有一个或多个 xml 作为附件,现在我正在迁移这个过程从当前的标准邮件帐户到一个认证系统,该系统将邮件包装到一个新的电子邮件。 因
我不太明白通过 attach api 连接到另一个虚拟机是什么意思.我读到每个 Java 程序都在其自己的虚拟机中运行(参见 here )。那么对于一个程序“附加”到另一个 jvm 进程以便它可以访问
无论如何我可以强制使用这种方法,ActiveStorage::Attached#attach不排队后台工作?换句话说,我想禁用似乎包含在 ActiveStorage::Attached#attach
在 Eclipse 插件开发中,我通过 MANIFEST.MF 文件导入包。所以我没有 lib 文件夹,也没有引用的库部分。 即使对于像 String.format() 这样最基本的方法,我也看不到
我不知道执行此操作的确切方法。我想要一种方法,而不是针对 Eclipse 中所有项目的单个项目。请告诉我如何解决这个问题。 最佳答案 这是在 Eclipse 中的类路径中的一个 jar,你有 附加了一
我有一个多个文件要附加到选择器 View 中。当用户选择该选择器 View 项目时,他们可以单击电子邮件按钮来附加所选文件。我该如何在选择器 View 中执行此操作? 这是我的示例代码。 M 文件:
经过this之后通过讨论,我相信附加到同一虚拟机的选项默认情况下已在 OpenJDK11 中禁用。 我正在尝试将 java 代理升级到 OpenJDK11,在测试用例中,当调用 VirtualMach
首先 - 我知道 UWSGI 建议使用 smart-attach-daemon 来自:http://uwsgi-docs.readthedocs.io/en/latest/AttachingDaemo
我使用axios从Reaction网站调用我的API(Django服务器),我希望在授权头的每个请求中都出现一个带有持有者令牌的授权头。。这就是我如何设置授权头apiClient.defaults.h
我在 heroku 上有一个 rails 应用程序,我无法运行我最新的数据库更改。运行 heroku run rake db:migrate给我 Running `rake db:migrate` a
我使用 strope.js 构建一个简单的 IM(web)。 我有 2 个页面:index.html(用于登录)和 myChat.html(主聊天 View )。 当我通过index.html中的ji
我尝试过“heroku run python manage.py migrate”并收到“超时等待dyno,请参阅https://devcenter.heroku.com/articles/one-o
我正在使用 OpenGL 帧缓冲区对象 (FBO) 在 iOS 上实现模板阴影。代码有效——也就是说,从视觉上看,模板缓冲区正在完成这项工作,而且性能似乎还不错。 但是,当我通过 OpenGL ES
我正在尝试使用 slack 附件来记录应用程序错误,但是像堆栈跟踪这样的大字段表现得很奇怪。 首先,当使用附件时,表格似乎固定为任意宽度,是否有任何更改,以便可以允许更宽的值?否则 50+% 的松弛窗
我花了好几天时间寻找一种解决方案,将带有附件的属性字符串放到 NSPasteboard 上。 我可以读取带有附件的 RTFD 文件,修改其文本和属性,然后将其粘贴到 NSPasteboard 上以供其
我想对数据框的列进行许多修改。但是,由于需要大量的列和转换,我想避免一遍又一遍地使用数据框名称。 在 SAS 数据步中,在一个数据步中,您可以创建一个变量并在定义后立即引用它: data A; s
我是一名优秀的程序员,十分优秀!