bash 如何进入这个 dockerfile / nginx 容器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26012837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:24:13  来源:igfitidea点击:

How do I enter this dockerfile / nginx container?

bashnginxcentosdockercoreos

提问by peruvian

With centos in a docker container, I just type 'docker attach container ID' and it takes me to the shell prompt, where i can install and configure nginx.

使用 docker 容器中的 centos,我只需输入“docker attach 容器 ID”,它就会将我带到 shell 提示符,在那里我可以安装和配置 nginx。

This one is easier: docker.com dockerfile/nginxYou just run the file and everything is installed and configured.

这个更简单:docker.com dockerfile/nginx您只需运行该文件,一切都已安装和配置。

but i can't figure out how to get in and access the files.

但我不知道如何进入和访问文件。

回答by Tadas ?ubonis

UPDATE(a much easier method that was introduced later):

UPDATE(稍后介绍的更简单的方法):

docker exec -t -i container_name /bin/bash

Original answer

原答案

Actually you can access a running container too.

实际上,您也可以访问正在运行的容器。

Find your container's ID:

查找容器的 ID:

docker ps

Export the ID of the process that runs the container:

导出运行容器的进程的 ID:

PID=$(docker inspect --format '{{.State.Pid}}' my_container_id)

"Connect" to it by changing namespaces:

通过更改命名空间“连接”到它:

nsenter --target $PID --mount --uts --ipc --net --pid

Originally this was described here: http://jpetazzo.github.io/2014/03/23/lxc-attach-nsinit-nsenter-docker-0-9/

最初这是在这里描述的:http: //jpetazzo.github.io/2014/03/23/lxc-attach-nsinit-nsenter-docker-0-9/

回答by SJX

In my case the standard bash didn't exist. Using /bin/sh helped me:

就我而言,标准 bash 不存在。使用 /bin/sh 帮助了我:

docker run -it -p 80:80 dockerfile/nginx /bin/sh

回答by Andreas Steffan

First make sure to understand the difference between images and containers. Running the image:

首先确保了解图像和容器之间的区别。运行图像:

docker run -d -p 80:80 dockerfile/nginx

creates a new container executing only nginx. This process does not interact like a shell. If you really need access to the files in this container while its running, your only option is to use nsinit, nsenter or lxc-attach. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/for details.

创建一个仅执行 nginx 的新容器。这个过程不像 shell 那样交互。如果您确实需要在容器运行时访问其中的文件,您唯一的选择是使用 nsinit、nsenter 或 lxc-attach。有关详细信息,查看https://blog.codecentric.de/en/2014/07/enter-docker-container/

Alternatively, you might want to try

或者,您可能想尝试

docker run -it -p 80:80 dockerfile/nginx /bin/bash

which creates a new container executing an interactive shell instead of nginx.

它创建了一个新容器,它执行交互式 shell 而不是 nginx。