本文翻译自:How do I get into a Docker container's shell?

I'm getting started working with Docker. 我开始使用Docker了。 I'm using the WordPress base image and docker-compose. 我正在使用WordPress基本图像和docker-compose。

I'm trying to ssh into one of the containers to inspect the files/directories that were created during the initial build. 我正在尝试ssh到其中一个容器中来检查在初始构建期间创建的文件/目录。 I tried to run docker-compose run containername ls -la , but that didn't do anything. 我试图运行docker-compose run containername ls -la ,但是没有做任何事情。 Even if it did, I'd rather have a console where I can traverse the directory structure, rather than run a single command. 即使它确实如此,我宁愿有一个控制台,我可以遍历目录结构,而不是运行单个命令。 What is the right way to do this with Docker? 使用Docker执行此操作的正确方法是什么?


#1楼

参考:https://stackoom.com/question/22bGv/我如何进入Docker容器的shell


#2楼

docker attach will let you connect to your Docker container, but this isn't really the same thing as ssh . docker attach会让你连接到你的Docker容器,但这与ssh不是一回事。 If your container is running a webserver, for example, docker attach will probably connect you to the stdout of the web server process. 例如,如果您的容器正在运行Web服务器,则docker attach可能会将您连接到Web服务器进程的stdout It won't necessarily give you a shell. 它不一定会给你一个shell。

The docker exec command is probably what you are looking for; docker exec命令可能就是你要找的东西; this will let you run arbitrary commands inside an existing container. 这将允许您在现有容器中运行任意命令。 For example: 例如:

docker exec -it <mycontainer> bash

Of course, whatever command you are running must exist in the container filesystem. 当然,您运行的任何命令都必须存在于容器文件系统中。

In the above command <mycontainer> is the name or ID of the target container. 在上面的命令中, <mycontainer>是目标容器的名称或ID。 It doesn't matter whether or not you're using docker compose ; 你是否使用docker compose并不重要; just run docker ps and use either the ID (a hexadecimal string displayed in the first column) or the name (displayed in the final column). 只需运行docker ps并使用ID(第一列中显示的十六进制字符串)或名称(显示在最后一列中)。 Eg, given: 例如,给出:

$ docker ps
d2d4a89aaee9        larsks/mini-httpd   "mini_httpd -d /cont   7 days ago          Up 7 days                               web

I can run: 我可以跑:

$ docker exec -it web ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
18: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ffinet 172.17.0.3/16 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::42:acff:fe11:3/64 scope link valid_lft forever preferred_lft forever

I could accomplish the same thing by running: 我可以通过运行来完成同样的事情:

$ docker exec -it d2d4a89aaee9 ip addr

Similarly, I could start a shell in the container; 同样,我可以在容器中启动一个shell;

$ docker exec -it web sh
/ # echo This is inside the container.
This is inside the container.
/ # exit
$

#3楼

Another option is to use nsenter . 另一种选择是使用nsenter 。

PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
nsenter --target $PID --mount --uts --ipc --net --pid

#4楼

docker exec will definitely be a solution. docker exec绝对是一个解决方案。 An easy way to work with the question you asked is by mounting the directory inside Docker to the local system's directory . 处理您提出的问题的一种简单方法是将Docker中的目录挂载到本地系统的目录中 。

So that you can view the changes in local path instantly. 这样您就可以立即查看本地路径中的更改。

docker run -v /Users/<path>:/<container path>

#5楼

Notice : this answer promotes a tool I've written. 注意 :这个答案促进了我写的工具。

I've created a containerized SSH server that you can 'stick' to any running container. 我已经创建了一个容器化的SSH服务器,您可以“粘贴”任何正在运行的容器。 This way you can create compositions with every container. 这样您就可以为每个容器创建合成。 The only requirement is that the container has Bash. 唯一的要求是容器有Bash。

The following example would start an SSH server attached to a container with name 'my-container'. 以下示例将启动连接到名为“my-container”的容器的SSH服务器。

docker run -d -p 2222:22 \-v /var/run/docker.sock:/var/run/docker.sock \-e CONTAINER=my-container -e AUTH_MECHANISM=noAuth \jeroenpeeters/docker-sshssh localhost -p 2222

When you connect to this SSH service (with your SSH client of choice) a Bash session will be started in the container with name 'my-container'. 当您连接到此SSH服务(使用您选择的SSH客户端)时,将在名为“my-container”的容器中启动Bash会话。

For more pointers and documentation see: https://github.com/jeroenpeeters/docker-ssh 有关更多指示和文档,请参阅: https : //github.com/jeroenpeeters/docker-ssh


