Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

docker - 我如何进入Docker容器的shell?(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执行此操作的正确方法是什么?)

  ask by Andrew translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::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:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 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
$

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...