docker容器内的attached 和detached模式

本文介绍了Docker容器的两种运行模式:attached模式和detached模式。详细解释了这两种模式的区别及应用场景,包括如何通过端口映射启动容器,并演示了如何在两种模式间进行转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

博主介绍

📢点击下列内容可跳转对应的界面,查看更多精彩内容!

🍎主页:水香木鱼
🍍专栏:docker


文章目录

简介:这是一篇有关【docker容器内的attached 和detached模式】的文章,博主用最精简的语言去表达给前端读者们。

端口映射
attached模式
detached模式
detached模式转换attached模式

1、Docker端口映射

在开启端口映射之前,你首先要之道Docker对应的容器端口是多少。比如Nginx镜像的端口诗80。

知道这个端口后,就可以在启动容器的时候,用-p <port:port> 的形式,启用映射了。

用Nginx举例:

docker container run -p 80:80 nginx

等待项目启动后,打开浏览器窗口,在地址栏输入127.0.0.1,就可以打开nginx的默认网址。

在这里插入图片描述

  • 第一个端口是映射到服务器本机的端口;
  • 第二个端口是Docker容器使用的端口

比如你想把Docker的80端口,映射到服务器的90端口,执行如下命令:

docker container run -p 90:80 nginx

2、attached模式

两种模式最简单的对比理解就是:attached模式在前台运行,detached模式在后台运行。

当你打开127.0.0.1网址的时候,PowerShell上打印出了相关的日志(log),平且每访问一次,都会增加一条日志。

也就是说Docker容器的日志会实时的展现到窗口并且占用此端口。这种模式叫做attached模式

在这里插入图片描述

在windows系统下并不是一个完整的attached模式,只是帮我们打印出了Log。

现在到Linux服务器上,这时候你按Ctrl+C,就会停止掉Docker服务。而现实中我们工作的环境恰恰是这种Linux环境。

【也就是在Linux上你的操作命令,会直接传递个Docker容器。这个缺点就是很容易误操作,比如在公司的生产环境中,你直接一个Ctrl+C,整个服务就崩掉了,你这个月的绩效也就没有了。】

所以我们需要一个更好的,更稳定的模式。也就是detached模式

注意:attached模式更适用于容器和程序的调试阶段

3、detached模式

detached模式的开启方法,就是加一个参数-d或者--detach

docker run -d -p 80:80 nginx

这次你会看到,和attached模式不同的是,这次输入完命令后,只显示出了容器的编号,并且可以再输入任何命令

就算我们关掉窗口,容器依然运行,也就是他是在系统后台进行运行的。

这种就比较适合在生产环境中运行,停掉和删除容器都需要使用Shell脚本的形式。减少了很多误操作。

4、detached模式转换attached模式

在运行之后,也有需要调试的时候,Docker提供了两个模式间的转换。

比如现在要把detached模式的容器,改为attched模式

docker  attach <ID or Image Name>

相关推荐

⭐doker的多容器操作和强制删除容器的方法
⭐docker创建容器相关命令【详细版】
⭐如何在Linux系统下安装Docker
⭐如何安装Docker桌面版到Windows系统上
⭐在Windows10、Windows11系统下安装Docker

### Docker Desktop on Linux Installation and Usage #### Prerequisites for Installing Docker Desktop on Linux To install Docker Desktop, the system must meet certain requirements. The operating system should be a recent version of one of these distributions: Ubuntu (18.04 LTS or later), Fedora (33 or later), Debian (10 or later). Additionally, ensure that virtualization is enabled in BIOS/UEFI settings because it's required by HyperKit which Docker Desktop uses to create VMs[^1]. #### Installation Steps Overview The process involves downloading Docker Desktop from the official website, installing dependencies like HyperKit, and then running an installer package specific to your distribution. For instance, on **Ubuntu**, after ensuring prerequisites are met: ```bash sudo apt-get update && sudo apt-get upgrade -y ``` Install necessary packages including `hyperkit`, `vfio-vsock` kernel module support via APT repository updates as per documentation guidelines provided at Docker’s site. Once all setup scripts complete successfully without errors, launch Docker Desktop through GUI application menus or command line using `com.docker.desktop`. #### Using Docker with Volumes and Containers After successful installation, interacting with containers becomes straightforward. For example, mounting local directories into container environments can be achieved using `-v` flag during runtime commands such as shown below where `${PWD}` represents current working directory path outside container while `/data` specifies internal mount point inside container filesystem structure[^3]: ```bash docker run -d --rm --name qtumd_node1 \ --network=qtum_network \ -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro \ -v ${PWD}/node1_data:/data \ cryptominder/qtum:latest qtumd ``` This command starts a new detached (`-d`) session named `qtumd_node1` within network namespace `qtum_network`. It mounts two volumes read-only configuration file and writable data storage area respectively before executing entrypoint script defined under image tag `cryptominder/qtum:latest`. #### Accessing Running Containers Interacting directly with processes running inside active sessions requires attaching terminal interface linked against target environment. This action utilizes `exec` subcommand alongside specifying shell interpreter type intended for interactive use cases as demonstrated hereunder connecting bash instance attached to previously created service node[^2]: ```bash docker exec -it teamtalk /bin/bash ``` --related questions-- 1. What are common troubleshooting steps when facing issues launching Docker Desktop? 2. How does enabling virtualization improve performance for applications hosted within Docker containers? 3. Can you explain how networks function between host machines and their respective isolated guest instances managed by Docker? 4. In what scenarios would someone prefer utilizing bind mounts over anonymous volumes when configuring persistent storages across multiple services?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水香木鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值