#6楼

要打入正在运行的容器,请键入:

docker exec -t -i container_name /bin/bash

我如何进入Docker容器的shell?相关推荐

  1. Docker安装MoogoDB, 进入容器, mongo shell操作mongoDB

    安装MoogoDB, 进入容器, mongo shell操作mongoDB [ 包含 Docker-Compose方式.普通方式 ] 文章目录 安装MoogoDB, 进入容器, mongo shell ...

  2. shell脚本-----快速进入docker容器

    工具简介 该工具可以方便用户快速进入docker容器,而且只需要知道container id就可以进入容器. 使用示例 #查看container ID #docker ps CONTAINER ID ...

  3. Shell脚本自动监控docker容器的状态

    首先我们来写一个脚本root@server:~# cat docker_monitor.sh #!/bin/bash #监控容器的运行状态 #容器名称 传入参数 containerName=$1 #当 ...

  4. Docker容器化部署config-server无法直接访问

    Docker容器化部署config-server无法直接访问 1. 本机ip启动方式: 2. Docker容器启动 1)问题 2)解决办法 - 本机启动 - 服务器启动俩种方式 3. dockerfi ...

  5. docker容器虚拟化技术_Docker,虚拟机和容器的全面介绍

    docker容器虚拟化技术 by shota jolbordi 通过Shota Jolbordi Docker has been a buzzword for tech people for the ...

  6. Docker(二):Docker 容器使用

    在上文的学习中,我们简单地讲解了 Docker 的基本架构.了解到了 Docker 使用的是 C/S 结构,即客户端/服务器体系结构. 明白了 Docker 客户端与 Docker 服务器进行交互时, ...

  7. 创建尽可能小的 Docker 容器

    当我们在使用 Docker 的时候,你会很快注意到你正在下载很多 MB 作为你的预先配置的容器.一个简单的 Ubuntu 容器很容易超过 200 MB,并且随着在上面安装软件,尺寸在逐渐增大.在某些情 ...

  8. 《Docker容器:利用Kubernetes、Flannel、Cockpit和Atomic构建和部署》——2.2 容器式Linux系统的Docker配置...

    本节书摘来自异步社区<Docker容器:利用Kubernetes.Flannel.Cockpit和Atomic构建和部署>一书中的第2章,第2.2节,作者: [美]Christopher ...

  9. 如何在Docker容器中挂载主机目录

    本文翻译自:How to mount a host directory in a Docker container I am trying to mount a host directory into ...

最新文章

  1. 微软的DeepSinger产生可以英语和中文唱歌的声音
  2. Python通过future处理并发
  3. ASP.NET MVC 简单的分页思想与实现
  4. 什么是软件开发中的 green field 和 brown field 模式 - 绿地开发和棕地开发
  5. z-index ie无效
  6. Azure认知服务之表单识别器
  7. AI医疗智能问答算法赛,超二十万大奖等你来拿
  8. H3C 模拟器 防火墙开启Web功能
  9. 这是一台家庭计算机重启无效,我买了一台二手计算机,配置还可以,但有时会自动重启机器,这是为什? 爱问知识人...
  10. 温度监视器的设计与制作
  11. 微软模拟飞行10厦门航空涂装_《微软模拟飞行》——准备起飞!
  12. 开源开放 | OpenKG发布cnSchema重构版本
  13. 阿里面试官亲述:如何利用设计模式改善业务代码
  14. Java开发必读--初识微服务一定要阅读这篇文章
  15. NFT协议标准梳理:除了ERC721和ERC1155,还有哪些?
  16. 2014.10.10 ——Jim Foley第二场讲座User Interface Design——An Overview
  17. 小程序开发之微信接入微信调用wenxin4j
  18. 产品经理的金字塔之旅---将“打杂”的实习经历描述的高大上!!!
  19. ubuntu日志文件管理
  20. 潜水寻宝:AHP层次分析法应用浅析

热门文章

  1. sigsuspend的理解
  2. 算法-从先序遍历还原二叉树
  3. getDimension/getDimensionPixelSize/getDimensionPixelOffset()
  4. 第七周项目一-成员函数、友元函数和一般函数有区别(1)
  5. HTML5语义化标签综合基础案例,HTML5语义化标签综合案例
  6. Java学习笔记25
  7. Block的副本放置策略
  8. 【Android UI设计与开发】第13期:顶部标题栏(四)自定义ActionBar风格和样式
  9. 【洛谷P3846】【TJOI2007】—可爱的质数(BSGS模板)
  10. Python实现单链表